summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-01-08 19:45:48 -0600
committerJeff Carr <[email protected]>2025-01-08 19:45:48 -0600
commit3fe2fde185b950215008e67a50364ec741f2fd09 (patch)
tree25392d9c486a324afe61239d54b5e4395d3a3000 /main.go
parent0a1eb821e9123f4e6793b410300803c610d4b804 (diff)
make a protofile for the app itself
that turns out to be a lot of fun!
Diffstat (limited to 'main.go')
-rw-r--r--main.go85
1 files changed, 46 insertions, 39 deletions
diff --git a/main.go b/main.go
index b52f579..5ed9080 100644
--- a/main.go
+++ b/main.go
@@ -28,6 +28,9 @@ var uniqueKeys []string
func main() {
pp := arg.MustParse(&argv)
+ var pb *Files
+ pb = new(Files)
+
// you need a proto file
if argv.Proto == "" {
log.Info("you must provide --proto <filename>")
@@ -47,10 +50,9 @@ func main() {
os.Exit(-1)
}
- if err := findGlobalAutogenpb(argv.Proto); err != nil {
- log.Info("autogenpb parse error:", err)
- os.Exit(-1)
- }
+ f := new(File)
+ pb.Files = append(pb.Files, f)
+ f.Filename = argv.Proto
// todo, look for go.work files
if argv.GoSrc == "" {
@@ -65,45 +67,42 @@ func main() {
}
log.Info(argv.GoSrc, argv.GoPath)
+ pwd, _ := os.Getwd()
+ log.Info("pwd = ", pwd)
+
if !shell.Exists("go.sum") {
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
+ shell.RunQuiet([]string{"go", "mod", "edit", "-go=1.18"}) // TODO: pass this as ENV. verify protobuf version needed
}
+ var packageName string
var result cmd.Status
var cmd []string
- // if forge.IsGoWork() {
- if false {
- cmd = []string{"go", "work", "use"}
- result = shell.Run(cmd)
- log.Info(strings.Join(result.Stdout, "\n"))
- log.Info(strings.Join(result.Stderr, "\n"))
- if !shell.Exists("go.mod") {
- cmd = []string{"go", "mod", "init"}
- result = shell.Run(cmd)
- log.Info(strings.Join(result.Stdout, "\n"))
- log.Info(strings.Join(result.Stderr, "\n"))
- }
- cmd = []string{"go", "list", "-f", "'{{.Name}}'"}
- result = shell.Run(cmd)
- log.Info(strings.Join(result.Stdout, "\n"))
- log.Info(strings.Join(result.Stderr, "\n"))
- } else {
+ if argv.Package == "" {
// 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.RunQuiet(cmd)
os.Unsetenv("GO111MODULE")
- }
- packageName := strings.Join(result.Stdout, "\n")
- packageName = strings.TrimSpace(packageName)
- packageName = strings.Trim(packageName, "'")
- // log.Info("packageName == ", packageName)
+ packageName = strings.Join(result.Stdout, "\n")
+ packageName = strings.TrimSpace(packageName)
+ packageName = strings.Trim(packageName, "'")
+ // log.Info("packageName == ", packageName)
+ } else {
+ packageName = argv.Package
+ }
+ f.Package = packageName
protobase := strings.TrimSuffix(argv.Proto, ".proto")
+ f.Filebase = protobase
+
+ if err := pb.findGlobalAutogenpb(f); err != nil {
+ log.Info("autogenpb parse error:", err)
+ os.Exit(-1)
+ }
sortmap = make(map[string]string)
sortmap["package"] = packageName
@@ -132,32 +131,40 @@ func main() {
}
// parse sort & marshal options from the .proto file
+ // this goes through the .proto files and looks
+ // for `autogenpb: ` lines
if err := findAutogenpb(sortmap); err != nil {
log.Info("autogenpb parse error:", err)
os.Exit(-1)
}
// try to make foo.pb.go with protoc if it's not here
+ // this is helpful because the protoc-gen-go lines
+ // are also annoying to code by hand
sortmap["protoc"] = protobase + ".pb.go"
if !shell.Exists(sortmap["protoc"]) {
if err := protocBuild(sortmap); err != nil {
log.Info("protoc build error:", err)
os.Exit(-1)
}
+ pb.addMutex(f)
+ os.Exit(0)
- // experiment to add a mutex to the structs.
- // this might fix my other not so great lock implementation on sort (?)
- // 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")
- sortmap["mutex"] = "true"
- sortmap["lock"] = "all"
- } else {
- log.Info("adding mutex to existing protoc-gen-go file did not work", err)
- sortmap["mutex"] = "false"
+ /*
+ // experiment to add a mutex to the structs.
+ // this might fix my other not so great lock implementation on sort (?)
+ // 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")
+ sortmap["mutex"] = "true"
+ sortmap["lock"] = "all"
+ } else {
+ log.Info("adding mutex to existing protoc-gen-go file did not work", err)
+ sortmap["mutex"] = "false"
+ }
}
- }
+ */
}
// if foo.pb.go still doesn't exist, protoc failed