Skip to content

Commit

Permalink
improvement: Add overwrite option, whether to overwrite when the k… (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
locona committed Mar 17, 2020
1 parent 81438af commit ef9ead9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 36 deletions.
10 changes: 0 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 11 additions & 7 deletions cmd/envdef/envdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
26 changes: 16 additions & 10 deletions envdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}

Expand All @@ -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 {
Expand Down
25 changes: 19 additions & 6 deletions envdef_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,37 @@ 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"},
DeleteSlice: DeleteSlice{"DELETE=delete"},
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) {
Expand Down
11 changes: 8 additions & 3 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand All @@ -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))
}
}

Expand All @@ -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))
}
}

Expand All @@ -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)
}

0 comments on commit ef9ead9

Please sign in to comment.