Repository: aws-cloudformation/rain
Pull request #22 (2020-05-27 13:45:54)

Add a variant without CGO to the build script as per #17
*Issue #, if available:* #17

*Description of changes:* Add `nocgo` build variant


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/build.sh b/build.sh
index 75e1f493..3dbea653 100755
--- a/build.sh
+++ b/build.sh
@@ -11,6 +11,7 @@ VERSION=$(git describe)
 
 declare -A PLATFORMS=([linux]=linux [darwin]=osx [windows]=windows)
 declare -A ARCHITECTURES=([386]=i386 [amd64]=amd64)
+declare -A VARIANTS=([default]="" [nocgo]="CGO_ENABLED=0")
 
 # Run tests first
 golint ./... || exit 1
@@ -21,22 +22,29 @@ echo "Building $NAME $VERSION..."
 
 for platform in ${!PLATFORMS[@]}; do
     for architecture in ${!ARCHITECTURES[@]}; do
-        echo "$platform/$architecture..."
+        for variant in ${!VARIANTS[@]}; do
+            echo "$platform/$architecture/$variant..."
 
-        full_name="${NAME}-${VERSION}_${PLATFORMS[$platform]}-${ARCHITECTURES[$architecture]}"
-        bin_name="$NAME"
+            full_name="${NAME}-${VERSION}_${PLATFORMS[$platform]}-${ARCHITECTURES[$architecture]}"
+            bin_name="$NAME"
 
-        if [ "$platform" == "windows" ]; then
-            bin_name="${NAME}.exe"
-        fi
+            if [ "$variant" != "default" ]; then
+                full_name+="-$variant"
+            fi
 
-        mkdir -p "$OUTPUT_DIR/$full_name"
+            if [ "$platform" == "windows" ]; then
+                bin_name="${NAME}.exe"
+            fi
 
-        GOOS=$platform GOARCH=$architecture go build -o "$OUTPUT_DIR/${full_name}/${bin_name}"
+            mkdir -p "$OUTPUT_DIR/$full_name"
 
-        zip -9 -r "$OUTPUT_DIR/${full_name}.zip" "$OUTPUT_DIR/$full_name"
+            eval GOOS=$platform GOARCH=$architecture ${VARIANTS[$variant]} go build -o "$OUTPUT_DIR/${full_name}/${bin_name}"
 
-        rm -r "$OUTPUT_DIR/$full_name"
+            cd "$OUTPUT_DIR"
+            zip -9 -r "${full_name}.zip" "$full_name"
+            rm -r "$full_name"
+            cd -
+        done
     done
 done
 
diff --git a/cfn/format/common.go b/cfn/format/common.go
index 55142460..fd5d59b5 100644
--- a/cfn/format/common.go
+++ b/cfn/format/common.go
@@ -22,8 +22,6 @@ 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 d5554106..db8f4840 100644
--- a/cfn/format/common_test.go
+++ b/cfn/format/common_test.go
@@ -43,16 +43,12 @@ 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)
-	}
-}