Skip to content

Commit a3a622e

Browse files
authored
Merge pull request #4011 from beatrausch/cli-extension
✨ (Only relevant for users of Kubebuilder as a library) - Add new cli option and allow access to underlying cli command
2 parents c8f4c5f + cd1e3f7 commit a3a622e

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

pkg/cli/cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,8 @@ func (c CLI) metadata() plugin.CLIMetadata {
462462
func (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+
}

pkg/cli/cli_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
})

pkg/cli/options.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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.
156169
func parseExternalPluginArgs() (args []string) {
157170
// Loop through os.Args and only get flags and their values that should be passed to the plugins

pkg/cli/options_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff 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
})

0 commit comments

Comments
 (0)