// 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" "os" "strings" "go.wit.com/lib/fhelp" "go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/argvpb" "go.wit.com/log" ) func main() { me = new(mainType) argvpb.Init(&argv, APPNAME, BUILDTIME, VERSION) // adds shell auto-complete me.pb = new(Files) var s string var err error if argv.Identify != "" { err = doIdentify(argv.Identify) } // check if it's ok to run autogenpb s, err = doPrecheck() if err == nil { // it's safe to run s, err = doProto(argv.Proto) } // safe exits back to your shell (with timing and toolkit close) if err != nil { argvpb.BadExit(s, err) } argvpb.GoodExit(s) } func okExit(s string) { log.Info("autogenpb ok", s) os.Exit(0) } func badExit(err error) { log.Info("autogenpb error:", err) os.Exit(-1) } func doPrecheck() (string, error) { var s string var err error // you need a proto file if argv.Proto == "" { // todo: run on every .proto file log.Info("todo: run on all .proto files") me.pp.WriteHelp(os.Stdout) return "you must provide --proto .proto", errors.New("need .proto") } if !shell.Exists(argv.Proto) { s = log.Sprintf("protobuf %s is missing", argv.Proto) return "missing file", errors.New(s) } if !strings.HasSuffix(argv.Proto, ".proto") { s = "wrote filetype" err = errors.New(log.Sprintf("protobuf %s doesn't end in '.proto'", argv.Proto)) return s, err } if path, err := fhelp.CheckCmd("goimports"); err != nil { log.Info("this tool requires goimports") if path != "" { log.Info("path might be:", path) } log.Info("this tool requires goimports") cmd := []string{"go", "install", "-v", "-x", "golang.org/x/tools/cmd/goimports@latest"} log.Info("Need to run:", cmd) if fhelp.QuestionUser("build goimports into ~/go/bin/ ?") { shell.RunRealtime(cmd) } else { s = "autogenpb missing goimports" err = errors.New("need goimports") } } return s, err }