diff --git a/.golangci.yml b/.golangci.yml index 4aa9f33..9189dc6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -10,16 +10,6 @@ linters-settings: lines: 200 statements: 120 -linters: - disable: - - gochecknoinits - - gocyclo - - godox - - gocognit - - maligned - - dupl - - gochecknoglobals - issues: # List of regexps of issue texts to exclude, empty list by default. # But independently from this option we use default exclude patterns, diff --git a/cmd/envdef/envdef.go b/cmd/envdef/envdef.go index 4a4ab26..205935d 100644 --- a/cmd/envdef/envdef.go +++ b/cmd/envdef/envdef.go @@ -7,23 +7,27 @@ import ( "github.com/locona/envdef" ) -var ( - defaultSource = ".env.sample" - defaultDist = ".env" -) - func main() { + defaultSource := ".env.sample" + defaultDist := ".env" + var source string flag.StringVar(&source, "s", defaultSource, "source .env file") var dist string flag.StringVar(&dist, "d", defaultDist, "distribution .env file") + var overwrite bool + flag.BoolVar(&overwrite, "o", false, "Whether to overwrite when the key already exists.") + flag.Parse() - result, err := envdef.Diff(source, dist) + result, err := envdef.Diff(source, dist, overwrite) if err != nil { log.Fatal(err) } - result.Write() + err = result.Write() + if err != nil { + log.Fatal(err) + } result.Print() } diff --git a/envdef.go b/envdef.go index 16d72ee..4cef835 100644 --- a/envdef.go +++ b/envdef.go @@ -6,10 +6,7 @@ import ( "github.com/joho/godotenv" ) -var iconFormat = "%v %v" -var envFormat = "%v=%v" - -func Diff(source, dist string) (*Result, error) { +func Diff(source, dist string, overwrite bool) (*Result, error) { var ( insertSlice InsertSlice updateSlice UpdateSlice @@ -30,23 +27,27 @@ func Diff(source, dist string) (*Result, error) { for k, v := range sourceEnv { // update if _, ok := distEnv[k]; ok { - if v != distEnv[k] { - updateSlice = append(updateSlice, fmt.Sprintf(envFormat, k, v)) + if v == distEnv[k] { + // nochange + noChangeSlice = append(noChangeSlice, envFormat(k, v)) continue } - noChangeSlice = append(noChangeSlice, fmt.Sprintf(envFormat, k, v)) - + if overwrite { + updateSlice = append(updateSlice, envFormat(k, v)) + } else { + updateSlice = append(updateSlice, envFormat(k, distEnv[k])) + } continue } // insert - insertSlice = append(insertSlice, fmt.Sprintf(envFormat, k, v)) + insertSlice = append(insertSlice, envFormat(k, v)) } for k, v := range distEnv { if _, ok := sourceEnv[k]; !ok { - deleteSlice = append(deleteSlice, fmt.Sprintf(envFormat, k, v)) + deleteSlice = append(deleteSlice, envFormat(k, v)) } } @@ -58,6 +59,11 @@ func Diff(source, dist string) (*Result, error) { }, nil } +func envFormat(k, v string) string { + format := "%v=%v" + return fmt.Sprintf(format, k, v) +} + func Read(path string) (map[string]string, error) { e, err := godotenv.Read(path) if err != nil { diff --git a/envdef_test.go b/envdef_test.go index 2928aca..a561878 100644 --- a/envdef_test.go +++ b/envdef_test.go @@ -40,13 +40,15 @@ func TestRead(t *testing.T) { func TestDiff(t *testing.T) { testdata := []struct { - source string - dist string - expected Result + source string + dist string + overwrite bool + expected Result }{ { - source: "testdata/.env.sample", - dist: "testdata/.env", + source: "testdata/.env.sample", + dist: "testdata/.env", + overwrite: true, expected: Result{ InsertSlice: InsertSlice{"INSERT=insert"}, UpdateSlice: UpdateSlice{"UPDATE=default"}, @@ -54,10 +56,21 @@ func TestDiff(t *testing.T) { NoChangeSlice: NoChangeSlice{"NOCHANGE=nochange"}, }, }, + { + source: "testdata/.env.sample", + dist: "testdata/.env", + overwrite: false, + expected: Result{ + InsertSlice: InsertSlice{"INSERT=insert"}, + UpdateSlice: UpdateSlice{"UPDATE=update"}, + DeleteSlice: DeleteSlice{"DELETE=delete"}, + NoChangeSlice: NoChangeSlice{"NOCHANGE=nochange"}, + }, + }, } for _, d := range testdata { - res, _ := Diff(d.source, d.dist) + res, _ := Diff(d.source, d.dist, d.overwrite) // check len if len(d.expected.InsertSlice) != len(res.InsertSlice) { diff --git a/result.go b/result.go index 2578d3f..b34cb49 100644 --- a/result.go +++ b/result.go @@ -39,7 +39,7 @@ func (es InsertSlice) Print() { iconInsert := "+" for _, e := range es { - color.Yellow(fmt.Sprintf(iconFormat, iconInsert, e)) + color.Yellow(iconFormat(iconInsert, e)) } } @@ -49,7 +49,7 @@ func (es UpdateSlice) Print() { iconUpdate := "~" for _, e := range es { - color.Cyan(fmt.Sprintf(iconFormat, iconUpdate, e)) + color.Cyan(iconFormat(iconUpdate, e)) } } @@ -59,7 +59,7 @@ func (es DeleteSlice) Print() { iconDelete := "-" for _, e := range es { - color.Red(fmt.Sprintf(iconFormat, iconDelete, e)) + color.Red(iconFormat(iconDelete, e)) } } @@ -70,3 +70,8 @@ func (es NoChangeSlice) Print() { color.White(e) } } + +func iconFormat(icon, msg string) string { + iconFormat := "%v %v" + return fmt.Sprintf(iconFormat, icon, msg) +}