diff options
| author | Jeff Carr <[email protected]> | 2025-10-16 09:01:11 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-16 09:01:11 -0500 |
| commit | 579745b6634c4195c3bc071941c0870dd1bfa129 (patch) | |
| tree | 4cc37039dbd2c3f1c8209f696b75e40905a33766 /main.go | |
| parent | f383ca1b9bcc3a23113f9d16179eff87c0afab69 (diff) | |
automation using the excellent 'goimports' toolv0.5.27
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 68 |
1 files changed, 44 insertions, 24 deletions
@@ -14,6 +14,7 @@ package main import ( + "errors" "os" "strings" @@ -33,29 +34,58 @@ func main() { me = new(mainType) me.sh = prep.Autocomplete(&argv) // adds shell auto complete to go-args me.pb = new(Files) + var s string + var err error if argv.Identify != "" { - if err := doIdentify(argv.Identify); err != nil { - badExit(err) - } - okExit("") + 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 { + me.sh.BadExit(s, err) + } + me.sh.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 == "" { - log.Info("you must provide --proto <filename>") + // todo: run on every .proto file + log.Info("todo: run on all .proto files") me.sh.WriteHelp() - os.Exit(-1) + return "you must provide --proto <protoname>.proto", errors.New("need .proto") } if !shell.Exists(argv.Proto) { - log.Info("protobuf", argv.Proto, "is missing") - os.Exit(-1) + s = log.Sprintf("protobuf %s is missing", argv.Proto) + return "missing file", errors.New(s) } if !strings.HasSuffix(argv.Proto, ".proto") { - log.Info("protobuf", argv.Proto, "must end in .proto") - os.Exit(-1) + 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 { @@ -66,22 +96,12 @@ func main() { 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?") { + if fhelp.QuestionUser("build goimports into ~/go/bin/ ?") { shell.RunRealtime(cmd) } else { - os.Exit(-1) + s = "autogenpb missing goimports" + err = errors.New("need goimports") } } - - doProto(argv.Proto) -} - -func okExit(s string) { - log.Info("autogenpb ok", s) - os.Exit(0) -} - -func badExit(err error) { - log.Info("autogenpb error:", err) - os.Exit(-1) + return s, err } |
