Skip to content

Commit

Permalink
Make stream flag work with templates
Browse files Browse the repository at this point in the history
I don't like the solution much but it works.

done > perfect.
  • Loading branch information
lucapette committed Oct 11, 2022
1 parent 9b69a41 commit 03f3e4e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func main() {
rand.Seed(time.Now().UnixNano())

if tmpl := findTemplate(*templateFlag); tmpl != "" {
if err := fakedata.ExecuteTemplate(tmpl, *limitFlag); err != nil {
if err := fakedata.ExecuteTemplate(tmpl, *limitFlag, *streamFlag); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/fakedata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,23 @@ func (tf templateFactory) getFunctions() template.FuncMap {
}

// ExecuteTemplate takes a tmpl string and a n int and generates n rows of based
// on the specified tmpl
func ExecuteTemplate(tmpl string, n int) (err error) {
// on the specified tmpl. Will loop forever if streamMode is true
func ExecuteTemplate(tmpl string, n int, streamMode bool) (err error) {
f := newTemplateFactory()
t, err := template.New("template").Funcs(f.getFunctions()).Parse(tmpl)
if err != nil {
return err
}

if streamMode {
for {
err = t.Execute(os.Stdout, nil)
if err != nil {
return err
}
}
}

for i := 1; i <= n; i++ {
err = t.Execute(os.Stdout, nil)
if err != nil {
Expand Down

0 comments on commit 03f3e4e

Please sign in to comment.