2024-11-29 08:30:19 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2024-11-29 11:08:11 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alexflint/go-arg"
|
|
|
|
"go.wit.com/lib/gui/shell"
|
|
|
|
"go.wit.com/log"
|
2024-11-29 14:08:33 -06:00
|
|
|
"golang.org/x/text/cases"
|
|
|
|
"golang.org/x/text/language"
|
2024-11-29 08:30:19 -06:00
|
|
|
)
|
|
|
|
|
2024-11-29 11:08:11 -06:00
|
|
|
// sent via -ldflags
|
|
|
|
var VERSION string
|
|
|
|
var BUILDTIME string
|
2024-11-29 10:20:06 -06:00
|
|
|
|
2024-11-29 08:30:19 -06:00
|
|
|
func main() {
|
2024-11-29 11:08:11 -06:00
|
|
|
pp := arg.MustParse(&argv)
|
|
|
|
|
2024-11-29 11:15:40 -06:00
|
|
|
// you need a proto file
|
|
|
|
if argv.Proto == "" {
|
|
|
|
log.Info("you must provide --proto <filename>")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !shell.Exists(argv.Proto) {
|
|
|
|
log.Info("protobuf", argv.Proto, "is missing")
|
2024-11-29 11:56:57 -06:00
|
|
|
if !argv.DryRun {
|
2024-11-29 11:32:27 -06:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2024-11-29 11:15:40 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 11:21:56 -06:00
|
|
|
if !strings.HasSuffix(argv.Proto, ".proto") {
|
2024-11-29 11:15:40 -06:00
|
|
|
log.Info("protobuf", argv.Proto, "must end in .proto")
|
|
|
|
os.Exit(-1)
|
2024-11-29 11:08:11 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 11:21:56 -06:00
|
|
|
// you need --upbase and --lobase
|
2024-11-29 14:08:33 -06:00
|
|
|
if argv.Proto == "" {
|
2024-11-29 11:21:56 -06:00
|
|
|
pp.WriteHelp(os.Stdout)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
2024-11-29 11:08:11 -06:00
|
|
|
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)
|
|
|
|
|
2024-11-29 11:15:40 -06:00
|
|
|
protobase := strings.TrimSuffix(argv.Proto, ".proto")
|
|
|
|
|
2024-11-29 10:30:58 -06:00
|
|
|
sortmap := make(map[string]string)
|
2024-11-29 11:08:11 -06:00
|
|
|
sortmap["package"] = packageName
|
2024-11-29 11:56:57 -06:00
|
|
|
sortmap["protobase"] = protobase
|
2024-11-29 14:08:33 -06:00
|
|
|
if argv.LoBase == "" {
|
|
|
|
// if not set, assumed to be protobase
|
|
|
|
sortmap["base"] = protobase
|
|
|
|
} else {
|
|
|
|
sortmap["base"] = argv.LoBase
|
|
|
|
}
|
2024-11-29 15:19:04 -06:00
|
|
|
sortmap["lock"] = sortmap["base"] + "sMu" // is nonglobal and plural
|
2024-11-29 14:08:33 -06:00
|
|
|
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"
|
|
|
|
}
|
2024-11-29 13:35:13 -06:00
|
|
|
|
2024-11-29 11:32:27 -06:00
|
|
|
if argv.DryRun {
|
|
|
|
for k, v := range sortmap {
|
|
|
|
log.Info(k, "=", v)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-11-29 15:19:04 -06:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2024-11-29 11:32:27 -06:00
|
|
|
f, _ := os.OpenFile(protobase+".sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
|
2024-11-29 10:30:58 -06:00
|
|
|
|
|
|
|
header(f, sortmap)
|
|
|
|
syncLock(f, sortmap)
|
|
|
|
iterTop(f, sortmap)
|
|
|
|
iterNext(f, sortmap)
|
2024-11-29 14:08:33 -06:00
|
|
|
|
|
|
|
// setup Sort() functions
|
|
|
|
if len(argv.Sort) == 0 {
|
|
|
|
// don't do any sorting
|
2024-11-29 14:18:35 -06:00
|
|
|
// 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
|
|
|
|
}
|
2024-11-29 13:35:13 -06:00
|
|
|
} else {
|
2024-11-29 14:08:33 -06:00
|
|
|
sortparts := strings.Split(argv.Sort[0], ",")
|
|
|
|
sortmap["sortBy"] = sortparts[0]
|
|
|
|
sortmap["sortKey"] = sortparts[1]
|
|
|
|
|
|
|
|
iterSort(f, sortmap)
|
|
|
|
|
2024-11-29 14:18:35 -06:00
|
|
|
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
|
|
|
|
|
2024-11-29 13:35:13 -06:00
|
|
|
// add ReplaceKey()
|
|
|
|
iterDelete(f, sortmap)
|
|
|
|
iterReplace(f, sortmap)
|
|
|
|
}
|
2024-11-29 10:30:58 -06:00
|
|
|
iterEnd(f, sortmap)
|
2024-11-29 11:56:57 -06:00
|
|
|
|
2024-11-29 12:35:12 -06:00
|
|
|
if argv.NoMarshal {
|
|
|
|
log.Info("not making marshal.pb.go file (--no-marshal == true)")
|
|
|
|
} else {
|
|
|
|
// make the foo.marshal.pb.go file
|
|
|
|
marshal(sortmap)
|
|
|
|
}
|
2024-11-29 13:35:13 -06:00
|
|
|
|
|
|
|
}
|