Skip to content

Commit

Permalink
Add --stream option
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapette committed Oct 3, 2022
1 parent 0309c50 commit 9b69a41
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 25 deletions.
23 changes: 11 additions & 12 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ name: Go

on:
push:
branches: [ '*' ]
branches: ["*"]
pull_request:
branches: [ '*' ]
branches: ["*"]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.19

- name: build
run: make build
- name: build
run: make build

- name: test
run: make test
- name: test
run: make test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ the number of iterations. `Loop` must be used with `range` e.g.
```

`Loop` can take a second argument, so that you can specify a range and
`fakedata` will generate a random number of interations in that range. For
`fakedata` will generate a random number of iterations in that range. For
example:

```html
Expand Down
3 changes: 1 addition & 2 deletions cmd/import/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -119,7 +118,7 @@ func main() {
content := fmt.Sprintf(fileTemplate, task.Var, task.Key, task.Var, data)

// Write to Go file
if err := ioutil.WriteFile(file, []byte(content), 0644); err != nil {
if err := os.WriteFile(file, []byte(content), 0644); err != nil {
log.Fatal(err)
}
}
Expand Down
5 changes: 2 additions & 3 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -56,7 +55,7 @@ func (tf *testFile) path() string {

func (tf *testFile) write(content string) {
tf.t.Helper()
err := ioutil.WriteFile(tf.path(), []byte(content), 0644)
err := os.WriteFile(tf.path(), []byte(content), 0644)
if err != nil {
tf.t.Fatalf("could not write %s: %v", tf.name, err)
}
Expand All @@ -74,7 +73,7 @@ func (tf *testFile) asFile() *os.File {
func (tf *testFile) load() string {
tf.t.Helper()

content, err := ioutil.ReadFile(tf.path())
content, err := os.ReadFile(tf.path())
if err != nil {
tf.t.Fatalf("could not read file %s: %v", tf.name, err)
}
Expand Down
2 changes: 1 addition & 1 deletion integration/golden/broken-template.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
template: template:1: unexpected "}" in operand
template: template:1: bad character U+007D '}'
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 @@ -9,6 +9,7 @@ Usage: fakedata [option ...] field...
-c, --generators-with-constraints lists available generators with constraints
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
-S, --stream streams rows till the end of time
-t, --table string table name of the sql format (default "TABLE")
-T, --template string Use template as input
-v, --version shows version information
1 change: 1 addition & 0 deletions integration/golden/help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Usage: fakedata [option ...] field...
-c, --generators-with-constraints lists available generators with constraints
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
-S, --stream streams rows till the end of time
-t, --table string table name of the sql format (default "TABLE")
-T, --template string Use template as input
-v, --version shows version information
1 change: 1 addition & 0 deletions integration/golden/path-empty.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Usage: fakedata [option ...] field...
-c, --generators-with-constraints lists available generators with constraints
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
-S, --stream streams rows till the end of time
-t, --table string table name of the sql format (default "TABLE")
-T, --template string Use template as input
-v, --version shows version information
1 change: 1 addition & 0 deletions integration/golden/unknown-format.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Usage: fakedata [option ...] field...
-c, --generators-with-constraints lists available generators with constraints
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
-S, --stream streams rows till the end of time
-t, --table string table name of the sql format (default "TABLE")
-T, --template string Use template as input
-v, --version shows version information
1 change: 1 addition & 0 deletions integration/golden/unknown-generators.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Usage: fakedata [option ...] field...
-c, --generators-with-constraints lists available generators with constraints
-l, --limit int limits rows up to n (default 10)
-s, --separator string specifies separator for the column format (default " ")
-S, --stream streams rows till the end of time
-t, --table string table name of the sql format (default "TABLE")
-T, --template string Use template as input
-v, --version shows version information
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"math/rand"
"os"
"time"
Expand Down Expand Up @@ -42,7 +42,7 @@ func isPipe() bool {

func findTemplate(path string) string {
if path != "" {
tp, err := ioutil.ReadFile(path)
tp, err := os.ReadFile(path)
if err != nil {
fmt.Printf("unable to read input: %s", err)
os.Exit(1)
Expand All @@ -52,7 +52,7 @@ func findTemplate(path string) string {
}

if isPipe() {
tp, err := ioutil.ReadAll(os.Stdin)
tp, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Printf("unable to read input: %s", err)
os.Exit(1)
Expand All @@ -70,6 +70,7 @@ func main() {
generatorFlag = flag.StringP("generator", "g", "", "show help for a specific generator")
constraintsFlag = flag.BoolP("generators-with-constraints", "c", false, "lists available generators with constraints")
limitFlag = flag.IntP("limit", "l", 10, "limits rows up to n")
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")
Expand Down Expand Up @@ -164,6 +165,11 @@ func main() {
os.Exit(1)
}

if *streamFlag {
for {
fmt.Println(columns.GenerateRow(formatter))
}
}
for i := 0; i < *limitFlag; i++ {
fmt.Println(columns.GenerateRow(formatter))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/data/occupations.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pkg/fakedata/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fakedata

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"sort"
Expand Down Expand Up @@ -184,7 +183,7 @@ func file(path string) (func() string, error) {

filePath := strings.Trim(path, "'\"")

content, err := ioutil.ReadFile(filePath)
content, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("could not read file %s: %v", filePath, err)
}
Expand Down

0 comments on commit 9b69a41

Please sign in to comment.