Skip to content

Commit

Permalink
fix: shuttle actions currently reports issues double, we don't want t…
Browse files Browse the repository at this point in the history
…hat (#234)

* fix: shuttle actions currently reports issues double, we don't want that

Signed-off-by: Kasper J. Hermansen <contact@kjuulh.io>

* feat: update to silence errors

Signed-off-by: Kasper J. Hermansen <contact@kjuulh.io>

* feat: silence usage on errors

Signed-off-by: Kasper J. Hermansen <contact@kjuulh.io>

---------

Signed-off-by: Kasper J. Hermansen <contact@kjuulh.io>
  • Loading branch information
kjuulh committed Apr 12, 2024
1 parent bc23ef2 commit 2167822
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 62 deletions.
7 changes: 4 additions & 3 deletions cmd/run.go
Expand Up @@ -177,9 +177,10 @@ func newRunSubCommand(
}

cmd := &cobra.Command{
Use: script,
Short: value.Description,
Long: value.Description,
Use: script,
Short: value.Description,
Long: value.Description,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if *interactiveArg {
uii.Verboseln("Running using interactive mode!")
Expand Down
54 changes: 5 additions & 49 deletions cmd/run_test.go
Expand Up @@ -8,30 +8,6 @@ import (
)

func TestRun(t *testing.T) {

usage := `Usage:
shuttle run required_arg [flags]
Flags:
--foo string
-h, --help help for required_arg
Global Flags:
-c, --clean Start from clean setup
--interactive sets whether to enable ui for getting missing values via. prompt instead of failing immediadly, default is set by [SHUTTLE_INTERACTIVE=true/false]
--plan string Overload the plan used.
Specifying a local path with either an absolute path (/some/plan) or a relative path (../some/plan) to another location
for the selected plan.
Select a version of a git plan by using #branch, #sha or #tag
If none of above is used, then the argument will expect a full plan spec.
-p, --project string Project path (default ".")
--skip-pull Skip git plan pulling step
--template string Template string to use. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
--validate Validate arguments against script definition in plan and exit with 1 on unknown or missing arguments (default true)
-v, --verbose Print verbose output
`

pwd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
Expand Down Expand Up @@ -60,29 +36,9 @@ Global Flags:
err: nil,
},
{
name: "exit 1",
input: args("-p", "testdata/project", "run", "exit_1"),
stdoutput: `Usage:
shuttle run exit_1 [flags]
Flags:
-h, --help help for exit_1
Global Flags:
-c, --clean Start from clean setup
--interactive sets whether to enable ui for getting missing values via. prompt instead of failing immediadly, default is set by [SHUTTLE_INTERACTIVE=true/false]
--plan string Overload the plan used.
Specifying a local path with either an absolute path (/some/plan) or a relative path (../some/plan) to another location
for the selected plan.
Select a version of a git plan by using #branch, #sha or #tag
If none of above is used, then the argument will expect a full plan spec.
-p, --project string Project path (default ".")
--skip-pull Skip git plan pulling step
--template string Template string to use. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
--validate Validate arguments against script definition in plan and exit with 1 on unknown or missing arguments (default true)
-v, --verbose Print verbose output
`,
name: "exit 1",
input: args("-p", "testdata/project", "run", "exit_1"),
stdoutput: ``,
erroutput: "Error: exit code 4 - Failed executing script `exit_1`: shell script `exit 1`\nExit code: 1\n",
err: errors.New(
"exit code 4 - Failed executing script `exit_1`: shell script `exit 1`\nExit code: 1",
Expand All @@ -107,7 +63,7 @@ Global Flags:
{
name: "script fails when required argument is missing",
input: args("-p", "testdata/project", "run", "required_arg"),
stdoutput: usage,
stdoutput: "",
erroutput: `Error: Error: required flag(s) "foo" not set
`,
err: errors.New(`Error: required flag(s) "foo" not set`),
Expand Down Expand Up @@ -150,7 +106,7 @@ Global Flags:
{
name: "script fails on unkown argument",
input: args("-p", "testdata/project", "run", "required_arg", "foo=bar", "--a b"),
stdoutput: usage,
stdoutput: "",
erroutput: `Error: unknown flag: --a b
`,
err: errors.New(`unknown flag: --a b`),
Expand Down
26 changes: 19 additions & 7 deletions pkg/executors/golang/cmder/cmder.go
Expand Up @@ -3,6 +3,7 @@ package cmder
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand All @@ -12,6 +13,10 @@ import (
"github.com/spf13/cobra"
)

var (
ErrNoHelp = errors.New("cmd failed with exit 1")
)

type RootCmd struct {
Cmds []*Cmd
}
Expand All @@ -30,7 +35,12 @@ func (rc *RootCmd) AddCmds(cmd ...*Cmd) *RootCmd {

func (rc *RootCmd) Execute() {
if err := rc.TryExecute(os.Args[1:]); err != nil {
log.Fatalf("%v\n", err)
if errors.Is(err, ErrNoHelp) {
os.Exit(1)
} else {
log.Fatalf("%v\n", err)
}

}
}

Expand All @@ -47,7 +57,7 @@ func (rc *RootCmd) TryExecute(args []string) error {
&cobra.Command{
Hidden: true,
Use: "lsjson",
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
actions := executer.NewActions()
for _, cmd := range rc.Cmds {
args := make([]executer.ActionArg, 0)
Expand All @@ -65,16 +75,16 @@ func (rc *RootCmd) TryExecute(args []string) error {

rawJson, err := json.Marshal(actions)
if err != nil {
return err
log.Fatal(err)
}

// Prints the commands and args as json to stdout
_, err = fmt.Printf("%s", string(rawJson))
if err != nil {
return err
log.Fatal(err)
}

return nil
return
},
},
)
Expand All @@ -87,7 +97,8 @@ func (rc *RootCmd) TryExecute(args []string) error {
Use: cmd.Name,
RunE: func(cobracmd *cobra.Command, args []string) error {
if err := cobracmd.ParseFlags(args); err != nil {
return err
log.Println(err)
return ErrNoHelp
}

inputs := make([]reflect.Value, 0, len(cmd.Args)+1)
Expand All @@ -108,7 +119,8 @@ 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 {
return err
log.Println(err)
return ErrNoHelp
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/executors/golang/cmder/cmder_test.go
Expand Up @@ -21,7 +21,7 @@ func TestCmderWithError(t *testing.T) {

err := cmder.NewRoot().AddCmds(testFunc).TryExecute(args)

assert.ErrorContains(t, err, "some-error")
assert.ErrorIs(t, err, cmder.ErrNoHelp)
}

func TestCmderWithNoError(t *testing.T) {
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestCmderWithMultipeReturnsErroring(t *testing.T) {

err := cmder.NewRoot().AddCmds(testFunc).TryExecute(args)

assert.ErrorContains(t, err, "some-error")
assert.ErrorIs(t, err, cmder.ErrNoHelp)
}

func TestCmderWithMultipeReturnsErroringInAnyPlace(t *testing.T) {
Expand All @@ -77,5 +77,5 @@ func TestCmderWithMultipeReturnsErroringInAnyPlace(t *testing.T) {

err := cmder.NewRoot().AddCmds(testFunc).TryExecute(args)

assert.ErrorContains(t, err, "some-error")
assert.ErrorIs(t, err, cmder.ErrNoHelp)
}

0 comments on commit 2167822

Please sign in to comment.