Skip to content

Commit

Permalink
Add long flag to diff command
Browse files Browse the repository at this point in the history
  • Loading branch information
stilvoid committed Jun 24, 2019
1 parent 671974e commit a3f7cdc
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 86 deletions.
4 changes: 2 additions & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ func getRainBucket() string {
return bucketName
}

func colouriseDiff(d diff.Diff) string {
func colouriseDiff(d diff.Diff, longFormat bool) string {
output := strings.Builder{}

for _, line := range strings.Split(diff.Format(d), "\n") {
for _, line := range strings.Split(diff.Format(d, longFormat), "\n") {
switch {
case strings.HasPrefix(line, diff.Added.String()):
output.WriteString(util.Green(line).String())
Expand Down
4 changes: 2 additions & 2 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ var deployCmd = &cobra.Command{

d := diff.Compare(oldTemplate, newTemplate)

if d == diff.Unchanged {
if d.Mode() == diff.Unchanged {
fmt.Println(util.Green("No changes to deploy!"))
return
}

util.ClearLine()
if util.Confirm(true, fmt.Sprintf("Stack '%s' exists. Do you wish to see the diff before deploying?", stackName)) {
fmt.Print(colouriseDiff(d))
fmt.Print(colouriseDiff(d, false))

if !util.Confirm(true, "Do you wish to continue?") {
panic(errors.New("User cancelled deployment."))
Expand Down
5 changes: 4 additions & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/spf13/cobra"
)

var longDiff = false

var diffCmd = &cobra.Command{
Use: "diff <from> <to>",
Short: "Compare CloudFormation templates",
Expand All @@ -27,10 +29,11 @@ var diffCmd = &cobra.Command{
panic(fmt.Errorf("Unable to parse template '%s': %s", leftFn, err))
}

fmt.Print(colouriseDiff(diff.Compare(left, right)))
fmt.Print(colouriseDiff(diff.Compare(left, right), longDiff))
},
}

func init() {
diffCmd.Flags().BoolVarP(&longDiff, "long", "l", false, "Include unchanged elements in diff output")
rootCmd.AddCommand(diffCmd)
}
6 changes: 3 additions & 3 deletions diff/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func Compare(old, new interface{}) Diff {
}
}

return Unchanged
return diffValue{old, Unchanged}
}

func compareSlices(old, new []interface{}) Diff {
if reflect.DeepEqual(old, new) {
return Unchanged
return diffValue{old, Unchanged}
}

max := int(math.Max(float64(len(old)), float64(len(new))))
Expand All @@ -47,7 +47,7 @@ func compareSlices(old, new []interface{}) Diff {

func compareMaps(old, new map[string]interface{}) Diff {
if reflect.DeepEqual(old, new) {
return Unchanged
return diffValue{old, Unchanged}
}

d := make(diffMap)
Expand Down
34 changes: 17 additions & 17 deletions diff/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestCompareScalar(t *testing.T) {
expected Diff
}{
{
"foo", "foo", Unchanged,
"foo", "foo", diffValue{"foo", Unchanged},
},
{
"foo", "bar", diffValue{"bar", Changed},
Expand Down Expand Up @@ -41,27 +41,27 @@ func TestCompareSlices(t *testing.T) {
expected Diff
}{
{
[]interface{}{1, 2, 3}, []interface{}{1, 2, 3}, Unchanged,
[]interface{}{1, 2, 3}, []interface{}{1, 2, 3}, diffValue{[]interface{}{1, 2, 3}, Unchanged},
},
{
[]interface{}{1, 2, 3}, []interface{}{1, 2, 4}, diffSlice{
Unchanged,
Unchanged,
diffValue{1, Unchanged},
diffValue{2, Unchanged},
diffValue{4, Changed},
},
},
{
[]interface{}{1, 2, 3}, []interface{}{1, 2, 3, 4}, diffSlice{
Unchanged,
Unchanged,
Unchanged,
diffValue{1, Unchanged},
diffValue{2, Unchanged},
diffValue{3, Unchanged},
diffValue{4, Added},
},
},
{
[]interface{}{1, 2, 3}, []interface{}{1, 2}, diffSlice{
Unchanged,
Unchanged,
diffValue{1, Unchanged},
diffValue{2, Unchanged},
diffValue{3, Removed},
},
},
Expand All @@ -85,7 +85,7 @@ func TestCompareMaps(t *testing.T) {
{
map[string]interface{}{"foo": "bar"},
map[string]interface{}{"foo": "bar"},
Unchanged,
diffValue{map[string]interface{}{"foo": "bar"}, Unchanged},
},
{
map[string]interface{}{"foo": "bar"},
Expand All @@ -95,7 +95,7 @@ func TestCompareMaps(t *testing.T) {
{
map[string]interface{}{"foo": "bar"},
map[string]interface{}{"foo": "bar", "baz": "quux"},
diffMap{"foo": Unchanged, "baz": diffValue{"quux", Added}},
diffMap{"foo": diffValue{"bar", Unchanged}, "baz": diffValue{"quux", Added}},
},
{
map[string]interface{}{"foo": "bar"},
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestCompare(t *testing.T) {
{
original,
original,
Unchanged,
diffValue{original, Unchanged},
},
{
original,
Expand All @@ -158,15 +158,15 @@ func TestCompare(t *testing.T) {
diffMap{
"foo": diffSlice{
diffMap{
"foo": Unchanged,
"foo": diffValue{"bar", Unchanged},
"baz": diffSlice{
Unchanged,
Unchanged,
diffValue{"foo", Unchanged},
diffValue{"bar", Unchanged},
diffValue{"baz", Added},
},
"quux": diffValue{"mooz", Added},
},
Unchanged,
diffValue{"foo", Unchanged},
diffValue{"bar", Added},
},
"bar": diffValue{"baz", Added},
Expand All @@ -188,7 +188,7 @@ func TestCompare(t *testing.T) {
diffMap{
"foo": diffValue{"bar", Removed},
"baz": diffSlice{
Unchanged,
diffValue{"foo", Unchanged},
diffValue{"bar", Removed},
},
},
Expand Down
20 changes: 8 additions & 12 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const (
Added mode = "+ "
Removed mode = "- "
Changed mode = "| "
Unchanged mode = "= "
Unchanged mode = " "
)

type Diff interface {
mode() mode
Mode() mode
}

type diffValue struct {
Expand All @@ -26,22 +26,18 @@ func (m mode) String() string {
return string(m)
}

func (m mode) mode() mode {
return m
}

func (d diffValue) mode() mode {
func (d diffValue) Mode() mode {
return d.valueMode
}

func (d diffSlice) mode() mode {
func (d diffSlice) Mode() mode {
mode := Added

for i, v := range d {
if i == 0 {
mode = v.mode()
mode = v.Mode()
} else {
if mode != v.mode() {
if mode != v.Mode() {
mode = Changed
}
}
Expand All @@ -50,14 +46,14 @@ func (d diffSlice) mode() mode {
return mode
}

func (d diffMap) mode() mode {
func (d diffMap) Mode() mode {
slice := make(diffSlice, 0)

for _, v := range d {
slice = append(slice, v)
}

return slice.mode()
return slice.Mode()
}

func (d diffMap) Keys() []string {
Expand Down
30 changes: 15 additions & 15 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,62 @@ func TestDiffMode(t *testing.T) {
},
{
diffSlice{
Added,
diffValue{"foo", Added},
},
Added,
},
{
diffSlice{
Added,
Added,
diffValue{"foo", Added},
diffValue{"bar", Added},
},
Added,
},
{
diffSlice{
Removed,
Removed,
diffValue{"foo", Removed},
diffValue{"bar", Removed},
},
Removed,
},
{
diffSlice{
Added,
Removed,
diffValue{"foo", Added},
diffValue{"bar", Removed},
},
Changed,
},
{
diffMap{
"foo": Added,
"foo": diffValue{"bar", Added},
},
Added,
},
{
diffMap{
"foo": Added,
"bar": Added,
"foo": diffValue{"bar", Added},
"baz": diffValue{"quux", Added},
},
Added,
},
{
diffMap{
"foo": Removed,
"bar": Removed,
"foo": diffValue{"bar", Removed},
"baz": diffValue{"quux", Removed},
},
Removed,
},
{
diffMap{
"foo": Added,
"bar": Removed,
"foo": diffValue{"bar", Added},
"baz": diffValue{"quux", Removed},
},
Changed,
},
}

for _, testCase := range cases {
actual := testCase.value.mode()
actual := testCase.value.Mode()

if actual != testCase.expected {
t.Errorf("%#v\n!=\n%#v", actual, testCase.expected)
Expand Down
Loading

0 comments on commit a3f7cdc

Please sign in to comment.