Skip to content

Commit

Permalink
feat/silence usage for shuttle actions (#242)
Browse files Browse the repository at this point in the history
* fix: silence usage for shuttle actions

Signed-off-by: Kasper J. Hermansen <kah@lunar.app>

* fix: errors

Signed-off-by: Kasper J. Hermansen <kah@lunar.app>

* fix: tests

Signed-off-by: Kasper J. Hermansen <kah@lunar.app>

---------

Signed-off-by: Kasper J. Hermansen <kah@lunar.app>
  • Loading branch information
kjuulh committed Aug 23, 2024
1 parent a535647 commit 9f6c4b3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func newRunSubCommand(
}

} else if *inputArgs[arg.Name] == "" && arg.Required && *validateArgs {
return fmt.Errorf("Error: required flag(s) \"%s\" not set", argName(arg.Name))
return fmt.Errorf("required flag(s) \"%s\" not set", argName(arg.Name))
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func TestRun(t *testing.T) {
name: "script fails when required argument is missing",
input: args("-p", "testdata/project", "run", "required_arg"),
stdoutput: "",
erroutput: `Error: Error: required flag(s) "foo" not set
erroutput: `Error: required flag(s) "foo" not set
`,
err: errors.New(`Error: required flag(s) "foo" not set`),
err: errors.New(`required flag(s) "foo" not set`),
},
{
name: "script succeeds with required argument",
Expand Down
11 changes: 7 additions & 4 deletions pkg/executors/golang/cmder/cmder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (rc *RootCmd) Execute() {
} else {
log.Fatalf("%v\n", err)
}

}
}

Expand Down Expand Up @@ -95,6 +94,10 @@ func (rc *RootCmd) TryExecute(args []string) error {

cobracmd := &cobra.Command{
Use: cmd.Name,

// We don't want to show the full usage, instead just show the error
SilenceUsage: true,

RunE: func(cobracmd *cobra.Command, args []string) error {
if err := cobracmd.ParseFlags(args); err != nil {
log.Println(err)
Expand All @@ -119,7 +122,7 @@ func (rc *RootCmd) TryExecute(args []string) error {
if val.Type().Implements(reflect.TypeOf((*error)(nil)).Elem()) {
err, ok := val.Interface().(error)
if ok && err != nil {
log.Println(err)
fmt.Fprintln(cobracmd.ErrOrStderr(), err)
return ErrNoHelp
}
}
Expand All @@ -129,8 +132,8 @@ func (rc *RootCmd) TryExecute(args []string) error {
},
}
for i, arg := range cmd.Args {
cobracmd.PersistentFlags().StringVar(&parameters[i], arg.Name, "", "")
_ = cobracmd.MarkPersistentFlagRequired(arg.Name)
cobracmd.Flags().StringVar(&parameters[i], arg.Name, "", "")
_ = cobracmd.MarkFlagRequired(arg.Name)
}

rootcmd.AddCommand(cobracmd)
Expand Down

0 comments on commit 9f6c4b3

Please sign in to comment.