Skip to content

Commit a9d57ce

Browse files
committed
Ignore unknown falgs for init and create commands
If the user attempts to run a command like: riff create java --handler functions.Uppercase ...but they do not have the java invoker installed, they currently recieve an unhelpful error saying the `--handler` flag is unknown. Since the init and create root comamnds require an invoker, and some invokers require the handler flag while others do no, we can relax the enforcement of unknown flags for these commands. Subcommands continue to enforce unknown flags. The ability to supress unknown flag warnings is new in pflags v1.0.1. Fixes projectriff#555
1 parent 1ecd7e1 commit a9d57ce

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

riff-cli/cmd/create.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func Create(initCmd *cobra.Command, buildCmd *cobra.Command, applyCmd *cobra.Com
2929
createChainCmd.Use = "create"
3030
createChainCmd.Short = "Create a function (equivalent to init, build, apply)"
3131
createChainCmd.Long = utils.CreateCmdLong()
32+
33+
createChainCmd.Flags().ParseErrorsWhitelist.UnknownFlags = true
3234
createChainCmd.SetUsageTemplate(utils.CustomInvokerUsageTemplate)
3335

3436
// ignore all validation

riff-cli/cmd/create_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ var _ = Describe("The create command", func() {
104104
Expect(".").NotTo(HaveUnstagedChanges())
105105
})
106106

107+
It("ignores unknown flags", func() {
108+
os.Chdir("../test_data/riff-init/no-invokers")
109+
110+
invokers, err := stubInvokers("invokers/*.yaml")
111+
Expect(err).NotTo(HaveOccurred())
112+
rootCommand, _, _, _, err := setupCreateTest(invokers, normalDocker, dryRunDocker, normalKubeCtl, dryRunKubeCtl)
113+
Expect(err).NotTo(HaveOccurred())
114+
115+
rootCommand.SetArgs(append([]string{"create", "java", "--handler", "functions.FooFunc"}, commonRiffArgs...))
116+
117+
err = rootCommand.Execute()
118+
Expect(err).To(HaveOccurred())
119+
Expect(err).To(Equal(fmt.Errorf("Invokers must be installed, run `riff invokers apply --help` for help")))
120+
121+
Expect(".").NotTo(HaveUnstagedChanges())
122+
})
123+
107124
It("creates a function with a matched invoker", func() {
108125
os.Chdir("../test_data/riff-init/matching-invoker")
109126

riff-cli/cmd/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func Init(invokers []projectriff_v1.Invoker) (*cobra.Command, *options.InitOptio
6868
initCmd.PersistentFlags().StringVarP(&initOptions.Output, "output", "o", "", "the name of the output topic (optional)")
6969
initCmd.PersistentFlags().BoolVar(&initOptions.Force, "force", utils.DefaultValues.Force, "overwrite existing functions artifacts")
7070

71+
initCmd.Flags().ParseErrorsWhitelist.UnknownFlags = true
7172
initCmd.SetUsageTemplate(utils.CustomInvokerUsageTemplate)
7273

7374
return initCmd, &initOptions

riff-cli/cmd/init_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ var _ = Describe("The init command", func() {
7272
Expect(".").NotTo(HaveUnstagedChanges())
7373
})
7474

75+
It("ignores unknown flags", func() {
76+
os.Chdir("../test_data/riff-init/no-invokers")
77+
78+
invokers, err := stubInvokers("invokers/*.yaml")
79+
Expect(err).NotTo(HaveOccurred())
80+
rootCommand, _, _, _, err := setupInitTest(invokers)
81+
Expect(err).NotTo(HaveOccurred())
82+
83+
rootCommand.SetArgs(append([]string{"init", "--handler", "functions.FooFunc"}, commonRiffArgs...))
84+
85+
err = rootCommand.Execute()
86+
Expect(err).To(HaveOccurred())
87+
Expect(err).To(Equal(fmt.Errorf("Invokers must be installed, run `riff invokers apply --help` for help")))
88+
89+
Expect(".").NotTo(HaveUnstagedChanges())
90+
})
91+
7592
})
7693

7794
Context("with an explict invoker", func() {
@@ -88,6 +105,24 @@ var _ = Describe("The init command", func() {
88105

89106
err = rootCommand.Execute()
90107
Expect(err).To(HaveOccurred())
108+
Expect(err).To(Equal(fmt.Errorf("The invoker must be specified. Pick one of: node")))
109+
110+
Expect(".").NotTo(HaveUnstagedChanges())
111+
})
112+
113+
It("ignores unknown flags", func() {
114+
os.Chdir("../test_data/riff-init/no-matching-invoker")
115+
116+
invokers, err := stubInvokers("invokers/*.yaml")
117+
Expect(err).NotTo(HaveOccurred())
118+
rootCommand, _, _, _, err := setupInitTest(invokers)
119+
Expect(err).NotTo(HaveOccurred())
120+
121+
rootCommand.SetArgs(append([]string{"init", "java", "--handler", "functions.FooFunc"}, commonRiffArgs...))
122+
123+
err = rootCommand.Execute()
124+
Expect(err).To(HaveOccurred())
125+
Expect(err).To(Equal(fmt.Errorf("The invoker must be specified. Pick one of: node")))
91126

92127
Expect(".").NotTo(HaveUnstagedChanges())
93128
})

0 commit comments

Comments
 (0)