From ef4430c8eaa0107a6c0a4011b5e88259d790ee8e Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 18 May 2021 12:09:50 +0200 Subject: [PATCH] refactor: rename from flag to input also add an error when input is not specified --- pkg/cmd/gen_driftignore.go | 7 +++++- pkg/cmd/gen_driftignore_test.go | 42 ++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/pkg/cmd/gen_driftignore.go b/pkg/cmd/gen_driftignore.go index 1114d821..0e7656a6 100644 --- a/pkg/cmd/gen_driftignore.go +++ b/pkg/cmd/gen_driftignore.go @@ -6,6 +6,7 @@ import ( "os" "github.com/cloudskiff/driftctl/pkg/analyser" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -18,6 +19,10 @@ func NewGenDriftIgnoreCmd() *cobra.Command { Long: "This command will generate a new .driftignore file containing your current drifts and send output to /dev/stdout\n\nExample: driftctl scan -o json://stdout | driftctl gen-driftignore > .driftignore", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { + if opts.InputPath == "" { + return errors.New("Error: you need to specify an input to parse JSON from") + } + _, list, err := genDriftIgnore(opts) if err != nil { return err @@ -34,7 +39,7 @@ func NewGenDriftIgnoreCmd() *cobra.Command { fl.BoolVar(&opts.ExcludeUnmanaged, "exclude-unmanaged", false, "Exclude resources not managed by IaC") fl.BoolVar(&opts.ExcludeDeleted, "exclude-missing", false, "Exclude missing resources") fl.BoolVar(&opts.ExcludeDrifted, "exclude-changed", false, "Exclude resources that changed on cloud provider") - fl.StringVarP(&opts.InputPath, "from", "f", "/dev/stdin", "Input where the JSON should be parsed from") + fl.StringVarP(&opts.InputPath, "input", "i", "", "Input where the JSON should be parsed from") return cmd } diff --git a/pkg/cmd/gen_driftignore_test.go b/pkg/cmd/gen_driftignore_test.go index 4756ef0f..c8a9e18d 100644 --- a/pkg/cmd/gen_driftignore_test.go +++ b/pkg/cmd/gen_driftignore_test.go @@ -21,40 +21,46 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) { }{ { name: "test error on invalid input", - args: []string{"-f", "./testdata/input_stdin_invalid.json"}, + args: []string{"-i", "./testdata/input_stdin_invalid.json"}, output: "./testdata/output_stdin_empty.txt", err: errors.New("invalid character 'i' looking for beginning of value"), }, { name: "test empty driftignore with valid input", - args: []string{"-f", "./testdata/input_stdin_empty.json"}, + args: []string{"-i", "./testdata/input_stdin_empty.json"}, output: "./testdata/output_stdin_empty.txt", err: nil, }, { name: "test driftignore content with valid input", - args: []string{"-f", "./testdata/input_stdin_valid.json"}, + args: []string{"-i", "./testdata/input_stdin_valid.json"}, output: "./testdata/output_stdin_valid.txt", err: nil, }, { name: "test driftignore content with valid input and filter missing & changed only", - args: []string{"-f", "./testdata/input_stdin_valid.json", "--exclude-unmanaged"}, + args: []string{"-i", "./testdata/input_stdin_valid.json", "--exclude-unmanaged"}, output: "./testdata/output_stdin_valid_filter.txt", err: nil, }, { name: "test driftignore content with valid input and filter unmanaged only", - args: []string{"-f", "./testdata/input_stdin_valid.json", "--exclude-missing", "--exclude-changed"}, + args: []string{"-i", "./testdata/input_stdin_valid.json", "--exclude-missing", "--exclude-changed"}, output: "./testdata/output_stdin_valid_filter2.txt", err: nil, }, { name: "test error when input file does not exist", - args: []string{"-f", "doesnotexist"}, + args: []string{"-i", "doesnotexist"}, output: "./testdata/output_stdin_valid_filter2.txt", err: errors.New("open doesnotexist: no such file or directory"), }, + { + name: "test error when input flag is not specified", + args: []string{}, + output: "", + err: errors.New("Error: you need to specify an input to parse JSON from"), + }, } for _, c := range cases { @@ -62,18 +68,13 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) { rootCmd := &cobra.Command{Use: "root"} rootCmd.AddCommand(NewGenDriftIgnoreCmd()) - output, err := os.ReadFile(c.output) - if err != nil { - t.Fatal(err) - } - stdout := os.Stdout // keep backup of the real stdout r, w, _ := os.Pipe() os.Stdout = w args := append([]string{"gen-driftignore"}, c.args...) - _, err = test.Execute(rootCmd, args...) + _, err := test.Execute(rootCmd, args...) if c.err != nil { assert.EqualError(t, err, c.err.Error()) return @@ -94,7 +95,14 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) { os.Stdout = stdout // restoring the real stdout result := <-outC - assert.Equal(t, string(output), string(result)) + if c.output != "" { + output, err := os.ReadFile(c.output) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, string(output), string(result)) + } }) } } @@ -112,8 +120,8 @@ func TestGenDriftIgnoreCmd_ValidFlags(t *testing.T) { {args: []string{"gen-driftignore", "--exclude-missing"}}, {args: []string{"gen-driftignore", "--exclude-changed"}}, {args: []string{"gen-driftignore", "--exclude-changed=false", "--exclude-missing=false", "--exclude-unmanaged=true"}}, - {args: []string{"gen-driftignore", "--from", "/dev/stdin"}}, - {args: []string{"gen-driftignore", "-f", "/dev/stdout"}}, + {args: []string{"gen-driftignore", "--input", "/dev/stdin"}}, + {args: []string{"gen-driftignore", "-i", "/dev/stdout"}}, } for _, tt := range cases { @@ -141,8 +149,8 @@ func TestGenDriftIgnoreCmd_InvalidFlags(t *testing.T) { {args: []string{"gen-driftignore", "--drifted"}, err: errors.New("unknown flag: --drifted")}, {args: []string{"gen-driftignore", "--changed"}, err: errors.New("unknown flag: --changed")}, {args: []string{"gen-driftignore", "--missing"}, err: errors.New("unknown flag: --missing")}, - {args: []string{"gen-driftignore", "--from"}, err: errors.New("flag needs an argument: --from")}, - {args: []string{"gen-driftignore", "-f"}, err: errors.New("flag needs an argument: 'f' in -f")}, + {args: []string{"gen-driftignore", "--input"}, err: errors.New("flag needs an argument: --input")}, + {args: []string{"gen-driftignore", "-i"}, err: errors.New("flag needs an argument: 'i' in -i")}, } for _, tt := range cases {