Skip to content

Commit

Permalink
Add null-termination for blocks command output (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison authored Aug 13, 2020
1 parent dde478d commit 3d07e2c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
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

0 comments on commit 3d07e2c

Please sign in to comment.