diff --git a/argv.go b/argv.go index 52f1728..ca49bf1 100644 --- a/argv.go +++ b/argv.go @@ -16,6 +16,7 @@ type args struct { Sort []string `arg:"--sort" help:"how and what to sort on"` Marshal []string `arg:"--marshal" help:"what to marshal on"` NoMarshal bool `arg:"--no-marshal" help:"do not make a marshal.pb.go file"` + NoSort bool `arg:"--no-sort" help:"do not make a sort.pb.go file"` DryRun bool `arg:"--dry-run" help:"show what would be run"` } diff --git a/main.go b/main.go index 61f671b..01e24fe 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,8 @@ import ( var VERSION string var BUILDTIME string +var sortmap map[string]string + func main() { pp := arg.MustParse(&argv) @@ -52,7 +54,7 @@ func main() { protobase := strings.TrimSuffix(argv.Proto, ".proto") - sortmap := make(map[string]string) + sortmap = make(map[string]string) sortmap["package"] = packageName sortmap["protobase"] = protobase if argv.LoBase == "" { @@ -80,50 +82,17 @@ func main() { // add mutex if err := addMutex(sortmap); err == nil { log.Info("adding mutex to existing protoc-gen-go file worked") + sortmap["mutex"] = "true" } else { log.Info("adding mutex to existing protoc-gen-go file did not work") + sortmap["mutex"] = "false" } - 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 - } + if argv.NoSort { + log.Info("not making sort.pb.go file (--no-sort == true)") } 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) + makeSortfile() } - iterEnd(f, sortmap) if argv.NoMarshal { log.Info("not making marshal.pb.go file (--no-marshal == true)") diff --git a/sort.go b/sort.go index c7e5a07..871632b 100644 --- a/sort.go +++ b/sort.go @@ -3,8 +3,52 @@ package main import ( "fmt" "io" + "os" + "strings" ) +func makeSortfile() { + f, _ := os.OpenFile(sortmap["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) +} + func headerComment(w io.Writer) { fmt.Fprintln(w, "") fmt.Fprintln(w, "// this file was autogenerated with autogenpb")