Skip to content

Commit

Permalink
Add --header option (#75)
Browse files Browse the repository at this point in the history
Fixes #73
  • Loading branch information
lucapette authored Oct 18, 2022
1 parent 4fdd104 commit 708ef72
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 8 deletions.
10 changes: 8 additions & 2 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/kr/pretty"
)

// In the following tests, there's a lot going on. Please have a look at the
// following article for a longer explanation:
// In these tests, there's a lot going on. Have a look at this article for a
// longer explanation:
// https://lucapette.me/writing/writing-integration-tests-for-a-go-cli-application

var update = flag.Bool("update", false, "update golden files")
Expand Down Expand Up @@ -100,6 +100,12 @@ func TestCLI(t *testing.T) {
"default-format.golden",
false,
},
{
"default format with headers",
[]string{"--header", "int:42,42", "enum:foo,foo"},
"default-format-with-headers.golden",
false,
},
{
"unknown generators",
[]string{"madeupgenerator", "anothermadeupgenerator"},
Expand Down
11 changes: 11 additions & 0 deletions integration/golden/default-format-with-headers.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int enum
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
42 foo
1 change: 1 addition & 0 deletions integration/golden/file-does-not-exist.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Usage: fakedata [option ...] generator...
-g, --generator string show help for a specific generator
-G, --generators lists available generators
-c, --generators-with-constraints lists available generators with constraints
-H, --header adds headers row
-h, --help shows help
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
Expand Down
1 change: 1 addition & 0 deletions integration/golden/help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Usage: fakedata [option ...] generator...
-g, --generator string show help for a specific generator
-G, --generators lists available generators
-c, --generators-with-constraints lists available generators with constraints
-H, --header adds headers row
-h, --help shows help
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
Expand Down
1 change: 1 addition & 0 deletions integration/golden/path-empty.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Usage: fakedata [option ...] generator...
-g, --generator string show help for a specific generator
-G, --generators lists available generators
-c, --generators-with-constraints lists available generators with constraints
-H, --header adds headers row
-h, --help shows help
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
Expand Down
1 change: 1 addition & 0 deletions integration/golden/unknown-format.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Usage: fakedata [option ...] generator...
-g, --generator string show help for a specific generator
-G, --generators lists available generators
-c, --generators-with-constraints lists available generators with constraints
-H, --header adds headers row
-h, --help shows help
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
Expand Down
1 change: 1 addition & 0 deletions integration/golden/unknown-generators.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Usage: fakedata [option ...] generator...
-g, --generator string show help for a specific generator
-G, --generators lists available generators
-c, --generators-with-constraints lists available generators with constraints
-H, --header adds headers row
-h, --help shows help
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
Expand Down
17 changes: 11 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,19 @@ func findTemplate(path string) string {

func main() {
var (
generatorsFlag = flag.BoolP("generators", "G", false, "lists available generators")
generatorFlag = flag.StringP("generator", "g", "", "show help for a specific generator")
completionFlag = flag.StringP("completion", "C", "", "print shell completion function, pass shell name as argument (\"bash\", \"zsh\" or \"fish\")")
constraintsFlag = flag.BoolP("generators-with-constraints", "c", false, "lists available generators with constraints")
formatFlag = flag.StringP("format", "f", "column", "generates rows in f format. Available formats: column|sql")
generatorFlag = flag.StringP("generator", "g", "", "show help for a specific generator")
generatorsFlag = flag.BoolP("generators", "G", false, "lists available generators")
headerFlag = flag.BoolP("header", "H", false, "adds headers row")
helpFlag = flag.BoolP("help", "h", false, "shows help")
limitFlag = flag.IntP("limit", "l", 10, "limits rows up to n")
separatorFlag = flag.StringP("separator", "s", " ", "specifies separator for the column format")
streamFlag = flag.BoolP("stream", "S", false, "streams rows till the end of time")
formatFlag = flag.StringP("format", "f", "column", "generates rows in f format. Available formats: column|sql")
tableFlag = flag.StringP("table", "t", "TABLE", "table name of the sql format")
separatorFlag = flag.StringP("separator", "s", " ", "specifies separator for the column format")
templateFlag = flag.StringP("template", "T", "", "Use template as input")
completionFlag = flag.StringP("completion", "C", "", "print shell completion function, pass shell name as argument (\"bash\", \"zsh\" or \"fish\")")
versionFlag = flag.BoolP("version", "v", false, "shows version information")
helpFlag = flag.BoolP("help", "h", false, "shows help")
)

flag.Usage = func() {
Expand Down Expand Up @@ -175,6 +176,10 @@ func main() {
fOut := bufio.NewWriter(os.Stdout)
defer fOut.Flush()

if *headerFlag {
columns.GenerateHeader(fOut, formatter)
}

if *streamFlag {
for {
columns.GenerateRow(fOut, formatter)
Expand Down
11 changes: 11 additions & 0 deletions pkg/fakedata/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ func (columns Columns) GenerateRow(f io.Writer, formatter Formatter) {

fmt.Fprintf(f, "%s\n", formatter.Format(columns, values))
}

// GenerateRow generates an header row using column names
// in the specified format
func (columns Columns) GenerateHeader(f io.Writer, formatter Formatter) {
values := make([]string, len(columns))
for i, column := range columns {
values[i] = column.Name
}

fmt.Fprintf(f, "%s\n", formatter.Format(columns, values))
}

0 comments on commit 708ef72

Please sign in to comment.