refactor: rename from flag to input
also add an error when input is not specifiedmain
parent
fe3d0a14ea
commit
ef4430c8ea
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue