github.com/lheiskan/zebrapack@v4.1.1-0.20181107023619-e955d028f9bf+incompatible/cmd/addzid/main.go (about) 1 /* 2 addzid automatically adds `zid:"0"`, `zid:"1"`, ... tags to your Go structs. 3 4 Given a set of golang (Go) source files, addzid will tag the public 5 struct fields with sequential zid tags. This prepares your source 6 so that it can be fed to the `zebrapack` codegen tool. 7 8 `addzid` was dervied from the author's `bambam` tool to support ZebraPack. 9 */ 10 package main 11 12 import ( 13 "flag" 14 "fmt" 15 "os" 16 "strings" 17 ) 18 19 func use() { 20 fmt.Fprintf(os.Stderr, "\nuse: addzid {-o outdir} myGoSourceFile.go myGoSourceFile2.go ...\n") 21 fmt.Fprintf(os.Stderr, " # addzid makes it easy to add ZebraPack serialization[1] to Go source files.\n") 22 fmt.Fprintf(os.Stderr, " # addzid reads .go files and adds `zid` tags to struct fields.\n") 23 fmt.Fprintf(os.Stderr, " #\n # options:\n") 24 fmt.Fprintf(os.Stderr, " # -o=\"odir\" specifies the directory to write to (created if need be).\n") 25 fmt.Fprintf(os.Stderr, " # -unexported add tag to private fields of Go structs as well as public.\n") 26 fmt.Fprintf(os.Stderr, " # -version shows build version with git commit hash.\n") 27 fmt.Fprintf(os.Stderr, " # -debug print lots of debug info as we process.\n") 28 fmt.Fprintf(os.Stderr, " # -OVERWRITE modify .go files in-place, adding zid tags (by default\n # we write to the to -o dir).\n") 29 fmt.Fprintf(os.Stderr, " #\n # required: at least one .go source file for struct definitions.\n # note: the .go source file to process must be listed last, after any options.\n") 30 fmt.Fprintf(os.Stderr, " #\n") 31 fmt.Fprintf(os.Stderr, " # [1] https://github.com/glycerine/zebrapack \n") 32 fmt.Fprintf(os.Stderr, "\n") 33 os.Exit(1) 34 } 35 36 func main() { 37 MainArgs(os.Args) 38 } 39 40 // allow invocation from test 41 func MainArgs(args []string) { 42 //fmt.Println(os.Args) 43 os.Args = args 44 45 flag.Usage = use 46 if len(os.Args) < 2 { 47 use() 48 } 49 50 flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) 51 debug := flag.Bool("debug", false, "print lots of debug info as we process.") 52 verrequest := flag.Bool("version", false, "request git commit hash used to build this addzid") 53 outdir := flag.String("o", "odir", "specify output directory") 54 pkg := flag.String("p", "main", "specify package for generated code") 55 privs := flag.Bool("unexported", false, "tag private as well as public struct fields") 56 overwrite := flag.Bool("OVERWRITE", false, "replace named .go files with zid tagged versions.") 57 flag.Parse() 58 59 if debug != nil { 60 Verbose = *debug 61 } 62 63 if verrequest != nil && *verrequest { 64 fmt.Printf("%s\n", LASTGITCOMMITHASH) 65 os.Exit(0) 66 } 67 68 if outdir == nil || *outdir == "" { 69 fmt.Fprintf(os.Stderr, "required -o option missing. Use addzid -o <dirname> myfile.go # to specify the output directory.\n") 70 use() 71 } 72 73 if !DirExists(*outdir) { 74 err := os.MkdirAll(*outdir, 0755) 75 if err != nil { 76 panic(err) 77 } 78 } 79 80 if pkg == nil || *pkg == "" { 81 fmt.Fprintf(os.Stderr, "required -p option missing. Specify a package name for the generated go code with -p <pkgname>\n") 82 use() 83 } 84 85 // all the rest are input .go files 86 inputFiles := flag.Args() 87 88 if len(inputFiles) == 0 { 89 fmt.Fprintf(os.Stderr, "addzid needs at least one .go golang source file to process specified on the command line.\n") 90 os.Exit(1) 91 } 92 93 for _, fn := range inputFiles { 94 if !strings.HasSuffix(fn, ".go") && !strings.HasSuffix(fn, ".go.txt") { 95 fmt.Fprintf(os.Stderr, "error: addzid input file '%s' did not end in '.go' or '.go.txt'.\n", fn) 96 os.Exit(1) 97 } 98 } 99 100 x := NewExtractor() 101 x.fieldPrefix = " " 102 x.fieldSuffix = "\n" 103 x.outDir = *outdir 104 if privs != nil { 105 x.extractPrivate = *privs 106 } 107 if overwrite != nil { 108 x.overwrite = *overwrite 109 } 110 111 for _, inFile := range inputFiles { 112 _, err := x.ExtractStructsFromOneFile(nil, inFile) 113 if err != nil { 114 panic(err) 115 } 116 } 117 // get rid of default tmp dir 118 x.compileDir.Cleanup() 119 120 x.compileDir.DirPath = *outdir 121 122 x.SetFinalFieldOrder() 123 124 err := x.CopySourceFilesAddZidTag(nil) 125 if err != nil { 126 panic(err) 127 } 128 fmt.Printf("generated files in '%s'\n", x.compileDir.DirPath) 129 }