diff --git a/integration/cli_test.go b/integration/cli_test.go index 0f7dc8b..77734f8 100644 --- a/integration/cli_test.go +++ b/integration/cli_test.go @@ -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") @@ -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"}, diff --git a/integration/golden/default-format-with-headers.golden b/integration/golden/default-format-with-headers.golden new file mode 100644 index 0000000..a7bb15c --- /dev/null +++ b/integration/golden/default-format-with-headers.golden @@ -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 diff --git a/integration/golden/file-does-not-exist.golden b/integration/golden/file-does-not-exist.golden index 3460d74..c5dc906 100644 --- a/integration/golden/file-does-not-exist.golden +++ b/integration/golden/file-does-not-exist.golden @@ -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 " ") diff --git a/integration/golden/help.golden b/integration/golden/help.golden index f6fd4ba..2c87359 100644 --- a/integration/golden/help.golden +++ b/integration/golden/help.golden @@ -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 " ") diff --git a/integration/golden/path-empty.golden b/integration/golden/path-empty.golden index 4874e9e..0ee8bb4 100644 --- a/integration/golden/path-empty.golden +++ b/integration/golden/path-empty.golden @@ -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 " ") diff --git a/integration/golden/unknown-format.golden b/integration/golden/unknown-format.golden index 4fb7c6d..bbb2c12 100644 --- a/integration/golden/unknown-format.golden +++ b/integration/golden/unknown-format.golden @@ -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 " ") diff --git a/integration/golden/unknown-generators.golden b/integration/golden/unknown-generators.golden index c147b02..d671438 100644 --- a/integration/golden/unknown-generators.golden +++ b/integration/golden/unknown-generators.golden @@ -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 " ") diff --git a/main.go b/main.go index f0813de..3934657 100644 --- a/main.go +++ b/main.go @@ -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() { @@ -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) diff --git a/pkg/fakedata/column.go b/pkg/fakedata/column.go index d1fce24..6b7b716 100644 --- a/pkg/fakedata/column.go +++ b/pkg/fakedata/column.go @@ -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)) +}