/
root.go
104 lines (87 loc) · 2.06 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
104
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"github.com/ariejan/zelda/core"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"github.com/spf13/cobra"
)
var (
cfgFile string
// RootCmd is where CLI commands come in
RootCmd = &cobra.Command{
Use: "zelda",
Short: "Zelda is a CLI to Hertek Connect Link",
Long: `Zelda is a CLI to Hertek Connect Link - a valid Integrator account with Hertek Connect is required.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
},
}
// API is what we use to communicate with the API
API *core.ConnectLinkAPI
)
// Execute the `link` command
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/zelda.yaml)")
}
func er(msg interface{}) {
fmt.Println("Error:", msg)
os.Exit(1)
}
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
configHome, err := homedir.Expand("~/.config")
if err != nil {
er(err)
}
viper.AddConfigPath(configHome)
viper.SetConfigName("zelda")
viper.SetConfigType("yaml")
}
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Cannot find config file at ~/.config/zelda.yaml. Please check the README.")
os.Exit(1)
} else {
fmt.Printf("--- Using config found at: %s\n", viper.ConfigFileUsed())
}
API = core.NewConnectLinkAPI(
viper.GetString("zelda.server_url"),
viper.GetString("zelda.username"),
viper.GetString("zelda.password"),
)
}
func prettyPrintJSON(data string) {
var formatted bytes.Buffer
err := json.Indent(&formatted, []byte(data), "", " ")
if err != nil {
log.Println("JSON parse error: ", err)
os.Exit(1)
}
fmt.Println("==> Formatted JSON response: ")
fmt.Print(string(formatted.Bytes()))
fmt.Println()
}
func parseIntArg(arg string) int {
result, err := strconv.Atoi(arg)
if err != nil {
er(err)
}
return result
}