Skip to content

Commit

Permalink
multiline: better handle yaml and other text that comes with +/- (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-knurek authored Oct 6, 2020
1 parent 3318721 commit e5ed0a2
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
15 changes: 15 additions & 0 deletions example/example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@
+ node_name = (known after apply)
restart_policy = "Always"
+ service_account_name = (known after apply)

# local_file.foo will be created
+ resource "local_file" "foo" {
+ content = <<~EOT
can:
enter:
- yaml
- or
+ anything
EOT
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./foo.out"
+ id = (known after apply)
}
32 changes: 29 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@ import (
)

func main() {
iterateInput(os.Stdin)
iterateInput(os.Stdin, os.Stdout)
}

func iterateInput(input io.Reader) {
func iterateInput(input io.Reader, out io.Writer) {
skipLines := false
key := ""
scanner := bufio.NewScanner(input)
for scanner.Scan() {
ln := scanner.Text()
processLine(ln, os.Stdout)
if strings.TrimSpace(ln) == key {
skipLines = false
}
if skipLines {
fmt.Fprintln(out, ln)
} else {
processLine(ln, out)
}
if strings.Contains(ln, "<<~") {
skipLines = true
key = after(ln, "<<~")
}
}
return
}
Expand Down Expand Up @@ -83,3 +96,16 @@ func cleanRawInput(raw string) string {
}
return nocolor
}

// Get substring after a string.
func after(value string, a string) string {
pos := strings.LastIndex(value, a)
if pos == -1 {
return ""
}
adjustedPos := pos + len(a)
if adjustedPos >= len(value) {
return ""
}
return value[adjustedPos:]
}
78 changes: 78 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,81 @@ func Test_cleanRawInput(t *testing.T) {
})
}
}

func Test_iterateInput(t *testing.T) {
tests := []struct {
name string
input string
out *bytes.Buffer
wantOut string
}{
{
name: "empty",
input: "",
out: &bytes.Buffer{},
wantOut: ``,
},
{
name: "example",
input: `
# module.apps.kubernetes_deployment.deployment is tainted, so must be replaced
-/+ resource "kubernetes_deployment" "deployment" {
~ id = "some-app" -> (known after apply)
wait_for_rollout = true
~ spec {
- active_deadline_seconds = 0 -> null
`,
out: &bytes.Buffer{},
wantOut: `
# module.apps.kubernetes_deployment.deployment is tainted, so must be replaced
-/+ resource "kubernetes_deployment" "deployment" {
~ id = "some-app" -> (known after apply)
wait_for_rollout = true
~ spec {
- active_deadline_seconds = 0 -> null
`,
},
{
name: "multiline example",
input: `
# local_file.foo will be created
+ resource "local_file" "foo" {
+ content = <<~EOT
can:
enter:
- yaml
- or
+ anything
EOT
+ directory_permission = "0777"
`,
out: &bytes.Buffer{},
wantOut: `
# local_file.foo will be created
+ resource "local_file" "foo" {
+ content = <<~EOT
can:
enter:
- yaml
- or
+ anything
EOT
+ directory_permission = "0777"
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var stdin bytes.Buffer
stdin.Write([]byte(tt.input))
iterateInput(&stdin, tt.out)
got := tt.out.String()
if got != tt.wantOut {
t.Errorf("output for %s is now aligned. \nGot: %v, \nwant: %v", tt.name, got, tt.wantOut)
t.Errorf("length \nGot: %v, \nwant: %v", len(got), len(tt.wantOut))
}
})
}
}

0 comments on commit e5ed0a2

Please sign in to comment.