Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Support stdin and stdout from piped process #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 48 additions & 28 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -66,29 +104,11 @@ 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)
printPlan(input)
} else {
// If no stdin or arguments, print usage
cmd.Usage() // nolint: gosec
os.Exit(0)
}

}
23 changes: 23 additions & 0 deletions test/main.tf
Original file line number Diff line number Diff line change
@@ -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}"
}
5 changes: 5 additions & 0 deletions test/subproc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#/bin/bash

echo "Enter some text:"
read -r input
echo "you typed: $input"
12 changes: 12 additions & 0 deletions test/wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#/bin/bash

set -x

pushd test
trap popd EXIT

echo "asdasd"

echo "!23123"

terraform plan -input=true -lock=false