gen-driftignore defaults to writing to a file

Users can still print to stdout by passing `-o -`.
main
Craig Furman 2021-11-08 10:43:18 +00:00
parent 4466a0430e
commit 612118d646
3 changed files with 52 additions and 40 deletions

View File

@ -69,6 +69,7 @@ type GenDriftIgnoreOptions struct {
ExcludeDeleted bool ExcludeDeleted bool
ExcludeDrifted bool ExcludeDrifted bool
InputPath string InputPath string
OutputPath string
} }
func (a Analysis) MarshalJSON() ([]byte, error) { func (a Analysis) MarshalJSON() ([]byte, error) {

View File

@ -5,8 +5,10 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"time"
"github.com/cloudskiff/driftctl/pkg/analyser" "github.com/cloudskiff/driftctl/pkg/analyser"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -16,25 +18,25 @@ func NewGenDriftIgnoreCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "gen-driftignore", Use: "gen-driftignore",
Short: "Generate a .driftignore file based on your scan result", Short: "Generate a .driftignore file based on your scan result",
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", Long: "This command will generate a new .driftignore file containing your current drifts\n\nExample: driftctl scan -o json://stdout | driftctl gen-driftignore",
Args: cobra.NoArgs, Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
driftFile := os.Stdin _, list, err := genDriftIgnore(opts)
if opts.InputPath != "-" {
var err error
driftFile, err = os.Open(opts.InputPath)
if err != nil {
return err
}
defer driftFile.Close()
}
_, list, err := genDriftIgnore(opts, driftFile)
if err != nil { if err != nil {
return err return err
} }
fmt.Println(list) ignoreFile := os.Stdout
if opts.OutputPath != "-" {
var err error
ignoreFile, err = os.OpenFile(opts.OutputPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return errors.Errorf("error opening output file: %s", err)
}
defer ignoreFile.Close()
fmt.Fprintf(os.Stderr, "Appending ignore rules to %s\n", opts.OutputPath)
}
fmt.Fprintf(ignoreFile, "# Generated by gen-driftignore cmd @ %s\n%s\n", time.Now().Format(time.RFC1123), list)
return nil return nil
}, },
@ -46,12 +48,23 @@ func NewGenDriftIgnoreCmd() *cobra.Command {
fl.BoolVar(&opts.ExcludeDeleted, "exclude-missing", false, "Exclude missing resources") 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.BoolVar(&opts.ExcludeDrifted, "exclude-changed", false, "Exclude resources that changed on cloud provider")
fl.StringVarP(&opts.InputPath, "input", "i", "-", "Input where the JSON should be parsed from. Defaults to stdin.") fl.StringVarP(&opts.InputPath, "input", "i", "-", "Input where the JSON should be parsed from. Defaults to stdin.")
fl.StringVarP(&opts.OutputPath, "output", "o", ".driftignore", "Output file path to write the driftignore to.")
return cmd return cmd
} }
func genDriftIgnore(opts *analyser.GenDriftIgnoreOptions, drift *os.File) (int, string, error) { func genDriftIgnore(opts *analyser.GenDriftIgnoreOptions) (int, string, error) {
input, err := io.ReadAll(drift) driftFile := os.Stdin
if opts.InputPath != "-" {
var err error
driftFile, err = os.Open(opts.InputPath)
if err != nil {
return 0, "", err
}
defer driftFile.Close()
}
input, err := io.ReadAll(driftFile)
if err != nil { if err != nil {
return 0, "", err return 0, "", err
} }

View File

@ -1,15 +1,15 @@
package cmd package cmd
import ( import (
"bytes"
"errors" "errors"
"io"
"os" "os"
"strings"
"testing" "testing"
"github.com/cloudskiff/driftctl/test" "github.com/cloudskiff/driftctl/test"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestGenDriftIgnoreCmd_Input(t *testing.T) { func TestGenDriftIgnoreCmd_Input(t *testing.T) {
@ -62,13 +62,16 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) {
rootCmd := &cobra.Command{Use: "root"} rootCmd := &cobra.Command{Use: "root"}
rootCmd.AddCommand(NewGenDriftIgnoreCmd()) rootCmd.AddCommand(NewGenDriftIgnoreCmd())
stdout := os.Stdout // keep backup of the real stdout f, err := os.CreateTemp("", "TestGenDriftIgnoreCmd_Input")
r, w, _ := os.Pipe() require.Nil(t, err)
os.Stdout = w defer func() {
f.Close()
os.Remove(f.Name())
}()
args := append([]string{"gen-driftignore"}, c.args...) args := append([]string{"gen-driftignore", "-o", f.Name()}, c.args...)
_, err := test.Execute(rootCmd, args...) _, err = test.Execute(rootCmd, args...)
if c.err != nil { if c.err != nil {
assert.EqualError(t, err, c.err.Error()) assert.EqualError(t, err, c.err.Error())
return return
@ -76,26 +79,13 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) {
assert.Equal(t, c.err, err) assert.Equal(t, c.err, err)
} }
outC := make(chan []byte) output, err := os.ReadFile(f.Name())
// copy the output in a separate goroutine so printing can't block indefinitely require.Nil(t, err)
go func() {
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
outC <- buf.Bytes()
}()
// back to normal state
w.Close()
os.Stdout = stdout // restoring the real stdout
result := <-outC
if c.output != "" { if c.output != "" {
output, err := os.ReadFile(c.output) expectedOutput, err := os.ReadFile(c.output)
if err != nil { require.Nil(t, err)
t.Fatal(err) assert.Equal(t, string(expectedOutput), trimLeadingComment(string(output)))
}
assert.Equal(t, string(output), string(result))
} }
}) })
} }
@ -110,6 +100,7 @@ func TestGenDriftIgnoreCmd_ValidFlags(t *testing.T) {
cases := []struct { cases := []struct {
args []string args []string
}{ }{
{args: []string{"gen-driftignore"}},
{args: []string{"gen-driftignore", "--exclude-unmanaged"}}, {args: []string{"gen-driftignore", "--exclude-unmanaged"}},
{args: []string{"gen-driftignore", "--exclude-missing"}}, {args: []string{"gen-driftignore", "--exclude-missing"}},
{args: []string{"gen-driftignore", "--exclude-changed"}}, {args: []string{"gen-driftignore", "--exclude-changed"}},
@ -152,3 +143,10 @@ func TestGenDriftIgnoreCmd_InvalidFlags(t *testing.T) {
assert.EqualError(t, err, tt.err.Error()) assert.EqualError(t, err, tt.err.Error())
} }
} }
// The leading comment, "Generated by gen-driftignore..." contains a timestamp,
// that we don't care to assert on.
func trimLeadingComment(content string) string {
lines := strings.Split(content, "\n")
return strings.Join(lines[1:], "\n")
}