diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2017-05-07 21:09:31 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2017-05-07 21:09:31 +0200 |
| commit | 385645d1a0bb9ae618e92d2e7a2ea6fe256b5ba2 (patch) | |
| tree | a3dc1b31c22db47e515f541e87fbfc2e8bb872f4 | |
| parent | a54004fbd960e4851f1f249ce0d030cf56b20f1b (diff) | |
| download | gofu-385645d1a0bb9ae618e92d2e7a2ea6fe256b5ba2.tar.gz | |
bubble up exit codes instead of calling os.Exit() in functions
| -rw-r--r-- | main.go | 22 | ||||
| -rw-r--r-- | pkg/rtree/main.go | 42 |
2 files changed, 29 insertions, 35 deletions
@@ -23,34 +23,28 @@ import ( "os" "path/filepath" - "github.com/majewsky/gofu/pkg/cli" "github.com/majewsky/gofu/pkg/rtree" ) func main() { - execApplet(filepath.Base(os.Args[0]), os.Args[1:], true) + os.Exit(execApplet(filepath.Base(os.Args[0]), os.Args[1:], true)) } -func execApplet(applet string, args []string, allowGofu bool) { +func execApplet(applet string, args []string, allowGofu bool) int { //allow explicit specification of applet as "./build/gofu <applet> <args>" if allowGofu && applet == "gofu" { if len(args) == 0 { fmt.Fprintln(os.Stderr, "Usage: gofu <applet> [args...]") - os.Exit(1) + return 1 } - execApplet(args[0], args[1:], false) - return + return execApplet(args[0], args[1:], false) } switch applet { case "rtree": - rtree.Exec(args) - case "test": - choice, _ := cli.Query("Which is the best editor?", - cli.Choice{Shortcut: 'v', Text: "vim"}, - cli.Choice{Shortcut: 'e', Text: "emacs"}, - cli.Choice{Text: "atom"}, - ) - fmt.Printf("You selected %s. Good choice!\n", choice.Text) + return rtree.Exec(args) + default: + fmt.Fprintln(os.Stderr, "ERROR: unknown applet: "+applet) + return 255 } } diff --git a/pkg/rtree/main.go b/pkg/rtree/main.go index b77d6ca..d57b399 100644 --- a/pkg/rtree/main.go +++ b/pkg/rtree/main.go @@ -27,9 +27,9 @@ import ( //Exec executes the rtree applet and does not return. The argument is os.Args //minus the leading "rtree" or "gofu rtree". -func Exec(args []string) { +func Exec(args []string) int { if len(args) == 0 { - usageAndExit() + return usage() } index, errs := ReadIndex() @@ -37,65 +37,64 @@ func Exec(args []string) { for _, err := range errs { util.ShowError(err) } - os.Exit(255) + return 255 } var err error switch args[0] { case "get": if len(args) != 2 { - usageAndExit() + return usage() } err = commandGet(index, args[1]) case "drop": if len(args) != 2 { - usageAndExit() + return usage() } err = commandDrop(index, args[1]) case "index": if len(args) != 1 { - usageAndExit() + return usage() } err = commandIndex(index) case "repos": if len(args) != 1 { - usageAndExit() + return usage() } commandRepos(index) case "remotes": if len(args) != 1 { - usageAndExit() + return usage() } commandRemotes(index) case "import": if len(args) != 2 { - usageAndExit() + return usage() } err = commandImport(index, args[1]) case "each": if len(args) < 2 { - usageAndExit() + return usage() } - commandEach(index, args[1], args[2:]) + return commandEach(index, args[1], args[2:]) default: - usageAndExit() + return usage() } if err == nil { - os.Exit(0) - } else { - util.ShowError(err) - os.Exit(1) + return 0 } + util.ShowError(err) + return 1 } -func usageAndExit() { +func usage() int { fmt.Fprintln(os.Stderr, "Usage:") fmt.Fprintln(os.Stderr, " rtree [get|drop] <url>") fmt.Fprintln(os.Stderr, " rtree [index|repos|remotes]") fmt.Fprintln(os.Stderr, " rtree import <path>") fmt.Fprintln(os.Stderr, " rtree each <command>") - os.Exit(1) + return 1 } func commandGet(index *Index, url string) error { @@ -145,7 +144,7 @@ func commandRemotes(index *Index) { util.ShowSorted(items) } -func commandEach(index *Index, command string, args []string) { +func commandEach(index *Index, command string, args []string) int { allOK := true for _, repo := range index.Repos { ok := repo.InteractiveExec(command, args...) @@ -154,9 +153,10 @@ func commandEach(index *Index, command string, args []string) { } } - if !allOK { - os.Exit(1) + if allOK { + return 0 } + return 1 } func commandImport(index *Index, dirPath string) error { |
