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
ExcludeDrifted bool
InputPath string
OutputPath string
}
func (a Analysis) MarshalJSON() ([]byte, error) {

View File

@ -5,8 +5,10 @@ import (
"fmt"
"io"
"os"
"time"
"github.com/cloudskiff/driftctl/pkg/analyser"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -16,25 +18,25 @@ func NewGenDriftIgnoreCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "gen-driftignore",
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,
RunE: func(cmd *cobra.Command, args []string) error {
driftFile := os.Stdin
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)
_, list, err := genDriftIgnore(opts)
if err != nil {
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
},
@ -46,12 +48,23 @@ func NewGenDriftIgnoreCmd() *cobra.Command {
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, "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
}
func genDriftIgnore(opts *analyser.GenDriftIgnoreOptions, drift *os.File) (int, string, error) {
input, err := io.ReadAll(drift)
func genDriftIgnore(opts *analyser.GenDriftIgnoreOptions) (int, string, error) {
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 {
return 0, "", err
}

View File

@ -1,15 +1,15 @@
package cmd
import (
"bytes"
"errors"
"io"
"os"
"strings"
"testing"
"github.com/cloudskiff/driftctl/test"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGenDriftIgnoreCmd_Input(t *testing.T) {
@ -62,13 +62,16 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) {
rootCmd := &cobra.Command{Use: "root"}
rootCmd.AddCommand(NewGenDriftIgnoreCmd())
stdout := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
f, err := os.CreateTemp("", "TestGenDriftIgnoreCmd_Input")
require.Nil(t, err)
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 {
assert.EqualError(t, err, c.err.Error())
return
@ -76,26 +79,13 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) {
assert.Equal(t, c.err, err)
}
outC := make(chan []byte)
// copy the output in a separate goroutine so printing can't block indefinitely
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
output, err := os.ReadFile(f.Name())
require.Nil(t, err)
if c.output != "" {
output, err := os.ReadFile(c.output)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(output), string(result))
expectedOutput, err := os.ReadFile(c.output)
require.Nil(t, err)
assert.Equal(t, string(expectedOutput), trimLeadingComment(string(output)))
}
})
}
@ -110,6 +100,7 @@ func TestGenDriftIgnoreCmd_ValidFlags(t *testing.T) {
cases := []struct {
args []string
}{
{args: []string{"gen-driftignore"}},
{args: []string{"gen-driftignore", "--exclude-unmanaged"}},
{args: []string{"gen-driftignore", "--exclude-missing"}},
{args: []string{"gen-driftignore", "--exclude-changed"}},
@ -152,3 +143,10 @@ func TestGenDriftIgnoreCmd_InvalidFlags(t *testing.T) {
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")
}