-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
85 lines (71 loc) · 2.01 KB
/
command.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
package command
import (
"io"
"os"
cmdAuth "github.com/JulienBreux/mksctl/internal/mksctl/command/auth"
cmdImp "github.com/JulienBreux/mksctl/internal/mksctl/command/imp"
cmdRoot "github.com/JulienBreux/mksctl/internal/mksctl/command/root"
cmdTest "github.com/JulienBreux/mksctl/internal/mksctl/command/test"
cmdVersion "github.com/JulienBreux/mksctl/internal/mksctl/command/version"
"github.com/JulienBreux/mksctl/internal/mksctl/config"
"github.com/spf13/cobra"
)
const (
appName = "mksctl"
appShortDesc = "Microcks server CLI."
appLongDesc = "CLI for interacting with Microcks server."
appIssueURL = "https://github.com/JulienBreux/mksctl/issues/new?labels=bug"
)
var (
cmd = &cobra.Command{
Use: appName,
Short: appShortDesc,
Long: appLongDesc,
RunE: cmdRoot.Run,
}
)
type IOs struct {
In io.Reader
Out, Err io.Writer
}
// Execute executes command
func New(ios *IOs, args ...string) *cobra.Command {
defer recoverAndExit()
cobra.OnInitialize(initConfig)
cmd = initSetters(cmd, ios, args...)
cmd = initFlags(cmd)
cmd = initSubCommands(cmd)
return cmd
}
func initSetters(cmd *cobra.Command, ios *IOs, args ...string) *cobra.Command {
cmd.SetIn(ios.In)
cmd.SetOut(ios.Out)
cmd.SetErr(ios.Err)
cmd.SetArgs(args)
return cmd
}
func initFlags(cmd *cobra.Command) *cobra.Command {
cmd.Flags().StringVarP(&config.OverrideConfigFile, "config", "c", config.FullFilePath(), "configuration file")
cmd.PersistentFlags().StringP("output", "o", "", "Output format, one of 'yaml', 'json', 'toml' or 'xml'.")
return cmd
}
func initSubCommands(cmd *cobra.Command) *cobra.Command {
cmd.AddCommand(cmdVersion.New())
cmd.AddCommand(cmdAuth.New())
cmd.AddCommand(cmdImp.New())
cmd.AddCommand(cmdTest.New())
return cmd
}
func initConfig() {
if err := config.Init(); err != nil {
cmd.Printf("Error:\n%v", err)
os.Exit(1)
}
}
func recoverAndExit() {
if r := recover(); r != nil {
cmd.Println("Internal " + appName + " error")
cmd.Println("➡ Please report here: " + appIssueURL)
os.Exit(1)
}
}