-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathroot.go
102 lines (90 loc) · 2.13 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
/*
Copyright © 2024 Jozu.com
*/
package cmd
import (
"kitops/pkg/cmd/build"
"kitops/pkg/cmd/export"
"kitops/pkg/cmd/list"
"kitops/pkg/cmd/login"
"kitops/pkg/cmd/pull"
"kitops/pkg/cmd/push"
"kitops/pkg/cmd/version"
"os"
"os/user"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type (
RootOptions struct {
ConfigHome string
}
RootFlags struct {
ConfigHome string
}
)
var (
shortDesc = `KitOps model manager`
longDesc = `KitOps is a tool to manage AI and ML models`
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = newRootCmd()
func init() {
rootCmd.AddCommand(build.NewCmdBuild())
rootCmd.AddCommand(login.NewCmdLogin())
rootCmd.AddCommand(pull.PullCommand())
rootCmd.AddCommand(push.PushCommand())
rootCmd.AddCommand(list.ListCommand())
rootCmd.AddCommand(export.ExportCommand())
rootCmd.AddCommand(version.NewCmdVersion())
}
func newRootCmd() *cobra.Command {
flags := &RootFlags{}
cmd := &cobra.Command{
Use: "kit",
Short: shortDesc,
Long: longDesc,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
options, err := flags.ToOptions()
if err != nil {
panic(err)
}
err = options.Complete()
if err != nil {
panic(err)
}
},
}
flags.addFlags(cmd)
return cmd
}
func (f *RootFlags) addFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&f.ConfigHome, "config", "", "config file (default is $HOME/.kitops)")
viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config"))
}
func (f *RootFlags) ToOptions() (*RootOptions, error) {
return &RootOptions{
ConfigHome: f.ConfigHome,
}, nil
}
func (o *RootOptions) Complete() error {
if o.ConfigHome == "" {
currentUser, err := user.Current()
if err != nil {
return err
}
configpath := filepath.Join(currentUser.HomeDir, ".kitops")
viper.Set("config", configpath)
o.ConfigHome = configpath
}
return nil
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}