-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
config.go
185 lines (158 loc) · 3.85 KB
/
config.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package cmd
import (
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
// ConfigOptions is the config cmd option
type ConfigOptions struct {
ConfigFileLocation string
}
var configOptions ConfigOptions
func init() {
rootCmd.AddCommand(configCmd)
}
var configCmd = &cobra.Command{
Use: "config",
Aliases: []string{"cfg"},
Short: "Manage the config of jcli",
Long: `Manage the config of jcli`,
Run: func(_ *cobra.Command, _ []string) {
current := getCurrentJenkins()
if current.Description != "" {
fmt.Printf("Current Jenkins's name is %s, url is %s, description is %s\n", current.Name, current.URL, current.Description)
} else {
fmt.Printf("Current Jenkins's name is %s, url is %s\n", current.Name, current.URL)
}
},
Example: ` jcli config generate
jcli config list
jcli config edit`,
}
// JenkinsServer holds the configuration of your Jenkins
type JenkinsServer struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
UserName string `yaml:"username"`
Token string `yaml:"token"`
Proxy string `yaml:"proxy"`
ProxyAuth string `yaml:"proxyAuth"`
Description string `yaml:"description"`
}
// CommndHook is a hook
type CommndHook struct {
Path string `yaml:"path"`
Command string `yaml:"cmd"`
}
// PluginSuite define a suite of plugins
type PluginSuite struct {
Name string `yaml:"name"`
Plugins []string `yaml:"plugins"`
Description string `yaml:"description"`
}
// Config is a global config struct
type Config struct {
Current string `yaml:"current"`
JenkinsServers []JenkinsServer `yaml:"jenkins_servers"`
PreHooks []CommndHook `yaml:"preHooks"`
PostHooks []CommndHook `yaml:"postHooks"`
PluginSuites []PluginSuite `yaml:"pluginSuites"`
}
func setCurrentJenkins(name string) {
found := false
for _, jenkins := range getConfig().JenkinsServers {
if jenkins.Name == name {
found = true
break
}
}
if found {
config.Current = name
if err := saveConfig(); err != nil {
log.Fatal(err)
}
} else {
log.Fatalf("Cannot found Jenkins by name %s", name)
}
}
var config *Config
func getConfig() *Config {
return config
}
func getJenkinsNames() []string {
config := getConfig()
names := make([]string, 0)
for _, j := range config.JenkinsServers {
names = append(names, j.Name)
}
return names
}
func getCurrentJenkins() (jenkinsServer *JenkinsServer) {
config := getConfig()
if config != nil {
current := config.Current
jenkinsServer = findJenkinsByName(current)
}
return
}
func findJenkinsByName(name string) (jenkinsServer *JenkinsServer) {
for _, cfg := range config.JenkinsServers {
if cfg.Name == name {
jenkinsServer = &cfg
break
}
}
return
}
func findSuiteByName(name string) (suite *PluginSuite) {
for _, cfg := range config.PluginSuites {
if cfg.Name == name {
suite = &cfg
break
}
}
return
}
func loadDefaultConfig() (err error) {
userHome := userHomeDir()
configPath := fmt.Sprintf("%s/.jenkins-cli.yaml", userHome)
if _, err = os.Stat(configPath); err == nil {
err = loadConfig(configPath)
}
return
}
func loadConfig(path string) (err error) {
configOptions.ConfigFileLocation = path
var content []byte
if content, err = ioutil.ReadFile(path); err == nil {
err = yaml.Unmarshal([]byte(content), &config)
}
return
}
func saveConfig() (err error) {
var data []byte
config := getConfig()
if data, err = yaml.Marshal(&config); err == nil {
err = ioutil.WriteFile(configOptions.ConfigFileLocation, data, 0644)
}
return
}
func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
} else if runtime.GOOS == "linux" {
home := os.Getenv("XDG_CONFIG_HOME")
if home != "" {
return home
}
}
return os.Getenv("HOME")
}