forked from KurokuLabs/margo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mgcli.go
48 lines (41 loc) · 791 Bytes
/
mgcli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package mgcli
import (
"fmt"
"github.com/urfave/cli"
"os"
)
type Commands struct {
Name string
Build *cli.Command
Run *cli.Command
}
type App struct{ cli.App }
func (a *App) RunAndExitOnError() {
if err := a.Run(os.Args); err != nil {
fmt.Fprintln(a.ErrWriter, "error:", err)
os.Exit(1)
}
}
func Action(f cli.ActionFunc) cli.ActionFunc {
return func(c *cli.Context) error {
return Error("", f(c))
}
}
func NewApp() *App {
a := cli.NewApp()
a.Usage = ""
a.Version = ""
a.Writer = os.Stderr
a.ErrWriter = os.Stderr
return &App{App: *a}
}
func Error(message string, err error) error {
switch {
case err == nil:
return nil
case message == "":
return cli.NewExitError(err, 1)
default:
return cli.NewExitError(fmt.Sprintf("%s: %s", message, err), 1)
}
}