// 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" "path/filepath" "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) } if argv.Proto == "" { // try to process every .proto file globPattern := "*.proto" var files []string files, err = filepath.Glob(globPattern) if err != nil { fmt.Println("glob error", err, files) } if len(files) == 0 { err = errors.New("autogenpb found no .proto files") } else { for _, file := range files { fmt.Println("checking protobuf file:", file) s, err = doProto(file) if err != nil { break } } } } else { // user is trying a specific proto file // check if it's ok to run autogenpb s, err = doPrecheck(argv.Proto) 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 doPrecheck(protofile string) (string, error) { var s string var err error if !shell.Exists(protofile) { s = log.Sprintf("protobuf %s is missing", protofile) return "missing file", errors.New(s) } if !strings.HasSuffix(protofile, ".proto") { s = "wrote filetype" err = errors.New(log.Sprintf("protobuf %s doesn't end in '.proto'", protofile)) 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 }