Repository: aws-cloudformation/rain
Pull request #21 (2020-05-27 13:31:33)

Fix quoted strings as per #15
*Issue #, if available:*

*Description of changes:*


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.



Patch:
diff --git a/cfn/format/common.go b/cfn/format/common.go
index fd5d59b5..55142460 100644
--- a/cfn/format/common.go
+++ b/cfn/format/common.go
@@ -22,6 +22,8 @@ func intrinsicKey(data map[string]interface{}) (string, bool) {
 
 func formatString(data string) string {
 	switch {
+	case strings.HasPrefix(data, "\n"), strings.HasSuffix(data, "\n"):
+		return fmt.Sprintf("%q", data)
 	case strings.ContainsAny(data, "\n"):
 		parts := strings.Split(strings.TrimSpace(data), "\n")
 		for i, part := range parts {
diff --git a/cmd/ls.go b/cmd/ls.go
index 51f8ca38..fcc5ec87 100644
--- a/cmd/ls.go
+++ b/cmd/ls.go
@@ -3,7 +3,6 @@ package cmd
 import (
 	"fmt"
 	"sort"
-	"strings"
 
 	"github.com/aws-cloudformation/rain/client"
 	"github.com/aws-cloudformation/rain/client/cfn"
@@ -15,27 +14,6 @@ import (
 )
 
 var allRegions = false
-var showNested = false
-
-func formatStack(stack cloudformation.StackSummary, stackMap map[string]cloudformation.StackSummary) string {
-	out := strings.Builder{}
-
-	out.WriteString(fmt.Sprintf("%s: %s\n",
-		*stack.StackName,
-		colouriseStatus(string(stack.StackStatus)),
-	))
-
-	if showNested {
-		for _, otherStack := range stackMap {
-			if otherStack.ParentId != nil && *otherStack.ParentId == *stack.StackId {
-				out.WriteString(indent("- ", formatStack(otherStack, stackMap)))
-				out.WriteString("\n")
-			}
-		}
-	}
-
-	return out.String()
-}
 
 var lsCmd = &cobra.Command{
 	Use:                   "ls <stack>",
@@ -86,7 +64,6 @@ var lsCmd = &cobra.Command{
 				for _, stack := range stacks {
 					stackNames = append(stackNames, *stack.StackName)
 					stackMap[*stack.StackName] = stack
-					fmt.Println(stack)
 				}
 				sort.Strings(stackNames)
 
@@ -94,11 +71,10 @@ var lsCmd = &cobra.Command{
 
 				fmt.Println(text.Yellow(fmt.Sprintf("CloudFormation stacks in %s:", region)))
 				for _, stackName := range stackNames {
-					stack := stackMap[stackName]
-
-					if stack.ParentId == nil {
-						fmt.Println(indent("  ", formatStack(stack, stackMap)))
-					}
+					fmt.Printf("  %s: %s\n",
+						stackName,
+						colouriseStatus(string(stackMap[stackName].StackStatus)),
+					)
 				}
 				fmt.Println()
 			}
@@ -108,6 +84,5 @@ var lsCmd = &cobra.Command{
 
 func init() {
 	lsCmd.Flags().BoolVarP(&allRegions, "all", "a", false, "List stacks across all regions")
-	lsCmd.Flags().BoolVarP(&showNested, "nested", "n", false, "Show nested stacks (hidden by default)")
 	Root.AddCommand(lsCmd)
 }
diff --git a/cmd/util.go b/cmd/util.go
index 34bac137..6e81774f 100644
--- a/cmd/util.go
+++ b/cmd/util.go
@@ -19,10 +19,6 @@ import (
 	"github.com/aws/aws-sdk-go-v2/service/cloudformation"
 )
 
-func indent(prefix string, in string) string {
-	return prefix + strings.Join(strings.Split(strings.TrimSpace(in), "\n"), "\n"+prefix)
-}
-
 func colouriseStatus(status string) text.Text {
 	switch {
 	case strings.HasSuffix(status, "_FAILED"):


Test patch:
diff --git a/cfn/format/common_test.go b/cfn/format/common_test.go
index db8f4840..d5554106 100644
--- a/cfn/format/common_test.go
+++ b/cfn/format/common_test.go
@@ -43,12 +43,16 @@ func TestFormatString(t *testing.T) {
 		"foo",
 		"\"quoted\"",
 		"multi\nline",
+		"\nstarts with a newline",
+		"ends with a newline\n",
 	}
 
 	expecteds := []string{
 		"foo",
 		"\"\\\"quoted\\\"\"",
 		"|\n  multi\n  line",
+		"\"\\nstarts with a newline\"",
+		"\"ends with a newline\\n\"",
 	}
 
 	for i, testCase := range cases {
diff --git a/cmd/ls_test.go b/cmd/ls_test.go
index f259a8cb..f2b4c5c0 100644
--- a/cmd/ls_test.go
+++ b/cmd/ls_test.go
@@ -24,9 +24,8 @@ func Example_ls_help() {
 	//   ls, list
 	//
 	// Flags:
-	//   -a, --all      List stacks across all regions
-	//   -h, --help     help for ls
-	//   -n, --nested   Show nested stacks (hidden by default)
+	//   -a, --all    List stacks across all regions
+	//   -h, --help   help for ls
 	//
 	// Global Flags:
 	//       --debug            Output debugging information
diff --git a/cmd/util_test.go b/cmd/util_test.go
index 78017ae5..d048c4cd 100644
--- a/cmd/util_test.go
+++ b/cmd/util_test.go
@@ -5,8 +5,6 @@ import (
 	"strings"
 	"testing"
 
-	"github.com/google/go-cmp/cmp"
-
 	"github.com/aws-cloudformation/rain/cfn/diff"
 	"github.com/aws-cloudformation/rain/console"
 	"github.com/aws-cloudformation/rain/console/text"
@@ -99,22 +97,3 @@ func TestStatusIsSettled(t *testing.T) {
 		}
 	}
 }
-
-func TestIndent(t *testing.T) {
-	input := `This
-has
-multiple
-lines
-`
-
-	expected := `  This
-  has
-  multiple
-  lines` // Should chomp ending blank lines
-
-	actual := indent("  ", input)
-
-	if d := cmp.Diff(actual, expected); d != "" {
-		t.Errorf(d)
-	}
-}