package main import ( "os" "strings" "github.com/alexflint/go-arg" "go.wit.com/lib/gui/shell" "go.wit.com/log" "golang.org/x/text/cases" "golang.org/x/text/language" ) // sent via -ldflags var VERSION string var BUILDTIME string func main() { pp := arg.MustParse(&argv) // you need a proto file if argv.Proto == "" { log.Info("you must provide --proto ") os.Exit(-1) } if !shell.Exists(argv.Proto) { log.Info("protobuf", argv.Proto, "is missing") if !argv.DryRun { os.Exit(-1) } } if !strings.HasSuffix(argv.Proto, ".proto") { log.Info("protobuf", argv.Proto, "must end in .proto") os.Exit(-1) } // you need --upbase and --lobase if argv.Proto == "" { pp.WriteHelp(os.Stdout) os.Exit(-1) } cmd := []string{"go", "list", "-f", "'{{.Name}}'"} result := shell.Run(cmd) packageName := strings.Join(result.Stdout, "\n") packageName = strings.TrimSpace(packageName) packageName = strings.Trim(packageName, "'") log.Info("packageName == ", packageName) protobase := strings.TrimSuffix(argv.Proto, ".proto") sortmap := make(map[string]string) sortmap["package"] = packageName sortmap["protobase"] = protobase if argv.LoBase == "" { // if not set, assumed to be protobase sortmap["base"] = protobase } else { sortmap["base"] = argv.LoBase } sortmap["lock"] = sortmap["base"] + "sMu" // is nonglobal and plural if argv.UpBase == "" { sortmap["Base"] = cases.Title(language.English, cases.NoLower).String(protobase) sortmap["Bases"] = sortmap["Base"] + "s" } else { sortmap["Base"] = argv.UpBase sortmap["Bases"] = sortmap["Base"] + "s" } if argv.DryRun { for k, v := range sortmap { log.Info(k, "=", v) } os.Exit(0) } // add mutex if err := addMutex(sortmap); err == nil { log.Info("adding mutex to existing protoc-gen-go file worked") } else { log.Info("adding mutex to existing protoc-gen-go file did not work") } f, _ := os.OpenFile(protobase+".sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600) header(f, sortmap) syncLock(f, sortmap) iterTop(f, sortmap) iterNext(f, sortmap) // setup Sort() functions if len(argv.Sort) == 0 { // don't do any sorting // setup Append() functions if argv.Append == "" { iterAppend(f, sortmap) // Append() enforce no unique keys } else { iterAppend(f, sortmap) // Append() enforce no unique keys sortmap["append"] = argv.Append iterAppend(f, sortmap) // Append() enforce unique key argv.Append } } else { sortparts := strings.Split(argv.Sort[0], ",") sortmap["sortBy"] = sortparts[0] sortmap["sortKey"] = sortparts[1] iterSort(f, sortmap) if argv.Append == "" { iterAppend(f, sortmap) // Append() enforce no unique keys } else { iterAppend(f, sortmap) // Append() enforce no unique keys sortmap["append"] = argv.Append iterAppend(f, sortmap) // Append() enforce unique key argv.Append } sortmap["append"] = sortmap["sortKey"] iterAppend(f, sortmap) // Append() enforce unique key argv.Append // add ReplaceKey() iterDelete(f, sortmap) iterReplace(f, sortmap) } iterEnd(f, sortmap) if argv.NoMarshal { log.Info("not making marshal.pb.go file (--no-marshal == true)") } else { // make the foo.marshal.pb.go file marshal(sortmap) } }