summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go68
1 files changed, 44 insertions, 24 deletions
diff --git a/main.go b/main.go
index 7c9db2d..e386101 100644
--- a/main.go
+++ b/main.go
@@ -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
}