/
cmd.go
123 lines (100 loc) · 3 KB
/
cmd.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package root
import (
"fmt"
"os"
"path/filepath"
homedir "github.com/mitchellh/go-homedir"
"github.com/mycontroller-org/server/v2/cmd/client/api"
clientTY "github.com/mycontroller-org/server/v2/pkg/types/client"
printer "github.com/mycontroller-org/server/v2/pkg/utils/printer"
"gopkg.in/yaml.v3"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
ENV_PREFIX = "MYC"
CONFIG_FILE_NAME = ".mycontroller"
CONFIG_FILE_EXT = "yaml"
)
var (
cfgFile string
CONFIG *clientTY.Config // keep MyController server details
IOStreams clientTY.IOStreams // read and write to this stream
HideHeader bool
Pretty bool
OutputFormat string
rootCliLong = `MyController Client
This client helps you to control your MyController server from the command line.
`
)
var Cmd = &cobra.Command{
Use: "myc",
Short: "myc",
Long: rootCliLong,
PreRun: func(cmd *cobra.Command, args []string) {
UpdateStreams(cmd)
},
}
func init() {
CONFIG = &clientTY.Config{}
cobra.OnInitialize(initConfig)
Cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.mycontroller.yaml)")
Cmd.PersistentFlags().StringVarP(&OutputFormat, "output", "o", printer.OutputConsole, "output format. options: yaml, json, console, wide")
Cmd.PersistentFlags().BoolVar(&HideHeader, "hide-header", false, "hides the header on the console output")
Cmd.PersistentFlags().BoolVar(&Pretty, "pretty", false, "JSON pretty print")
}
func GetClient() *api.Client {
return api.NewClient(CONFIG.URL, CONFIG.GetPassword(), CONFIG.Insecure)
}
func UpdateStreams(cmd *cobra.Command) {
cmd.SetOut(IOStreams.Out)
cmd.SetErr(IOStreams.ErrOut)
}
func Execute(streams clientTY.IOStreams) {
IOStreams = streams
if err := Cmd.Execute(); err != nil {
fmt.Fprintln(IOStreams.ErrOut, err)
os.Exit(1)
}
}
func WriteConfigFile() {
if cfgFile == "" {
return
}
if CONFIG == nil {
CONFIG = &clientTY.Config{}
}
// encode password field
CONFIG.EncodePassword()
configBytes, err := yaml.Marshal(CONFIG)
if err != nil {
fmt.Fprintf(IOStreams.ErrOut, "error on config file marshal. error:[%s]\n", err.Error())
}
err = os.WriteFile(cfgFile, configBytes, os.ModePerm)
if err != nil {
fmt.Fprintf(IOStreams.ErrOut, "error on writing config file to disk, filename:%s, error:[%s]\n", cfgFile, err.Error())
}
}
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.initConfig
home, err := homedir.Dir()
cobra.CheckErr(err)
// Search config in home directory with name ".myc" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(CONFIG_FILE_NAME)
viper.SetConfigType(CONFIG_FILE_EXT)
cfgFile = filepath.Join(home, fmt.Sprintf("%s.%s", CONFIG_FILE_NAME, CONFIG_FILE_EXT))
}
viper.SetEnvPrefix(ENV_PREFIX)
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
err = viper.Unmarshal(&CONFIG)
if err != nil {
fmt.Fprint(IOStreams.ErrOut, "error on unmarshal of config\n", err)
}
}
}