-
-
Notifications
You must be signed in to change notification settings - Fork 959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --terragrunt-include-module-prefix option #2493
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package util | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
) | ||
|
||
func PrefixedWriter(writer io.Writer, prefix string) io.Writer { | ||
return &prefixedWriter{writer: writer, prefix: prefix, beginningOfANewLine: true} | ||
} | ||
|
||
func (pf *prefixedWriter) Write(p []byte) (int, error) { | ||
buf := bytes.Buffer{} | ||
|
||
for _, b := range p { | ||
if pf.beginningOfANewLine { | ||
buf.WriteString(pf.prefix) | ||
pf.beginningOfANewLine = false | ||
} | ||
|
||
buf.WriteByte(b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering why output should be buffered and not directly written to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not want to call the underlying This buffering does not cross single Still, it needs an additional memory and one copying of the input |
||
|
||
pf.beginningOfANewLine = b == '\n' | ||
} | ||
|
||
n, err := pf.writer.Write(buf.Bytes()) | ||
if n > len(p) { | ||
n = len(p) | ||
} | ||
return n, err | ||
} | ||
|
||
type prefixedWriter struct { | ||
writer io.Writer | ||
prefix string | ||
beginningOfANewLine bool | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package util | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPrefixWriter(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
prefix string | ||
values []string | ||
expected string | ||
}{ | ||
{"p1 ", []string{"a", "b"}, "p1 ab"}, | ||
{"p2 ", []string{"a", "b"}, "p2 ab"}, | ||
|
||
{"", []string{"a", "b"}, "ab"}, | ||
|
||
{"p1 ", []string{"a", "b\n"}, "p1 ab\n"}, | ||
{"p1 ", []string{"a\n", "b"}, "p1 a\np1 b"}, | ||
{"p1 ", []string{"a\n", "b\n"}, "p1 a\np1 b\n"}, | ||
{"p1 ", []string{"a", "b", "c", "def"}, "p1 abcdef"}, | ||
{"p1 ", []string{"a", "b\n", "c", "def"}, "p1 ab\np1 cdef"}, | ||
{"p1 ", []string{"a", "b\nc", "def"}, "p1 ab\np1 cdef"}, | ||
{"p1 ", []string{"ab", "cd", "ef", "gh\n"}, "p1 abcdefgh\n"}, | ||
{"p1 ", []string{"ab", "cd\n", "ef", "gh\n"}, "p1 abcd\np1 efgh\n"}, | ||
{"p1 ", []string{"ab", "cd", "e\nf", "gh\n"}, "p1 abcde\np1 fgh\n"}, | ||
{"p1 ", []string{"ab", "cd", "ef\n", "gh\n"}, "p1 abcdef\np1 gh\n"}, | ||
{"p1 ", []string{"ab\ncd\nef\ngh\n"}, "p1 ab\np1 cd\np1 ef\np1 gh\n"}, | ||
{"p1 ", []string{"ab\n\n\ngh\n"}, "p1 ab\np1 \np1 \np1 gh\n"}, | ||
|
||
{"p1 ", []string{""}, ""}, | ||
{"p1 ", []string{"\n"}, "p1 \n"}, | ||
{"p1\n", []string{"\n"}, "p1\n\n"}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
var b bytes.Buffer | ||
pw := PrefixedWriter(&b, testCase.prefix) | ||
for _, input := range testCase.values { | ||
written, err := pw.Write([]byte(input)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, written, len(input)) | ||
} | ||
assert.Equal(t, testCase.expected, b.String()) | ||
} | ||
} | ||
|
||
type FailingWriter struct{} | ||
|
||
func (fw *FailingWriter) Write(b []byte) (int, error) { | ||
return 0, errors.New("write failed") | ||
} | ||
|
||
func TestPrefixWriterFail(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
prefix string | ||
values []string | ||
expected string | ||
}{ | ||
{"p1 ", []string{"a", "b"}, "p1 ab"}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
pw := PrefixedWriter(&FailingWriter{}, testCase.prefix) | ||
for _, input := range testCase.values { | ||
written, err := pw.Write([]byte(input)) | ||
assert.Error(t, err) | ||
assert.Equal(t, written, 0) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticed that
terragrunt-include-module-prefix
is not listed in the help outputThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Included the info in help output. Is there any other place the help should be updated?