From b8534f957e19686a499549782bbea50f0d8d3a9c Mon Sep 17 00:00:00 2001 From: Mehdi Khajeh Date: Sat, 9 Jul 2016 01:52:15 +0200 Subject: [PATCH] cli error handling updated for snapctl --- cmd/snapctl/config.go | 24 +++------- cmd/snapctl/main.go | 41 ++++++++++++----- cmd/snapctl/metric.go | 11 ++--- cmd/snapctl/plugin.go | 67 ++++++++-------------------- cmd/snapctl/task.go | 100 ++++++++++++++---------------------------- cmd/snapctl/tribe.go | 51 +++++++-------------- 6 files changed, 106 insertions(+), 188 deletions(-) diff --git a/cmd/snapctl/config.go b/cmd/snapctl/config.go index 0ce2f0d43..5ea120757 100644 --- a/cmd/snapctl/config.go +++ b/cmd/snapctl/config.go @@ -21,7 +21,6 @@ package main import ( "encoding/json" - "errors" "fmt" "io/ioutil" "os" @@ -43,11 +42,11 @@ type restAPIConfig struct { func (c *config) loadConfig(path string) error { b, err := ioutil.ReadFile(path) if err != nil { - return errors.New("Unable to read config. File might not exist.") + return fmt.Errorf("Unable to read config. File might not exist.") } err = json.Unmarshal(b, &c) if err != nil { - return errors.New("Invalid config") + return fmt.Errorf("Invalid config") } return nil } @@ -64,9 +63,7 @@ func getConfig(ctx *cli.Context) error { pname = pDetails[1] pver, err = strconv.Atoi(pDetails[2]) if err != nil { - fmt.Println("Can't convert version string to integer") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Can't convert version string to integer", ctx) } } else { ptyp = ctx.String("plugin-type") @@ -75,26 +72,19 @@ func getConfig(ctx *cli.Context) error { } if ptyp == "" { - fmt.Println("Must provide plugin type") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide plugin type", ctx) } if pname == "" { - fmt.Println("Must provide plugin name") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide plugin name", ctx) } if pver < 1 { - fmt.Println("Must provide plugin version") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide plugin version", ctx) } w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0) defer w.Flush() r := pClient.GetPluginConfig(ptyp, pname, strconv.Itoa(pver)) if r.Err != nil { - fmt.Println("Error requesting info: ", r.Err) - return errCritical + return fmt.Errorf("Error requesting info: ", r.Err) } printFields(w, false, 0, "NAME", diff --git a/cmd/snapctl/main.go b/cmd/snapctl/main.go index 3246cd57c..b8830a44c 100644 --- a/cmd/snapctl/main.go +++ b/cmd/snapctl/main.go @@ -20,7 +20,6 @@ limitations under the License. package main import ( - "errors" "fmt" "os" "sort" @@ -34,13 +33,29 @@ import ( ) var ( - gitversion string - pClient *client.Client - timeFormat = time.RFC1123 - err error - errCritical = errors.New("A critical error occured") + gitversion string + pClient *client.Client + timeFormat = time.RFC1123 + err error ) +type usageError struct { + s string + ctx *cli.Context +} + +func (ue usageError) Error() string { + return ue.s +} + +func (ue usageError) help() { + cli.ShowCommandHelp(ue.ctx, ue.ctx.Command.Name) +} + +func newUsageError(s string, ctx *cli.Context) usageError { + return usageError{s, ctx} +} + func main() { app := cli.NewApp() app.Name = "snapctl" @@ -50,7 +65,13 @@ func main() { app.Commands = append(commands, tribeCommands...) sort.Sort(ByCommand(app.Commands)) app.Before = beforeAction - if app.Run(os.Args) != nil { + + err := app.Run(os.Args) + if err != nil { + fmt.Println(err) + if ue, ok := err.(usageError); ok { + ue.help() + } os.Exit(1) } } @@ -60,14 +81,12 @@ func beforeAction(ctx *cli.Context) error { username, password := checkForAuth(ctx) pClient, err = client.New(ctx.String("url"), ctx.String("api-version"), ctx.Bool("insecure")) if err != nil { - fmt.Println(err) - return errCritical + return fmt.Errorf("%v", err) } pClient.Password = password pClient.Username = username if err = checkTribeCommand(ctx); err != nil { - fmt.Println(err) - return errCritical + return fmt.Errorf("%v", err) } return nil } diff --git a/cmd/snapctl/metric.go b/cmd/snapctl/metric.go index a6d053b2f..7931f7178 100644 --- a/cmd/snapctl/metric.go +++ b/cmd/snapctl/metric.go @@ -50,8 +50,7 @@ func listMetrics(ctx *cli.Context) error { } mts := pClient.FetchMetrics(ns, ver) if mts.Err != nil { - fmt.Printf("Error getting metrics: %v\n", mts.Err) - return errCritical + return fmt.Errorf("Error getting metrics: %v\n", mts.Err) } /* @@ -97,17 +96,13 @@ func listMetrics(ctx *cli.Context) error { func getMetric(ctx *cli.Context) error { if !ctx.IsSet("metric-namespace") { - fmt.Println("namespace is required") - fmt.Println("") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("namespace is required\n\n", ctx) } ns := ctx.String("metric-namespace") ver := ctx.Int("metric-version") metric := pClient.GetMetric(ns, ver) if metric.Err != nil { - fmt.Println(metric.Err) - return errCritical + return fmt.Errorf("%v", metric.Err) } /* diff --git a/cmd/snapctl/plugin.go b/cmd/snapctl/plugin.go index 355c96d7c..420d1a170 100644 --- a/cmd/snapctl/plugin.go +++ b/cmd/snapctl/plugin.go @@ -35,27 +35,21 @@ func loadPlugin(ctx *cli.Context) error { pAsc := ctx.String("plugin-asc") var paths []string if len(ctx.Args()) != 1 { - fmt.Println("Incorrect usage:") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage:", ctx) } paths = append(paths, ctx.Args().First()) if pAsc != "" { if !strings.Contains(pAsc, ".asc") { - fmt.Println("Must be a .asc file for the -a flag") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must be a .asc file for the -a flag", ctx) } paths = append(paths, pAsc) } r := pClient.LoadPlugin(paths) if r.Err != nil { if r.Err.Fields()["error"] != nil { - fmt.Printf("Error loading plugin:\n%v\n%v\n", r.Err.Error(), r.Err.Fields()["error"]) - } else { - fmt.Printf("Error loading plugin:\n%v\n", r.Err.Error()) + return fmt.Errorf("Error loading plugin:\n%v\n%v\n", r.Err.Error(), r.Err.Fields()["error"]) } - return errCritical + return fmt.Errorf("Error loading plugin:\n%v\n", r.Err.Error()) } for _, p := range r.LoadedPlugins { fmt.Println("Plugin loaded") @@ -80,9 +74,7 @@ func unloadPlugin(ctx *cli.Context) error { pName = pDetails[1] pVer, err = strconv.Atoi(pDetails[2]) if err != nil { - fmt.Println("Can't convert version string to integer") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Can't convert version string to integer", ctx) } } else { pType = ctx.String("plugin-type") @@ -90,25 +82,18 @@ func unloadPlugin(ctx *cli.Context) error { pVer = ctx.Int("plugin-version") } if pType == "" { - fmt.Println("Must provide plugin type") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide plugin type", ctx) } if pName == "" { - fmt.Println("Must provide plugin name") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide plugin name", ctx) } if pVer < 1 { - fmt.Println("Must provide plugin version") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide plugin version", ctx) } r := pClient.UnloadPlugin(pType, pName, pVer) if r.Err != nil { - fmt.Printf("Error unloading plugin:\n%v\n", r.Err.Error()) - return errCritical + return fmt.Errorf("Error unloading plugin:\n%v\n", r.Err.Error()) } fmt.Println("Plugin unloaded") @@ -124,16 +109,12 @@ func swapPlugins(ctx *cli.Context) error { pAsc := ctx.String("plugin-asc") var paths []string if len(ctx.Args()) < 1 || len(ctx.Args()) > 2 { - fmt.Println("Incorrect usage:") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage:", ctx) } paths = append(paths, ctx.Args().First()) if pAsc != "" { if !strings.Contains(pAsc, ".asc") { - fmt.Println("Must be a .asc file for the -a flag") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must be a .asc file for the -a flag", ctx) } paths = append(paths, pAsc) } @@ -151,14 +132,10 @@ func swapPlugins(ctx *cli.Context) error { pName = pDetails[1] pVer, err = strconv.Atoi(pDetails[2]) if err != nil { - fmt.Println("Can't convert version string to integer") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Can't convert version string to integer", ctx) } } else { - fmt.Println("Missing type, name, or version") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Missing type, name, or version", ctx) } } else { pType = ctx.String("plugin-type") @@ -166,25 +143,18 @@ func swapPlugins(ctx *cli.Context) error { pVer = ctx.Int("plugin-version") } if pType == "" { - fmt.Println("Must provide plugin type") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide plugin type", ctx) } if pName == "" { - fmt.Println("Must provide plugin name") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide plugin name", ctx) } if pVer < 1 { - fmt.Println("Must provide plugin version") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide plugin version", ctx) } r := pClient.SwapPlugin(paths, pType, pName, pVer) if r.Err != nil { - fmt.Printf("Error swapping plugins:\n%v\n", r.Err.Error()) - return errCritical + return fmt.Errorf("Error swapping plugins:\n%v\n", r.Err.Error()) } fmt.Println("Plugin loaded") @@ -205,8 +175,7 @@ func swapPlugins(ctx *cli.Context) error { func listPlugins(ctx *cli.Context) error { plugins := pClient.GetPlugins(ctx.Bool("running")) if plugins.Err != nil { - fmt.Printf("Error: %v\n", plugins.Err) - return errCritical + return fmt.Errorf("Error: %v\n", plugins.Err) } w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0) if ctx.Bool("running") { diff --git a/cmd/snapctl/task.go b/cmd/snapctl/task.go index a39293d1e..970aefcdf 100644 --- a/cmd/snapctl/task.go +++ b/cmd/snapctl/task.go @@ -94,9 +94,7 @@ func createTask(ctx *cli.Context) error { fmt.Println("Using workflow manifest to create task") createTaskUsingWFManifest(ctx) } else { - fmt.Println("Must provide either --task-manifest or --workflow-manifest arguments") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Must provide either --task-manifest or --workflow-manifest arguments", ctx) } return nil @@ -107,8 +105,7 @@ func createTaskUsingTaskManifest(ctx *cli.Context) error { ext := filepath.Ext(path) file, e := ioutil.ReadFile(path) if e != nil { - fmt.Printf("File error [%s]- %v\n", ext, e) - return errCritical + return fmt.Errorf("File error [%s]- %v\n", ext, e) } t := task{} @@ -116,24 +113,20 @@ func createTaskUsingTaskManifest(ctx *cli.Context) error { case ".yaml", ".yml": e = yaml.Unmarshal(file, &t) if e != nil { - fmt.Printf("Error parsing YAML file input - %v\n", e) - return errCritical + return fmt.Errorf("Error parsing YAML file input - %v\n", e) } case ".json": e = json.Unmarshal(file, &t) if e != nil { - fmt.Printf("Error parsing JSON file input - %v\n", e) - return errCritical + return fmt.Errorf("Error parsing JSON file input - %v\n", e) } default: - fmt.Printf("Unsupported file type %s\n", ext) - return errCritical + return fmt.Errorf("Unsupported file type %s\n", ext) } t.Name = ctx.String("name") if t.Version != 1 { - fmt.Println("Invalid version provided") - return errCritical + return fmt.Errorf("Invalid version provided") } // If the number of failures does not specific, default value is 10 @@ -146,11 +139,11 @@ func createTaskUsingTaskManifest(ctx *cli.Context) error { if r.Err != nil { errors := strings.Split(r.Err.Error(), " -- ") - fmt.Println("Error creating task:") + errString := "Error creating task:" for _, err := range errors { - fmt.Printf("%v\n", err) + errString += fmt.Sprintf("%v\n", err) } - return errCritical + return fmt.Errorf(errString) } fmt.Println("Task created") fmt.Printf("ID: %s\n", r.ID) @@ -167,12 +160,10 @@ func createTaskUsingWFManifest(ctx *cli.Context) error { file, e := ioutil.ReadFile(path) if !ctx.IsSet("interval") && !ctx.IsSet("i") { - fmt.Println("Workflow manifest requires interval to be set via flag.") - return errCritical + return fmt.Errorf("Workflow manifest requires interval to be set via flag.") } if e != nil { - fmt.Printf("File error [%s]- %v\n", ext, e) - return errCritical + return fmt.Errorf("File error [%s]- %v\n", ext, e) } var wf *wmap.WorkflowMap @@ -181,15 +172,13 @@ func createTaskUsingWFManifest(ctx *cli.Context) error { // e = yaml.Unmarshal(file, &t) wf, e = wmap.FromYaml(file) if e != nil { - fmt.Printf("Error parsing YAML file input - %v\n", e) - return errCritical + return fmt.Errorf("Error parsing YAML file input - %v\n", e) } case ".json": wf, e = wmap.FromJson(file) // e = json.Unmarshal(file, &t) if e != nil { - fmt.Printf("Error parsing JSON file input - %v\n", e) - return errCritical + return fmt.Errorf("Error parsing JSON file input - %v\n", e) } } // Get the task name @@ -202,8 +191,7 @@ func createTaskUsingWFManifest(ctx *cli.Context) error { // try interpreting interval as cron entry _, e := cron.Parse(i) if e != nil { - fmt.Printf("Bad interval format:\nfor simple schedule: %v\nfor cron schedule: %v\n", err, e) - return errCritical + return fmt.Errorf("Bad interval format:\nfor simple schedule: %v\nfor cron schedule: %v\n", err, e) } isCron = true } @@ -219,8 +207,7 @@ func createTaskUsingWFManifest(ctx *cli.Context) error { if ctx.IsSet("duration") && !isCron { d, err := time.ParseDuration(ctx.String("duration")) if err != nil { - fmt.Printf("Bad duration format:\n%v\n", err) - return errCritical + return fmt.Errorf("Bad duration format:\n%v\n", err) } start := time.Now().Add(createTaskNowPad) stop := start.Add(d) @@ -257,8 +244,7 @@ func createTaskUsingWFManifest(ctx *cli.Context) error { if ctx.IsSet("duration") { d, err := time.ParseDuration(ctx.String("duration")) if err != nil { - fmt.Printf("Bad duration format:\n%v\n", err) - return errCritical + return fmt.Errorf("Bad duration format:\n%v\n", err) } // if start is set and stop is not then use duration to create stop if start != nil && stop == nil { @@ -282,11 +268,11 @@ func createTaskUsingWFManifest(ctx *cli.Context) error { r := pClient.CreateTask(sch, wf, name, dl, !ctx.IsSet("no-start"), maxFailures) if r.Err != nil { errors := strings.Split(r.Err.Error(), " -- ") - fmt.Println("Error creating task:") + errString := "Error creating task:" for _, err := range errors { - fmt.Printf("%v\n", err) + errString += fmt.Sprintf("%v\n", err) } - return errCritical + return fmt.Errorf(errString) } fmt.Println("Task created") fmt.Printf("ID: %s\n", r.ID) @@ -328,8 +314,7 @@ func mergeDateTime(tm, dt string) *time.Time { func listTask(ctx *cli.Context) error { tasks := pClient.GetTasks() if tasks.Err != nil { - fmt.Printf("Error getting tasks:\n%v\n", tasks.Err) - return errCritical + return fmt.Errorf("Error getting tasks:\n%v\n", tasks.Err) } w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0) @@ -362,17 +347,14 @@ func listTask(ctx *cli.Context) error { func watchTask(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Print("Incorrect usage\n") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage", ctx) } verbose := ctx.Bool("verbose") id := ctx.Args().First() r := pClient.WatchTask(id) if r.Err != nil { - fmt.Println(r.Err) - return errCritical + return fmt.Errorf("%v", r.Err) } fmt.Printf("Watching Task (%s):\n", id) @@ -462,9 +444,7 @@ func watchTask(ctx *cli.Context) error { func startTask(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Print("Incorrect usage\n") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage", ctx) } id := ctx.Args().First() @@ -475,8 +455,7 @@ func startTask(ctx *cli.Context) error { fmt.Printf("ID: %s\n", id) return nil } - fmt.Printf("Error starting task:\n%v\n", r.Err) - return errCritical + return fmt.Errorf("Error starting task:\n%v\n", r.Err) } fmt.Println("Task started:") fmt.Printf("ID: %s\n", r.ID) @@ -486,9 +465,7 @@ func startTask(ctx *cli.Context) error { func stopTask(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Print("Incorrect usage\n") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage", ctx) } id := ctx.Args().First() @@ -499,8 +476,7 @@ func stopTask(ctx *cli.Context) error { fmt.Printf("ID: %s\n", id) os.Exit(0) } - fmt.Printf("Error stopping task:\n%v\n", r.Err) - return errCritical + return fmt.Errorf("Error stopping task:\n%v\n", r.Err) } fmt.Println("Task stopped:") fmt.Printf("ID: %s\n", r.ID) @@ -510,16 +486,13 @@ func stopTask(ctx *cli.Context) error { func removeTask(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Print("Incorrect usage\n") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage", ctx) } id := ctx.Args().First() r := pClient.RemoveTask(id) if r.Err != nil { - fmt.Printf("Error stopping task:\n%v\n", r.Err) - return errCritical + return fmt.Errorf("Error stopping task:\n%v\n", r.Err) } fmt.Println("Task removed:") fmt.Printf("ID: %s\n", r.ID) @@ -529,20 +502,16 @@ func removeTask(ctx *cli.Context) error { func exportTask(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Print("Incorrect usage\n") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage", ctx) } id := ctx.Args().First() task := pClient.GetTask(id) if task.Err != nil { - fmt.Printf("Error exporting task:\n%v\n", task.Err) - return errCritical + return fmt.Errorf("Error exporting task:\n%v\n", task.Err) } tb, err := json.Marshal(task) if err != nil { - fmt.Printf("Error exporting task:\n%v\n", err) - return errCritical + return fmt.Errorf("Error exporting task:\n%v\n", err) } fmt.Println(string(tb)) return nil @@ -550,16 +519,13 @@ func exportTask(ctx *cli.Context) error { func enableTask(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Print("Incorrect usage\n") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage", ctx) } id := ctx.Args().First() r := pClient.EnableTask(id) if r.Err != nil { - fmt.Printf("Error enabling task:\n%v\n", r.Err) - return errCritical + return fmt.Errorf("Error enabling task:\n%v\n", r.Err) } fmt.Println("Task enabled:") fmt.Printf("ID: %s\n", r.ID) diff --git a/cmd/snapctl/tribe.go b/cmd/snapctl/tribe.go index 32831864b..0cf451821 100644 --- a/cmd/snapctl/tribe.go +++ b/cmd/snapctl/tribe.go @@ -34,8 +34,7 @@ import ( func listMembers(ctx *cli.Context) error { resp := pClient.ListMembers() if resp.Err != nil { - fmt.Printf("Error getting members:\n%v\n", resp.Err) - return errCritical + return fmt.Errorf("Error getting members:\n%v\n", resp.Err) } if len(resp.Members) > 0 { @@ -56,15 +55,12 @@ func listMembers(ctx *cli.Context) error { func showMember(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Println("Incorrect usage:") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage:", ctx) } resp := pClient.GetMember(ctx.Args().First()) if resp.Err != nil { - fmt.Printf("Error:\n%v\n", resp.Err) - return errCritical + return fmt.Errorf("Error:\n%v\n", resp.Err) } w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0) @@ -85,8 +81,7 @@ func showMember(ctx *cli.Context) error { } tags, err := json.Marshal(resp.Tags) if err != nil { - fmt.Printf("Error:\n%v\n", err) - return errCritical + return fmt.Errorf("Error:\n%v\n", err) } values := []interface{}{resp.Name, resp.PluginAgreement, tasks.String()} @@ -102,8 +97,7 @@ func showMember(ctx *cli.Context) error { func listAgreements(ctx *cli.Context) error { resp := pClient.ListAgreements() if resp.Err != nil { - fmt.Printf("Error getting members:\n%v\n", resp.Err) - return errCritical + return fmt.Errorf("Error getting members:\n%v\n", resp.Err) } printAgreements(resp.Agreements) return nil @@ -111,15 +105,12 @@ func listAgreements(ctx *cli.Context) error { func createAgreement(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Println("Incorrect usage:") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage:", ctx) } resp := pClient.AddAgreement(ctx.Args().First()) if resp.Err != nil { - fmt.Printf("Error creating agreement: %v\n", resp.Err) - return errCritical + return fmt.Errorf("Error creating agreement: %v\n", resp.Err) } printAgreements(resp.Agreements) return nil @@ -127,15 +118,12 @@ func createAgreement(ctx *cli.Context) error { func deleteAgreement(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Println("Incorrect usage:") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage:", ctx) } resp := pClient.DeleteAgreement(ctx.Args().First()) if resp.Err != nil { - fmt.Printf("Error: %v\n", resp.Err) - return errCritical + return fmt.Errorf("Error: %v\n", resp.Err) } printAgreements(resp.Agreements) return nil @@ -143,15 +131,12 @@ func deleteAgreement(ctx *cli.Context) error { func joinAgreement(ctx *cli.Context) error { if len(ctx.Args()) != 2 { - fmt.Println("Incorrect usage:") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage:", ctx) } resp := pClient.JoinAgreement(ctx.Args().First(), ctx.Args().Get(1)) if resp.Err != nil { - fmt.Printf("Error: %v\n", resp.Err) - return errCritical + return fmt.Errorf("Error: %v\n", resp.Err) } printAgreements(map[string]*agreement.Agreement{resp.Agreement.Name: resp.Agreement}) return nil @@ -159,15 +144,12 @@ func joinAgreement(ctx *cli.Context) error { func leaveAgreement(ctx *cli.Context) error { if len(ctx.Args()) != 2 { - fmt.Println("Incorrect usage:") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage:", ctx) } resp := pClient.LeaveAgreement(ctx.Args().First(), ctx.Args().Get(1)) if resp.Err != nil { - fmt.Printf("Error: %v\n", resp.Err) - return errCritical + return fmt.Errorf("Error: %v\n", resp.Err) } printAgreements(map[string]*agreement.Agreement{resp.Agreement.Name: resp.Agreement}) return nil @@ -175,15 +157,12 @@ func leaveAgreement(ctx *cli.Context) error { func agreementMembers(ctx *cli.Context) error { if len(ctx.Args()) != 1 { - fmt.Println("Incorrect usage:") - cli.ShowCommandHelp(ctx, ctx.Command.Name) - return errCritical + return newUsageError("Incorrect usage:", ctx) } resp := pClient.GetAgreement(ctx.Args().First()) if resp.Err != nil { - fmt.Printf("Error: %v\n", resp.Err) - return errCritical + return fmt.Errorf("Error: %v\n", resp.Err) } w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)