From 8267d4202b76df0df63ad8ca2ab7ca7b2dcbdbd0 Mon Sep 17 00:00:00 2001 From: Roxie Gibson Date: Mon, 2 Aug 2021 00:57:05 +0100 Subject: [PATCH] feat: function to display help, error, & exit --- cli/app.go | 3 ++- cli/errors.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 cli/errors.go diff --git a/cli/app.go b/cli/app.go index 42f4c067..41123141 100644 --- a/cli/app.go +++ b/cli/app.go @@ -2,6 +2,7 @@ package cli import ( "context" + "errors" "fmt" "path" "sort" @@ -54,7 +55,7 @@ on your $PATH. Action: func(c *cli.Context) error { appType := c.Args().First() if appType == "" { - cli.ShowSubcommandHelp(c) + errorExit(c, "new", errors.New("no app type provided")) return nil } diff --git a/cli/errors.go b/cli/errors.go new file mode 100644 index 00000000..f4ffbc73 --- /dev/null +++ b/cli/errors.go @@ -0,0 +1,17 @@ +package cli + +import ( + "os" + + "github.com/sirupsen/logrus" + "github.com/urfave/cli/v2" +) + +// errorExit exits the program on error, logs the error to the terminal, and shows the help command. +// When passing command, use an empty string to get the base command, and for a subcommand, give the name of the subcommand +// Example: "" for `app new` gets the app help, "new" gets the `new` subcommand help +func errorExit(c *cli.Context, command string, err interface{}) { + cli.ShowCommandHelp(c, command) + logrus.Error(err) + os.Exit(1) +}