func main() { flag.Parse() args := flag.Args() if len(args) < 2 { fail("%s", usage) } sourceDir := getSourceDir(args[0]) interfaceName := args[1] fakeName := getFakeName(interfaceName, *fakeNameFlag) outputPath := getOutputPath(sourceDir, fakeName, *outputPathFlag) outputDir := filepath.Dir(outputPath) fakePackageName := filepath.Base(outputDir) shouldPrintToStdout := len(args) >= 3 && args[2] == "-" iface, err := locator.GetInterfaceFromFilePath(interfaceName, sourceDir) if err != nil { fail("%v", err) } code, err := generator.CodeGenerator{ Model: *iface, StructName: fakeName, PackageName: fakePackageName, }.GenerateFake() if err != nil { fail("%v", err) } if shouldPrintToStdout { fmt.Println(code) } else { os.MkdirAll(outputDir, 0777) file, err := os.Create(outputPath) if err != nil { fail("Couldn't create fake file - %v", err) } _, err = file.WriteString(code) if err != nil { fail("Couldn't write to fake file - %v", err) } rel, err := filepath.Rel(cwd(), outputPath) if err != nil { fail("%v", err) } fmt.Printf("Wrote `%s` to `%s`\n", fakeName, rel) } }
func main() { flag.Parse() args := flag.Args() if len(args) < 1 { fail("%s", usage) return } argumentParser := arguments.NewArgumentParser( fail, cwd, filepath.EvalSymlinks, os.Stat, terminal.NewUI(), locator.NewInterfaceLocator(), ) parsedArgs := argumentParser.ParseArguments(args...) interfaceName := parsedArgs.InterfaceName fakeName := parsedArgs.FakeImplName sourceDir := parsedArgs.SourcePackageDir outputPath := parsedArgs.OutputPath outputDir := filepath.Dir(outputPath) fakePackageName := filepath.Base(outputDir) iface, err := locator.GetInterfaceFromFilePath(interfaceName, sourceDir) if err != nil { fail("%v", err) } code, err := generator.CodeGenerator{ Model: *iface, StructName: fakeName, PackageName: fakePackageName, }.GenerateFake() if err != nil { fail("%v", err) } newCode, err := format.Source([]byte(code)) code = string(newCode) if parsedArgs.PrintToStdOut { fmt.Println(code) } else { os.MkdirAll(outputDir, 0777) file, err := os.Create(outputPath) if err != nil { fail("Couldn't create fake file - %v", err) } _, err = file.WriteString(code) if err != nil { fail("Couldn't write to fake file - %v", err) } rel, err := filepath.Rel(cwd(), outputPath) if err != nil { fail("%v", err) } fmt.Printf("Wrote `%s` to `%s`\n", fakeName, rel) } }