Skip to content

Commit

Permalink
improvement: No print updated environment, when overwrite is false. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
locona committed Mar 17, 2020
1 parent ba9a6cd commit bc9f1d5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 50 deletions.
20 changes: 8 additions & 12 deletions envdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
)

func Diff(source, dist string, overwrite bool) (*Result, error) {
var (
insertSlice InsertSlice
updateSlice UpdateSlice
deleteSlice DeleteSlice
noChangeSlice NoChangeSlice
)
insertSlice := InsertSlice{}
updateSlice := UpdateSlice{}
deleteSlice := DeleteSlice{}
noChangeSlice := NoChangeSlice{}

sourceEnv, err := Read(source)
if err != nil {
Expand All @@ -27,18 +25,16 @@ func Diff(source, dist string, overwrite bool) (*Result, error) {
for k, v := range sourceEnv {
// update
if _, ok := distEnv[k]; ok {
if v == distEnv[k] {
// nochange
// nochange
if !overwrite || v == distEnv[k] {
noChangeSlice = append(noChangeSlice, envFormat(k, v))
continue
}

if overwrite {
if v != distEnv[k] {
updateSlice = append(updateSlice, envFormat(k, v))
} else {
updateSlice = append(updateSlice, envFormat(k, distEnv[k]))
continue
}
continue
}

// insert
Expand Down
43 changes: 8 additions & 35 deletions envdef_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package envdef

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRead(t *testing.T) {
Expand Down Expand Up @@ -62,47 +63,19 @@ func TestDiff(t *testing.T) {
overwrite: false,
expected: Result{
InsertSlice: InsertSlice{"INSERT=insert"},
UpdateSlice: UpdateSlice{"UPDATE=update"},
UpdateSlice: UpdateSlice{},
DeleteSlice: DeleteSlice{"DELETE=delete"},
NoChangeSlice: NoChangeSlice{"NOCHANGE=nochange"},
NoChangeSlice: NoChangeSlice{"UPDATE=default", "NOCHANGE=nochange"},
},
},
}

for _, d := range testdata {
res, _ := Diff(d.source, d.dist, d.overwrite)
// check len

if len(d.expected.InsertSlice) != len(res.InsertSlice) {
t.Errorf("expected len(%v), got len(%v)", len(d.expected.InsertSlice), len(res.InsertSlice))
}

if len(d.expected.UpdateSlice) != len(res.UpdateSlice) {
t.Errorf("expected len(%v), got len(%v)", len(d.expected.UpdateSlice), len(res.UpdateSlice))
}

if len(d.expected.DeleteSlice) != len(res.DeleteSlice) {
t.Errorf("expected len(%v), got len(%v)", len(d.expected.DeleteSlice), len(res.DeleteSlice))
}

if len(d.expected.NoChangeSlice) != len(res.NoChangeSlice) {
t.Errorf("expected len(%v), got len(%v)", len(d.expected.NoChangeSlice), len(res.NoChangeSlice))
}

if !reflect.DeepEqual(d.expected.InsertSlice, res.InsertSlice) {
t.Errorf("expected %s, got %s", d.expected.InsertSlice, res.InsertSlice)
}

if !reflect.DeepEqual(d.expected.UpdateSlice, res.UpdateSlice) {
t.Errorf("expected %s, got %s", d.expected.UpdateSlice, res.UpdateSlice)
}

if !reflect.DeepEqual(d.expected.DeleteSlice, res.DeleteSlice) {
t.Errorf("expected %s, got %s", d.expected.DeleteSlice, res.DeleteSlice)
}

if !reflect.DeepEqual(d.expected.NoChangeSlice, res.NoChangeSlice) {
t.Errorf("expected %s, got %s", d.expected.NoChangeSlice, res.NoChangeSlice)
}
assert.ElementsMatch(t, d.expected.InsertSlice, res.InsertSlice)
assert.ElementsMatch(t, d.expected.UpdateSlice, res.UpdateSlice)
assert.ElementsMatch(t, d.expected.DeleteSlice, res.DeleteSlice)
assert.ElementsMatch(t, d.expected.NoChangeSlice, res.NoChangeSlice)
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ require (
github.com/fatih/color v1.9.0
github.com/joho/godotenv v1.3.0
github.com/mattn/go-colorable v0.1.6 // indirect
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
github.com/stretchr/testify v1.5.1
golang.org/x/sys v0.0.0-20200316230553-a7d97aace0b0 // indirect
)
16 changes: 14 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
Expand All @@ -9,10 +11,20 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200316230553-a7d97aace0b0 h1:4Khi5GeNOkZS5DqSBRn4Sy7BE6GuxwOqARPqfurkdNk=
golang.org/x/sys v0.0.0-20200316230553-a7d97aace0b0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit bc9f1d5

Please sign in to comment.