-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit adds the abillity to use cli pipes to pipe templates into `fakedata`. It also adds all generators as named functions to the template funcMap.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,12 @@ package main | |
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/lucapette/fakedata/pkg/fakedata" | ||
flag "github.com/spf13/pflag" | ||
"io/ioutil" | ||
"math/rand" | ||
"os" | ||
"time" | ||
|
||
"github.com/lucapette/fakedata/pkg/fakedata" | ||
flag "github.com/spf13/pflag" | ||
) | ||
|
||
var version = "master" | ||
|
@@ -65,8 +65,27 @@ func main() { | |
os.Exit(0) | ||
} | ||
|
||
rand.Seed(time.Now().UnixNano()) | ||
|
||
if *templateFlag != "" { | ||
fakedata.ParseTemplate(*templateFlag) | ||
fakedata.ParseTemplate(*templateFlag, *limitFlag) | ||
os.Exit(0) | ||
} | ||
|
||
stat, _ := os.Stdin.Stat() | ||
// Check if template data is piped to fakedata | ||
if (stat.Mode() & os.ModeCharDevice) == 0 { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jorinvo
Contributor
|
||
t, err := ioutil.ReadAll(os.Stdin) | ||
|
||
if err != nil { | ||
fmt.Println("Unable to read input: %s", err) | ||
os.Exit(0) | ||
} | ||
|
||
err = fakedata.ParseTemplateFromPipe(string(t), *limitFlag) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
|
@@ -75,8 +94,6 @@ func main() { | |
os.Exit(0) | ||
} | ||
|
||
rand.Seed(time.Now().UnixNano()) | ||
This comment has been minimized.
Sorry, something went wrong.
KevinGimbel
Author
Collaborator
|
||
|
||
columns, err := fakedata.NewColumns(flag.Args()) | ||
if err != nil { | ||
fmt.Printf("%v\n\nSee fakedata --generators for a list of available generators.\n", err) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,29 +3,13 @@ package fakedata | |
import ( | ||
"fmt" | ||
"html/template" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var fakeColumn = Column{"Fake", "fakecolumn", ""} | ||
|
||
// var generatorFunctions template.FuncMap | ||
var generatorFunctions = template.FuncMap{ | ||
"Name": func() string { | ||
return generators["name"].Func(fakeColumn) | ||
}, | ||
"Email": func() string { | ||
return generators["email"].Func(fakeColumn) | ||
}, | ||
"Int": func(a, b int) string { | ||
return generators["int"].Func(Column{"Int", "int", fmt.Sprintf("%d..%d", a, b)}) | ||
}, | ||
"Enum": func(keywords ...string) string { | ||
constraints := createConstraints(keywords) | ||
return generators["enum"].Func(Column{"Enum", "enu", constraints}) | ||
}, | ||
"Generator": func(name string) string { | ||
return generators[name].Func(fakeColumn) | ||
}, | ||
"Loop": func(i int) []int { | ||
c := make([]int, i) | ||
|
||
|
@@ -43,6 +27,99 @@ var generatorFunctions = template.FuncMap{ | |
} | ||
return false | ||
}, | ||
"Date": func() string { | ||
return generators["date"].Func(Column{Name: "tmplDate", Key: "tmplDateKey", Constraints: ""}) | ||
}, | ||
"DomainTld": func() string { | ||
return generators["domain.tld"].Func(Column{Name: "tmplDomainTld", Key: "tmplDomainTld", Constraints: ""}) | ||
}, | ||
"DomainName": func() string { | ||
return generators["domain.name"].Func(Column{Name: "tmplDomainName", Key: "tmplDomainName", Constraints: ""}) | ||
}, | ||
"Country": func() string { | ||
return generators["country"].Func(Column{Name: "tmplCountry", Key: "tmplCountryKey", Constraints: ""}) | ||
}, | ||
"CountryCode": func() string { | ||
return generators["country.code"].Func(Column{Name: "tmplCountryCode", Key: "tmplCountryCode", Constraints: ""}) | ||
}, | ||
"State": func() string { | ||
return generators["state"].Func(Column{Name: "tmplState", Key: "tmplStateKey", Constraints: ""}) | ||
}, | ||
"Timezone": func() string { | ||
return generators["timezone"].Func(Column{Name: "tmplTimezone", Key: "tmplTimezoneKey", Constraints: ""}) | ||
}, | ||
"Username": func() string { | ||
return generators["username"].Func(Column{Name: "tmplUsername", Key: "tmplUsernameKey", Constraints: ""}) | ||
}, | ||
"NameFirst": func() string { | ||
return generators["name.first"].Func(Column{Name: "tmplNameFirst", Key: "tmplNameFirstKey", Constraints: ""}) | ||
}, | ||
"NameLast": func() string { | ||
return generators["name.last"].Func(Column{Name: "tmplNameLast", Key: "tmplNameLastKey", Constraints: ""}) | ||
}, | ||
"Color": func() string { | ||
return generators["color"].Func(Column{Name: "tmplColor", Key: "tmplColorKey", Constraints: ""}) | ||
}, | ||
"ProductCategory": func() string { | ||
return generators["product.category"].Func(Column{Name: "tmplProductCategory", Key: "tmplProductCategoryKey", Constraints: ""}) | ||
}, | ||
"ProductName": func() string { | ||
return generators["product.name"].Func(Column{Name: "tmplProductName", Key: "tmplProductNameKey", Constraints: ""}) | ||
}, | ||
"EventAction": func() string { | ||
return generators["event.action"].Func(Column{Name: "tmplEventAction", Key: "tmplEventActionKey", Constraints: ""}) | ||
}, | ||
"HTTPMethod": func() string { | ||
return generators["http.method"].Func(Column{Name: "tmplHTTPMethod", Key: "tmplHTTPMethodKey", Constraints: ""}) | ||
}, | ||
"Name": func() string { | ||
return generators["name"].Func(Column{Name: "tmplName", Key: "tmplNameKey", Constraints: " "}) | ||
}, | ||
"Email": func() string { | ||
return generators["email"].Func(Column{Name: "tmplEmail", Key: "tmplEmailKey", Constraints: " "}) | ||
}, | ||
"Domain": func() string { | ||
return generators["domain"].Func(Column{Name: "tmplDomain", Key: "tmplDomainKey", Constraints: ""}) | ||
}, | ||
"IPv4": func() string { | ||
return generators["ipv4"].Func(Column{Name: "tmplIPv4", Key: "tmplIPv4Key", Constraints: ""}) | ||
}, | ||
"IPv6": func() string { | ||
return generators["ipv6"].Func(Column{Name: "tmplIPv6", Key: "tmplIPv6Key", Constraints: ""}) | ||
}, | ||
"MacAddress": func() string { | ||
return generators["mac.address"].Func(Column{Name: "tmplMacAddress", Key: "tmplMacAddressKey", Constraints: ""}) | ||
}, | ||
"Latitude": func() string { | ||
return generators["latitude"].Func(Column{Name: "tmplLatitude", Key: "tmplLatitudeKey", Constraints: ""}) | ||
}, | ||
"Longitude": func() string { | ||
return generators["longitude"].Func(Column{Name: "tmplLongitude", Key: "tmplLongitudeKey", Constraints: ""}) | ||
}, | ||
"Double": func() string { | ||
return generators["double"].Func(Column{Name: "tmplDouble", Key: "tmplDoubleKey", Constraints: ""}) | ||
}, | ||
"Int": func(params ...int) string { | ||
constraint := ".." | ||
a := 0 | ||
b := 1000 | ||
if len(params) == 1 { | ||
a = params[0] | ||
} | ||
if len(params) == 2 { | ||
a = params[0] | ||
b = params[1] | ||
} | ||
constraint = fmt.Sprintf("%d..%d", a, b) | ||
return generators["int"].Func(Column{"Int", "int", constraint}) | ||
}, | ||
"Enum": func(keywords ...string) string { | ||
constraints := createConstraints(keywords) | ||
return generators["enum"].Func(Column{"Enum", "enu", constraints}) | ||
}, | ||
"File": func(path string) string { | ||
return generators["file"].Func(Column{Name: "tmplA", Key: "tmplAKey", Constraints: path}) | ||
}, | ||
This comment has been minimized.
Sorry, something went wrong.
lucapette
Owner
|
||
} | ||
|
||
func createConstraints(params []string) string { | ||
|
@@ -63,13 +140,30 @@ func splitPathName(r rune) bool { | |
|
||
// ParseTemplate takes a path to a template file as argument. It parses the template file and executes it on | ||
// os.Stdout. | ||
func ParseTemplate(path string) { | ||
func ParseTemplate(path string, limit int) (err error) { | ||
tn := getTemplateNameFromPath(path) | ||
tmp, err := template.New(tn).Funcs(generatorFunctions).ParseFiles(path) | ||
if err != nil { | ||
fmt.Println(err) | ||
return err | ||
} | ||
b := io.Writer(os.Stdout) | ||
for i := 1; i <= limit; i++ { | ||
tmp.Execute(b, nil) | ||
} | ||
|
||
tmp.Execute(os.Stdout, generators) | ||
return nil | ||
} | ||
|
||
func ParseTemplateFromPipe(t string, limit int) (err error) { | ||
This comment has been minimized.
Sorry, something went wrong.
KevinGimbel
Author
Collaborator
|
||
tmp, err := template.New("stdin").Funcs(generatorFunctions).Parse(t) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
b := io.Writer(os.Stdout) | ||
for i := 1; i <= limit; i++ { | ||
tmp.Execute(b, nil) | ||
} | ||
|
||
return nil | ||
} |
I did some research and it appears that this is the way to check if there's data from a CLI pipe, e.g
echo "{{ Name }}" | fakedata --limit 1
- my initial idea was to see ifos.Stdin
holds data but thats not so easy. If we useioutil.ReadAll(os.Stdin)
without this checkfakedata
will wait for input forever. Adding a timeout was not a good idea in my opinion.