summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-16 08:36:24 -0500
committerJeff Carr <[email protected]>2025-10-16 08:36:24 -0500
commitf383ca1b9bcc3a23113f9d16179eff87c0afab69 (patch)
tree22dc8088faa7133bcab136a233963c4d9801e24d
parenta553f3eeee489d762a740f2416cc7906a2bdb6db (diff)
housecleaning
-rw-r--r--doProto.go203
-rw-r--r--main.go180
2 files changed, 203 insertions, 180 deletions
diff --git a/doProto.go b/doProto.go
new file mode 100644
index 0000000..579865c
--- /dev/null
+++ b/doProto.go
@@ -0,0 +1,203 @@
+// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
+// Use of this source code is governed by the GPL 3.0
+
+// protobuf the way I am using them, require GO 1.20. I think. I could be wrong.
+// The Go Protocol Buffers library embeds a sync.Mutex within the MessageState struct to prevent unintended shallow copies of message structs
+// this optionally (but it is the default) inserts a mutex into the struct generated by protoc
+
+// probably don't need these build lines anymore
+//go:build go1.20
+// +build go1.20
+
+// go:generate autogenpb --proto file.proto
+
+package main
+
+import (
+ "errors"
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "github.com/go-cmd/cmd"
+ "go.wit.com/lib/fhelp"
+ "go.wit.com/lib/gui/shell"
+ "go.wit.com/log"
+)
+
+func doProto(argvProto string) error {
+ pf := new(File)
+ me.pb.Files = append(me.pb.Files, pf)
+ pf.Filename = argvProto
+ pf.IterMap = make(map[string]string)
+
+ pf.Filebase = strings.TrimSuffix(argvProto, ".proto")
+
+ if argv.Clean {
+ doClean(pf.Filebase)
+ me.sh.GoodExit("doClean() ran")
+ }
+
+ if argv.Mtime {
+ doMtime(pf.Filebase)
+ me.sh.GoodExit("doClean() ran")
+ }
+
+ if argv.ReFormat {
+ protoReformatComments(argvProto)
+ // time.Sleep(5 * time.Second)
+ protoReformat(argvProto)
+ log.Info("format done")
+ okExit("")
+ }
+
+ if argv.Comments {
+ protoReformatComments(argvProto)
+ okExit("")
+ }
+
+ if argv.Regret {
+ // this will override the manditory Uuid checks
+ os.Setenv("PROTOBUF_REGRET", "true")
+ }
+
+ if doMtime(pf.Filebase) {
+ me.sh.GoodExit(pf.Filename + " did not change")
+ } else {
+ log.Info("ctime check: need to re-run autogenpb")
+ }
+
+ // parse sort & marshal options from the .proto file
+ // this goes through the .proto files and looks
+ // for `autogenpb: ` lines
+ if err := pf.protoParse(); err != nil {
+ log.Info("autogenpb parse error:", err)
+ badExit(err)
+ }
+
+ if !argv.NoFormat {
+ protoReformat(argvProto)
+ }
+
+ if pf.Bases == nil {
+ badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", pf.Filebase))
+ }
+ if pf.Base == nil {
+ badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", pf.Filebase))
+ }
+
+ // if you have gotten here, at least the .proto buf file is OK
+ if argv.DryRun {
+ // show the protobuf of the protobuf. It's like Inception
+ pf.printMsgTable()
+ okExit("")
+ }
+
+ // todo, look for go.work files
+ if argv.GoSrc == "" {
+ homeDir, _ := os.UserHomeDir()
+ argv.GoSrc = filepath.Join(homeDir, "go/src")
+ }
+
+ var startpwd string
+ startpwd, _ = os.Getwd()
+ log.Info("start pwd = ", startpwd)
+ if argv.GoPath == "" {
+ argv.GoPath = strings.TrimPrefix(startpwd, argv.GoSrc)
+ argv.GoPath = strings.Trim(argv.GoPath, "/")
+ }
+ log.Info(argv.GoSrc, argv.GoPath)
+
+ if !shell.Exists("go.sum") {
+ shell.RunQuiet([]string{"go-mod-clean"})
+ 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: pass this as ENV. verify protobuf version needed
+ }
+ }
+
+ var packageName string
+ var result cmd.Status
+ var cmd []string
+ if argv.Package == "" {
+ // TODO: switch to using forgepb (expose the functions/logic from forgepb directly
+ // it could be a bad idea to use forge.Init() here as forge needs this to build
+ // 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)
+ } else {
+ packageName = argv.Package
+ }
+ pf.Package = packageName
+
+ // 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
+ // checkCmd("protoc")
+ // checkCmd("protoc-gen-go")
+
+ if !fhelp.CheckProtoc() {
+ badExit(fmt.Errorf("you do not have 'protoc' installed"))
+ }
+
+ pf.Pbfilename = pf.Filebase + ".pb.go"
+ // try to create the foo.pb.go file using protoc if it is not there
+ if !shell.Exists(pf.Pbfilename) {
+ if !fhelp.CheckProtoc() {
+ badExit(fmt.Errorf("you do not have 'protoc' installed"))
+ }
+
+ // checkCmd("protoc")
+ // checkCmd("protoc-gen-go")
+
+ if err := me.pb.protocBuild(pf); err != nil {
+ badExit(err)
+ }
+
+ }
+ os.Chdir(startpwd)
+
+ // try to add the Mutex to the pb.go file
+ if err := me.pb.addMutex(pf); err != nil {
+ badExit(err)
+ }
+
+ // if foo.pb.go still doesn't exist, protoc failed
+ if !shell.Exists(pf.Pbfilename) {
+ log.Info("protoc build error.", pf.Pbfilename)
+ badExit(errors.New("failed to be created with protoc and proto-gen-go"))
+ }
+
+ // make the marshal.pb.go file
+ me.pb.marshal(pf)
+
+ // make the sort.pb.go file
+ if err := me.pb.makeNewSortfile(pf); err != nil {
+ badExit(err)
+ }
+
+ if pf.DoGui {
+ if err := me.pb.makeGuiFile(pf); err != nil {
+ badExit(err)
+ }
+ }
+
+ if pf.DoHTTP {
+ if err := me.pb.makeHTTPFile(pf); err != nil {
+ badExit(err)
+ }
+ }
+ log.Info("")
+ log.Info("you may have to run 'goimport -w *.go' on the new *pb.go files")
+ log.Info("")
+ return nil
+}
diff --git a/main.go b/main.go
index 993d88a..7c9db2d 100644
--- a/main.go
+++ b/main.go
@@ -14,13 +14,9 @@
package main
import (
- "errors"
- "fmt"
"os"
- "path/filepath"
"strings"
- "github.com/go-cmd/cmd"
"go.wit.com/lib/fhelp"
"go.wit.com/lib/gui/prep"
"go.wit.com/lib/gui/shell"
@@ -80,182 +76,6 @@ func main() {
doProto(argv.Proto)
}
-func doProto(argvProto string) error {
- pf := new(File)
- me.pb.Files = append(me.pb.Files, pf)
- pf.Filename = argvProto
- pf.IterMap = make(map[string]string)
-
- pf.Filebase = strings.TrimSuffix(argvProto, ".proto")
-
- if argv.Clean {
- doClean(pf.Filebase)
- me.sh.GoodExit("doClean() ran")
- }
-
- if argv.Mtime {
- doMtime(pf.Filebase)
- me.sh.GoodExit("doClean() ran")
- }
-
- if argv.ReFormat {
- protoReformatComments(argvProto)
- // time.Sleep(5 * time.Second)
- protoReformat(argvProto)
- log.Info("format done")
- okExit("")
- }
-
- if argv.Comments {
- protoReformatComments(argvProto)
- okExit("")
- }
-
- if argv.Regret {
- // this will override the manditory Uuid checks
- os.Setenv("PROTOBUF_REGRET", "true")
- }
-
- if doMtime(pf.Filebase) {
- me.sh.GoodExit(pf.Filename + " did not change")
- } else {
- log.Info("ctime check: need to re-run autogenpb")
- }
-
- // parse sort & marshal options from the .proto file
- // this goes through the .proto files and looks
- // for `autogenpb: ` lines
- if err := pf.protoParse(); err != nil {
- log.Info("autogenpb parse error:", err)
- badExit(err)
- }
-
- if !argv.NoFormat {
- protoReformat(argvProto)
- }
-
- if pf.Bases == nil {
- badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", pf.Filebase))
- }
- if pf.Base == nil {
- badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", pf.Filebase))
- }
-
- // if you have gotten here, at least the .proto buf file is OK
- if argv.DryRun {
- // show the protobuf of the protobuf. It's like Inception
- pf.printMsgTable()
- okExit("")
- }
-
- // todo, look for go.work files
- if argv.GoSrc == "" {
- homeDir, _ := os.UserHomeDir()
- argv.GoSrc = filepath.Join(homeDir, "go/src")
- }
-
- var startpwd string
- startpwd, _ = os.Getwd()
- log.Info("start pwd = ", startpwd)
- if argv.GoPath == "" {
- argv.GoPath = strings.TrimPrefix(startpwd, argv.GoSrc)
- argv.GoPath = strings.Trim(argv.GoPath, "/")
- }
- log.Info(argv.GoSrc, argv.GoPath)
-
- if !shell.Exists("go.sum") {
- shell.RunQuiet([]string{"go-mod-clean"})
- 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: pass this as ENV. verify protobuf version needed
- }
- }
-
- var packageName string
- var result cmd.Status
- var cmd []string
- if argv.Package == "" {
- // TODO: switch to using forgepb (expose the functions/logic from forgepb directly
- // it could be a bad idea to use forge.Init() here as forge needs this to build
- // 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)
- } else {
- packageName = argv.Package
- }
- pf.Package = packageName
-
- // 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
- // checkCmd("protoc")
- // checkCmd("protoc-gen-go")
-
- if !fhelp.CheckProtoc() {
- badExit(fmt.Errorf("you do not have 'protoc' installed"))
- }
-
- pf.Pbfilename = pf.Filebase + ".pb.go"
- // try to create the foo.pb.go file using protoc if it is not there
- if !shell.Exists(pf.Pbfilename) {
- if !fhelp.CheckProtoc() {
- badExit(fmt.Errorf("you do not have 'protoc' installed"))
- }
-
- // checkCmd("protoc")
- // checkCmd("protoc-gen-go")
-
- if err := me.pb.protocBuild(pf); err != nil {
- badExit(err)
- }
-
- }
- os.Chdir(startpwd)
-
- // try to add the Mutex to the pb.go file
- if err := me.pb.addMutex(pf); err != nil {
- badExit(err)
- }
-
- // if foo.pb.go still doesn't exist, protoc failed
- if !shell.Exists(pf.Pbfilename) {
- log.Info("protoc build error.", pf.Pbfilename)
- badExit(errors.New("failed to be created with protoc and proto-gen-go"))
- }
-
- // make the marshal.pb.go file
- me.pb.marshal(pf)
-
- // make the sort.pb.go file
- if err := me.pb.makeNewSortfile(pf); err != nil {
- badExit(err)
- }
-
- if pf.DoGui {
- if err := me.pb.makeGuiFile(pf); err != nil {
- badExit(err)
- }
- }
-
- if pf.DoHTTP {
- if err := me.pb.makeHTTPFile(pf); err != nil {
- badExit(err)
- }
- }
- log.Info("")
- log.Info("you may have to run 'goimport -w *.go' on the new *pb.go files")
- log.Info("")
- return nil
-}
-
func okExit(s string) {
log.Info("autogenpb ok", s)
os.Exit(0)