func main() { var ( mainErr error exitCode int ) defer func() { if r := recover(); r != nil { fmt.Fprintf(os.Stderr, "Runtime panic: %v: %s\n", r, debug.Stack()) exitCode = exitcodeInternalError } if mainErr != nil { fmt.Fprintf(os.Stderr, "Error 111: %v\n", mainErr) } os.Exit(exitCode) }() var ( in = flag.String("in", "", "file to parse instead of stdin") out = flag.String("out", "", "file to save output to instead of stdout") pkgName = flag.String("pkg", "", "package name for generated files") imports Strings prefix = "https://github.com/metabition/gennylib/raw/master/" ) flag.Var(&imports, "imp", "spcify import explicitly (can be specified multiple times)") flag.Usage = usage flag.Parse() args := flag.Args() if len(args) < 2 { usage() os.Exit(exitcodeInvalidArgs) } if strings.ToLower(args[0]) != "gen" && strings.ToLower(args[0]) != "get" { usage() os.Exit(exitcodeInvalidArgs) } // parse the typesets var setsArg = args[1] if strings.ToLower(args[0]) == "get" { setsArg = args[2] } typeSets, err := parse.TypeSet(setsArg) if err != nil { exitCode, mainErr = exitcodeInvalidTypeSet, err return } var outWriter io.Writer if len(*out) > 0 { outFile, err := os.Create(*out) if err != nil { exitCode, mainErr = exitcodeDestFileFailed, err return } defer func(outPath string) { outFile.Close() if mainErr != nil { os.RemoveAll(outPath) } }(*out) outWriter = outFile } else { outWriter = os.Stdout } if strings.ToLower(args[0]) == "get" { if len(args) != 3 { fmt.Println("not enough arguments to get") usage() os.Exit(exitcodeInvalidArgs) } r, err := http.Get(prefix + args[1]) if err != nil { exitCode, mainErr = exitcodeGetFailed, err return } b, err := ioutil.ReadAll(r.Body) if err != nil { exitCode, mainErr = exitcodeGetFailed, err return } r.Body.Close() br := bytes.NewReader(b) err = gen(*in, *pkgName, br, typeSets, imports, outWriter) } else if len(*in) > 0 { var file *os.File file, err = os.Open(*in) if err != nil { exitCode = exitcodeSourceFileInvalid return } defer file.Close() err = gen(*in, *pkgName, file, typeSets, imports, outWriter) } else { var source []byte source, err = ioutil.ReadAll(os.Stdin) if err != nil { exitCode = exitcodeStdinFailed return } reader := bytes.NewReader(source) err = gen("stdin", *pkgName, reader, typeSets, imports, outWriter) } // do the work if err != nil { exitCode, mainErr = exitcodeGenFailed, err } }
func TestArgsToTypeset(t *testing.T) { args := "Person=man,woman Animal=dog,cat Place=london,paris" ts, err := parse.TypeSet(args) if assert.NoError(t, err) { if assert.Equal(t, 8, len(ts)) { assert.Equal(t, ts[0]["Person"], "man") assert.Equal(t, ts[0]["Animal"], "dog") assert.Equal(t, ts[0]["Place"], "london") assert.Equal(t, ts[1]["Person"], "man") assert.Equal(t, ts[1]["Animal"], "dog") assert.Equal(t, ts[1]["Place"], "paris") assert.Equal(t, ts[2]["Person"], "man") assert.Equal(t, ts[2]["Animal"], "cat") assert.Equal(t, ts[2]["Place"], "london") assert.Equal(t, ts[3]["Person"], "man") assert.Equal(t, ts[3]["Animal"], "cat") assert.Equal(t, ts[3]["Place"], "paris") assert.Equal(t, ts[4]["Person"], "woman") assert.Equal(t, ts[4]["Animal"], "dog") assert.Equal(t, ts[4]["Place"], "london") assert.Equal(t, ts[5]["Person"], "woman") assert.Equal(t, ts[5]["Animal"], "dog") assert.Equal(t, ts[5]["Place"], "paris") assert.Equal(t, ts[6]["Person"], "woman") assert.Equal(t, ts[6]["Animal"], "cat") assert.Equal(t, ts[6]["Place"], "london") assert.Equal(t, ts[7]["Person"], "woman") assert.Equal(t, ts[7]["Animal"], "cat") assert.Equal(t, ts[7]["Place"], "paris") } } ts, err = parse.TypeSet("Person=man Animal=dog Place=london") if assert.NoError(t, err) { assert.Equal(t, 1, len(ts)) } ts, err = parse.TypeSet("Person=1,2,3,4,5 Animal=1,2,3,4,5 Place=1,2,3,4,5") if assert.NoError(t, err) { assert.Equal(t, 125, len(ts)) } ts, err = parse.TypeSet("Person=1 Animal=1,2,3,4,5 Place=1,2") if assert.NoError(t, err) { assert.Equal(t, 10, len(ts)) } ts, err = parse.TypeSet("Person=interface{} Animal=interface{} Place=interface{}") if assert.NoError(t, err) { assert.Equal(t, 1, len(ts)) assert.Equal(t, ts[0]["Animal"], "interface{}") assert.Equal(t, ts[0]["Person"], "interface{}") assert.Equal(t, ts[0]["Place"], "interface{}") } }