-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
91 lines (76 loc) · 1.76 KB
/
root.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cli
import (
"context"
"errors"
"fmt"
"time"
"github.com/carlmjohnson/versioninfo"
"github.com/futurice/jalapeno/pkg/ui/colors"
uiutil "github.com/futurice/jalapeno/pkg/ui/util"
"github.com/spf13/cobra"
)
var (
// https://goreleaser.com/cookbooks/using-main.version/
version string
)
type ExitCodeContextKey struct{}
// Execute runs the command and returns the exit code
func Execute(cmd *cobra.Command) int {
err := cmd.ExecuteContext(context.Background())
exitCode, isExitCodeSet := cmd.Context().Value(ExitCodeContextKey{}).(int)
if isExitCodeSet {
return exitCode
}
if err == nil {
return 0
} else {
return 1
}
}
func NewRootCmd() *cobra.Command {
var cmd = &cobra.Command{
Use: "jalapeno",
Short: "Create, manage and share spiced up project templates",
Long: "Create, manage and share spiced up project templates.",
SilenceUsage: true,
}
if version != "" {
cmd.Version = version
} else {
cmd.Version = fmt.Sprintf(
"%s (Built on %s from Git SHA %s)",
versioninfo.Version,
versioninfo.Revision,
versioninfo.LastCommit.Format(time.RFC3339),
)
}
cmd.AddCommand(
NewCheckCmd(),
NewCreateCmd(),
NewEjectCmd(),
NewExecuteCmd(),
NewPullCmd(),
NewPushCmd(),
NewTestCmd(),
NewUpgradeCmd(),
NewValidateCmd(),
NewWhyCmd(),
NewBumpVerCmd(),
)
return cmd
}
func errorHandler(cmd *cobra.Command, err error) error {
if err == nil {
return nil
}
// Print empty line before error message
cmd.Println()
// If the error is a user abort, don't print the error message
if errors.Is(err, uiutil.ErrUserAborted) {
cmd.Println("User aborted")
return nil
}
// Color the error message
cmd.SetErrPrefix(colors.Red.Render("Error:"))
return errors.New(colors.Red.Render(err.Error()))
}