From eef046f0ff0d5f1e2e078671879e21984d833a52 Mon Sep 17 00:00:00 2001 From: Gonzalo Peci Date: Wed, 15 May 2019 15:12:10 +0200 Subject: [PATCH 1/2] Support stdin and stdout from piped process Only format the content between and output the rest, additionally allowing the user to pass input more clearly. --- pkg/cmd/cmd.go | 75 +++++++++++++++++++++++++++++++------------------ test/main.tf | 23 +++++++++++++++ test/subproc.sh | 5 ++++ test/wrapper.sh | 12 ++++++++ 4 files changed, 87 insertions(+), 28 deletions(-) create mode 100644 test/main.tf create mode 100755 test/subproc.sh create mode 100755 test/wrapper.sh diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index b317db5..be9aeb3 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -38,9 +38,32 @@ func Execute(version string) { } } +func printPlan(input string) { + // If we have input + if len(input) > 0 { + plan, err := parser.Parse(input) + if err != nil { + if err == parser.ErrParseFailure { + os.Stderr.WriteString(color.RedString("Failed to parse plan. Returning original input.\n")) // nolint: gosec + fmt.Println(input) + return + } + } + + // plan will be nil if the parser panicked (potentially due to unrecognized + // character or sequences) so we return the original input. + if plan == nil { + os.Stderr.WriteString(color.RedString("Failed to parse plan. Returning original input.\n")) // nolint: gosec + fmt.Println(input) + return + } + + printer.PrettyPrint(plan) + } +} + func runScenery(cmd *cobra.Command, args []string) { var input string - var foundInput bool if noColor { color.NoColor = true @@ -51,13 +74,28 @@ func runScenery(cmd *cobra.Command, args []string) { if (stat.Mode() & os.ModeCharDevice) == 0 { var lines []string scanner := bufio.NewScanner(os.Stdin) + insidePlanBlock := false + + // Loop through input until separator for scanner.Scan() { - lines = append(lines, scanner.Text()) + text := scanner.Text() + if strings.Contains(text, "------------------------------------------------------------------------") { + if insidePlanBlock { + // If we detect it again, its the end of the plan block + insidePlanBlock = false + input = strings.Join(lines, "\n") + printPlan(input) + } else { + // Otherwise we are entering the plan block + insidePlanBlock = true + } + fmt.Println(text) + } else if insidePlanBlock { + lines = append(lines, text) + } else { + fmt.Println(text) + } } - - input = strings.Join(lines, "\n") - foundInput = true - } else if len(args) == 1 { fileContents, err := ioutil.ReadFile(args[0]) if err != nil { @@ -66,29 +104,10 @@ func runScenery(cmd *cobra.Command, args []string) { } input = string(fileContents) - foundInput = true - } - - if foundInput { - plan, err := parser.Parse(input) - if err != nil { - if err == parser.ErrParseFailure { - os.Stderr.WriteString(color.RedString("Failed to parse plan. Returning original input.\n")) // nolint: gosec - fmt.Println(input) - return - } - } - - // plan will be nil if the parser panicked (potentially due to unrecognized - // character or sequences) so we return the original input. - if plan == nil { - os.Stderr.WriteString(color.RedString("Failed to parse plan. Returning original input.\n")) // nolint: gosec - fmt.Println(input) - return - } - - printer.PrettyPrint(plan) } else { + // If no stdin or arguments, print usage cmd.Usage() // nolint: gosec + os.Exit(0) } + } diff --git a/test/main.tf b/test/main.tf new file mode 100644 index 0000000..6dc554d --- /dev/null +++ b/test/main.tf @@ -0,0 +1,23 @@ +provider "aws" {} + +data "aws_iam_policy_document" "write" { + statement { + sid = "ECRGetAuthorizationToken" + effect = "Allow" + + actions = [ + "ecr:InitiateLayerUpload", + "ecr:UploadLayer*", + "ecr:CompleteLayerUpload", + "ecr:PutImage", + ] + + resources = ["*"] + } +} + +resource "aws_iam_policy" "write" { + name = "testpolicy-deleteme" + description = "Allow IAM Users to pull from ECR" + policy = "${data.aws_iam_policy_document.write.json}" +} diff --git a/test/subproc.sh b/test/subproc.sh new file mode 100755 index 0000000..1602b0f --- /dev/null +++ b/test/subproc.sh @@ -0,0 +1,5 @@ +#/bin/bash + +echo "Enter some text:" +read -r input +echo "you typed: $input" diff --git a/test/wrapper.sh b/test/wrapper.sh new file mode 100755 index 0000000..9093cb1 --- /dev/null +++ b/test/wrapper.sh @@ -0,0 +1,12 @@ +#/bin/bash + +set -x + +pushd test +trap popd EXIT + +echo "asdasd" + +echo "!23123" + +terraform plan -input=true -lock=false From f9e85ad7a4707d9bfc907b321668e7ab6cc7c16b Mon Sep 17 00:00:00 2001 From: Gonzalo Peci Date: Thu, 6 Jun 2019 12:55:41 +0200 Subject: [PATCH 2/2] Fix ineffassign when reading plans from file --- pkg/cmd/cmd.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index be9aeb3..beaf004 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -104,6 +104,7 @@ func runScenery(cmd *cobra.Command, args []string) { } input = string(fileContents) + printPlan(input) } else { // If no stdin or arguments, print usage cmd.Usage() // nolint: gosec