Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
cli error handling updated for snapctl
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi Khajeh committed Jul 8, 2016
1 parent 712a3ca commit b8534f9
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 188 deletions.
24 changes: 7 additions & 17 deletions cmd/snapctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -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
}
Expand All @@ -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")
Expand All @@ -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",
Expand Down
41 changes: 30 additions & 11 deletions cmd/snapctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
package main

import (
"errors"
"fmt"
"os"
"sort"
Expand All @@ -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"
Expand All @@ -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)
}
}
Expand All @@ -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
}
Expand Down
11 changes: 3 additions & 8 deletions cmd/snapctl/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/*
Expand Down Expand Up @@ -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)
}

/*
Expand Down
67 changes: 18 additions & 49 deletions cmd/snapctl/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -80,35 +74,26 @@ 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")
pName = ctx.String("plugin-name")
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")
Expand All @@ -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)
}
Expand All @@ -151,40 +132,29 @@ 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")
pName = ctx.String("plugin-name")
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")
Expand All @@ -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") {
Expand Down
Loading

0 comments on commit b8534f9

Please sign in to comment.