File tree Expand file tree Collapse file tree 4 files changed +50
-0
lines changed Expand file tree Collapse file tree 4 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -462,3 +462,8 @@ func (c CLI) metadata() plugin.CLIMetadata {
462462func (c CLI ) Run () error {
463463 return c .cmd .Execute ()
464464}
465+
466+ // Command returns the underlying root command.
467+ func (c CLI ) Command () * cobra.Command {
468+ return c .cmd
469+ }
Original file line number Diff line number Diff line change @@ -600,5 +600,14 @@ plugins:
600600 fmt .Sprintf (noticeColor , fmt .Sprintf (deprecationFmt , deprecationWarning ))))
601601 })
602602 })
603+
604+ When ("new succeeds" , func () {
605+ It ("should return the underlying command" , func () {
606+ c , err = New ()
607+ Expect (err ).NotTo (HaveOccurred ())
608+ Expect (c .Command ()).NotTo (BeNil ())
609+ Expect (c .Command ()).To (Equal (c .cmd ))
610+ })
611+ })
603612 })
604613})
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ import (
3131
3232 "sigs.k8s.io/kubebuilder/v4/pkg/config"
3333 cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
34+ "sigs.k8s.io/kubebuilder/v4/pkg/machinery"
3435 "sigs.k8s.io/kubebuilder/v4/pkg/plugin"
3536 "sigs.k8s.io/kubebuilder/v4/pkg/plugins/external"
3637)
@@ -152,6 +153,18 @@ func WithCompletion() Option {
152153 }
153154}
154155
156+ // WithFilesystem is an Option that allows to set the filesystem used in the CLI.
157+ func WithFilesystem (fs machinery.Filesystem ) Option {
158+ return func (c * CLI ) error {
159+ if fs .FS == nil {
160+ return errors .New ("invalid filesystem" )
161+ }
162+
163+ c .fs = fs
164+ return nil
165+ }
166+ }
167+
155168// parseExternalPluginArgs returns the program arguments.
156169func parseExternalPluginArgs () (args []string ) {
157170 // Loop through os.Args and only get flags and their values that should be passed to the plugins
Original file line number Diff line number Diff line change @@ -717,4 +717,27 @@ var _ = Describe("CLI options", func() {
717717 Expect (c .completionCommand ).To (BeTrue ())
718718 })
719719 })
720+
721+ Context ("WithFilesystem" , func () {
722+ When ("providing a valid filesystem" , func () {
723+ It ("should use the provided filesystem" , func () {
724+ fs := machinery.Filesystem {
725+ FS : afero .NewMemMapFs (),
726+ }
727+ c , err = newCLI (WithFilesystem (fs ))
728+ Expect (err ).NotTo (HaveOccurred ())
729+ Expect (c ).NotTo (BeNil ())
730+ Expect (c .fs ).To (Equal (fs ))
731+ })
732+ })
733+
734+ When ("providing a invalid filesystem" , func () {
735+ It ("should return an error" , func () {
736+ fs := machinery.Filesystem {}
737+ c , err = newCLI (WithFilesystem (fs ))
738+ Expect (err ).To (HaveOccurred ())
739+ Expect (c ).To (BeNil ())
740+ })
741+ })
742+ })
720743})
You can’t perform that action at this time.
0 commit comments