-
Notifications
You must be signed in to change notification settings - Fork 5
/
root.go
103 lines (96 loc) · 2.53 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
92
93
94
95
96
97
98
99
100
101
102
103
package cli
import (
"fmt"
"os"
"path"
"github.com/blang/semver"
"github.com/containerum/chkit/pkg/cli/clisetup"
"github.com/containerum/chkit/pkg/cli/login"
"github.com/containerum/chkit/pkg/cli/mode"
"github.com/containerum/chkit/pkg/cli/prerun"
"github.com/containerum/chkit/pkg/cli/set"
"github.com/containerum/chkit/pkg/configdir"
"github.com/containerum/chkit/pkg/configuration"
"github.com/containerum/chkit/pkg/context"
"github.com/containerum/chkit/pkg/util/angel"
"github.com/spf13/cobra"
)
var VERSION = ""
func Root() error {
ctx := &context.Context{
Version: func() string {
// try to normalise version string
v, err := semver.ParseTolerant(VERSION)
if err != nil {
return VERSION
}
return v.String()
}(),
ConfigDir: configdir.ConfigDir(),
ConfigPath: path.Join(configdir.ConfigDir(), "config.toml"),
}
root := &cobra.Command{
Use: "chkit",
Short: "Chkit is a terminal client for containerum.io powerful API",
Version: ctx.Version,
PreRun: func(cmd *cobra.Command, args []string) {
clisetup.Config.DebugRequests = true
clisetup.SetupLogs(ctx)
if cmd.Flag("username").Changed && cmd.Flag("password").Changed {
if err := login.Setup(ctx); err != nil {
angel.Angel(ctx, err)
os.Exit(1)
}
return
} else if cmd.Flag("username").Changed || cmd.Flag("password").Changed {
cmd.Help()
os.Exit(1)
}
if err := prerun.PreRun(ctx); err != nil {
angel.Angel(ctx, err)
os.Exit(1)
}
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
PostRun: func(cmd *cobra.Command, args []string) {
if !ctx.Changed {
return
}
if err := configuration.SyncConfig(ctx); err != nil {
fmt.Printf("Unable to save config file: %v\n", err)
}
},
TraverseChildren: true,
}
ctx.Client.APIaddr = mode.API_ADDR
root.PersistentFlags().
StringVarP(&ctx.Client.Username, "username", "u", "", "account username")
root.PersistentFlags().
StringVarP(&ctx.Client.Password, "password", "p", "", "account password")
root.PersistentFlags().
StringVarP(&ctx.Namespace, "namespace", "n", ctx.Namespace, "")
root.PersistentFlags().
BoolVarP(&ctx.Quiet, "quiet", "q", ctx.Quiet, "quiet mode")
root.AddCommand(
login.Login(ctx),
Get(ctx),
Delete(ctx),
Create(ctx),
Replace(ctx),
set.Set(ctx),
Logs(ctx),
Run(ctx),
Rename(ctx),
Update(ctx),
&cobra.Command{
Use: "version",
Short: "Print version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(ctx.Version)
},
},
)
return root.Execute()
}