Skip to content

Commit

Permalink
Adds zero-termination option for the blocks command
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Aug 6, 2020
1 parent b8ea66e commit 5ad5942
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
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", "0", 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

0 comments on commit 5ad5942

Please sign in to comment.