diff options
| author | Jeff Carr <[email protected]> | 2024-12-01 22:21:09 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-12-01 22:21:09 -0600 |
| commit | f888dab0f223ae4b41de232e941a62a46f978309 (patch) | |
| tree | a8090e1bbaca220946dbc998666256effbf623c1 | |
| parent | 7a1c1e318045165f14894ce4a12e3105a68eb3cc (diff) | |
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | main.go | 29 | ||||
| -rw-r--r-- | marshal.go | 2 | ||||
| -rw-r--r-- | parseProtoFile.go | 43 | ||||
| -rw-r--r-- | sort.go | 4 |
5 files changed, 60 insertions, 22 deletions
@@ -1,11 +1,11 @@ VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d) -run: build vet +run: vet build vet: @GO111MODULE=off go vet - @echo this go library package builds okay + @echo this go binary package should build okay build: GO111MODULE=off go build \ @@ -31,6 +31,7 @@ func main() { // you need a proto file if argv.Proto == "" { log.Info("you must provide --proto <filename>") + pp.WriteHelp(os.Stdout) os.Exit(-1) } @@ -46,24 +47,28 @@ func main() { os.Exit(-1) } - // you need --upbase and --lobase - if argv.Proto == "" { - pp.WriteHelp(os.Stdout) + if err := findGlobalAutogenpb(argv.Proto); err != nil { + log.Info("autogenpb parse error:", err) os.Exit(-1) } if !shell.Exists("go.sum") { - shell.Run([]string{"go", "mod", "init"}) - shell.Run([]string{"go", "mod", "tidy"}) + shell.RunQuiet([]string{"go", "mod", "init"}) + shell.RunQuiet([]string{"go", "mod", "tidy"}) + shell.RunQuiet([]string{"go", "mod", "edit", "-go=1.18"}) // TODO: make this a global } + // TODO: switch to using forgepb + // switch to forgepb + os.Setenv("GO111MODULE", "off") // keeps go list working if go version is back versioned for compatability cmd := []string{"go", "list", "-f", "'{{.Name}}'"} - result := shell.Run(cmd) + result := shell.RunQuiet(cmd) + os.Unsetenv("GO111MODULE") packageName := strings.Join(result.Stdout, "\n") packageName = strings.TrimSpace(packageName) packageName = strings.Trim(packageName, "'") - log.Info("packageName == ", packageName) + // log.Info("packageName == ", packageName) protobase := strings.TrimSuffix(argv.Proto, ".proto") @@ -112,11 +117,11 @@ func main() { // seems to work, but proto.Marshal() breaks with nil reference if argv.Mutex { if err := addMutex(sortmap); err == nil { - log.Info("adding mutex to existing protoc-gen-go file worked") + // log.Info("adding mutex to existing protoc-gen-go file worked") sortmap["mutex"] = "true" sortmap["lock"] = "all" } else { - log.Info("adding mutex to existing protoc-gen-go file did not work") + log.Info("adding mutex to existing protoc-gen-go file did not work", err) sortmap["mutex"] = "false" } } @@ -130,7 +135,7 @@ func main() { } if argv.NoSort { - log.Info("not making sort.pb.go file (--no-sort == true)") + // log.Info("not making sort.pb.go file (--no-sort == true)") } else { if len(uniqueKeys) != 0 { } @@ -138,10 +143,8 @@ func main() { } if argv.NoMarshal { - log.Info("not making marshal.pb.go file (--no-marshal == true)") + // log.Info("not making marshal.pb.go file (--no-marshal == true)") } else { - if len(marshalKeys) != 0 { - } // make the foo.marshal.pb.go file marshal(sortmap) } @@ -29,7 +29,7 @@ func marshal(names map[string]string) { fmt.Fprintln(w, "") for _, v := range marshalKeys { - log.Info("found marshal key in .proto", v) + // log.Info("found marshal key in .proto", v) marshalThing(w, v) } diff --git a/parseProtoFile.go b/parseProtoFile.go index 682234d..afc1e05 100644 --- a/parseProtoFile.go +++ b/parseProtoFile.go @@ -15,7 +15,7 @@ import ( // // adds fields to []marshal and []unique func findAutogenpb(names map[string]string) error { - log.Info("starting findAutogenpb() on", names["protofile"]) + // log.Info("starting findAutogenpb() on", names["protofile"]) // read in the .proto file data, err := os.ReadFile(names["protofile"]) if err != nil { @@ -30,15 +30,52 @@ func findAutogenpb(names map[string]string) error { parts := strings.Fields(line) if strings.Contains(line, "autogenpb:marshal") { newm := parts[1] - log.Info("found marshal", newm) + // log.Info("found marshal", newm) marshalKeys = append(marshalKeys, newm) } if strings.Contains(line, "autogenpb:unique") { newu := parts[1] newu = cases.Title(language.English, cases.NoLower).String(newu) - log.Info("found unique field", newu) + // log.Info("found unique field", newu) uniqueKeys = append(uniqueKeys, newu) } } return nil } + +func findGlobalAutogenpb(filename string) error { + // log.Info("starting findAutogenpb() on", filename) + // read in the .proto file + data, err := os.ReadFile(filename) + if err != nil { + // log.Info("open config file :", err) + return err + } + + lines := strings.Split(string(data), "\n") + for _, line := range lines { + if strings.Contains(line, "autogenpb:ignoreproto") { + // ignore this protofile completely (don't make foo.pb.go) + os.Exit(0) + } + if strings.Contains(line, "autogenpb:no-marshal") { + // don't marshal anything (don't make foo.marshal.pb.go) + argv.NoMarshal = true + } + if strings.Contains(line, "autogenpb:no-sort") { + // don't sort anything (don't make foo.sort.pb.go) + argv.NoSort = true + } + if strings.Contains(line, "autogenpb:mutex") { + // try the mutex hack + argv.Mutex = true + } + if strings.Contains(line, "autogenpb:gover:") { + // todo: parse the output here + parts := strings.Split(line, "autogenpb:gover:") + log.Info("found gover:", parts[1]) + argv.Mutex = true + } + } + return nil +} @@ -5,8 +5,6 @@ import ( "io" "os" "strings" - - "go.wit.com/log" ) func makeSortfile() { @@ -30,7 +28,7 @@ func makeSortfile() { } for _, s := range uniqueKeys { - log.Info("found unique key in .proto", s) + // log.Info("found unique key in .proto", s) sortmap["sortBy"] = s sortmap["sortKey"] = s |
