Skip to content
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 null-termination for blocks command output #25

Merged
merged 6 commits into from
Aug 13, 2020
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Use the `blocks` command to extract blocks from a file:

![blocks](_docs/blocks.png)

To output only the block content, separated by the null character, use the flags ``--zero-terminated` or `z`.

### Show What Format Would Do

Use the `diff` command to see what would be formatted (files can also be piped in on stdin) :
Expand Down
41 changes: 39 additions & 2 deletions cli/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestCmdBlocksDefault(t *testing.T) {
var outB strings.Builder
var errB strings.Builder
log := common.CreateLogger(&errB)
err := findBlocksInFile(fs, log, testcase.sourcefile, false, nil, &outB, &errB)
err := findBlocksInFile(fs, log, testcase.sourcefile, false, false, nil, &outB, &errB)
actualStdOut := outB.String()
actualStdErr := errB.String()

Expand Down Expand Up @@ -220,7 +220,7 @@ func TestCmdBlocksVerbose(t *testing.T) {
var outB strings.Builder
var errB strings.Builder
log := common.CreateLogger(&errB)
err := findBlocksInFile(fs, log, testcase.sourcefile, true, nil, &outB, &errB)
err := findBlocksInFile(fs, log, testcase.sourcefile, true, false, nil, &outB, &errB)
actualStdErr := errB.String()
if err != nil {
t.Fatalf("Case %q: Got an error when none was expected: %v", testcase.name, err)
Expand All @@ -235,3 +235,40 @@ func TestCmdBlocksVerbose(t *testing.T) {
})
}
}

func TestCmdBlocksZeroTerminated(t *testing.T) {
t.Parallel()

for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()

fs := afero.NewReadOnlyFs(afero.NewOsFs())

expectedBuilder := strings.Builder{}
for _, block := range testcase.expectedBlocks {
fmt.Fprint(&expectedBuilder, block.text, "\n\x00")
}
expected := expectedBuilder.String()

var outB strings.Builder
var errB strings.Builder
log := common.CreateLogger(&errB)
err := findBlocksInFile(fs, log, testcase.sourcefile, false, true, nil, &outB, &errB)
actualStdOut := outB.String()
actualStdErr := errB.String()

if err != nil {
t.Fatalf("Case %q: Got an error when none was expected: %v", testcase.name, err)
}

if actualStdOut != expected {
t.Errorf("Case %q: Output does not match expected:\n%s", testcase.name, diff.Diff(actualStdOut, expected))
}

if actualStdErr != "" {
t.Errorf("Case %q: Got error output:\n%s", testcase.name, actualStdErr)
}
})
}
}
19 changes: 14 additions & 5 deletions cli/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func Make() *cobra.Command {
diffCmd.Flags().StringP("pattern", "p", "", "glob pattern to match with each file name (e.g. *.markdown)")

// options
root.AddCommand(&cobra.Command{
blocksCmd := &cobra.Command{
Use: "blocks [file]",
Short: "extracts terraform blocks from a file ",
//options: no header (######), format (json? xml? etc), only should block x?
Expand All @@ -193,10 +193,14 @@ func Make() *cobra.Command {
}
log.Debugf("terrafmt blocks %s", filename)

verbose := viper.GetBool("verbose")
zeroTerminated, _ := cmd.Flags().GetBool("zero-terminated")
fs := afero.NewOsFs()
return findBlocksInFile(fs, log, filename, viper.GetBool("verbose"), cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr())
return findBlocksInFile(fs, log, filename, verbose, zeroTerminated, cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr())
},
})
}
root.AddCommand(blocksCmd)
blocksCmd.Flags().BoolP("zero-terminated", "z", false, "outputs blocks separated by null separator")

root.AddCommand(&cobra.Command{
Use: "version",
Expand Down Expand Up @@ -303,15 +307,20 @@ func versionCmd(cmd *cobra.Command, args []string) {
fmt.Println(" + " + terraformVersion)
}

func findBlocksInFile(fs afero.Fs, log *logrus.Logger, filename string, verbose bool, stdin io.Reader, stdout, stderr io.Writer) error {
func findBlocksInFile(fs afero.Fs, log *logrus.Logger, filename string, verbose, zeroTerminated bool, stdin io.Reader, stdout, stderr io.Writer) error {
br := blocks.Reader{
Log: log,
ReadOnly: true,
LineRead: blocks.ReaderIgnore,
BlockRead: func(br *blocks.Reader, i int, b string) error {
outW := stdout
fmt.Fprint(outW, c.Sprintf("\n<white>#######</> <cyan>B%d</><darkGray> @ #%d</>\n", br.BlockCount, br.LineCount))
if !zeroTerminated {
fmt.Fprint(outW, c.Sprintf("\n<white>#######</> <cyan>B%d</><darkGray> @ #%d</>\n", br.BlockCount, br.LineCount))
}
fmt.Fprint(outW, b)
if zeroTerminated {
fmt.Fprint(outW, "\x00")
}
return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion cli/upgrade012_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestCmdUpgrade012StdinVerbose(t *testing.T) {
}
}

func TestCmdUpgrade012File(t *testing.T) {
func TestCmdUpgrade012FileDefault(t *testing.T) {
testcases := []struct {
name string
sourcefile string
Expand Down