/
types.go
214 lines (193 loc) · 5.43 KB
/
types.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package core
import (
"errors"
"io/ioutil"
"time"
"gopkg.in/yaml.v2"
)
// BasicServerConfig server basic config
type BasicServerConfig struct {
User string `yaml:"user,omitempty"`
Password string `yaml:"password,omitempty"`
PrivateKey string `yaml:"private_key,omitempty"`
PrivateKeyPassword string `yaml:"private_key_password,omitempty"`
KeyboardAuthCmd string `yaml:"keyboard_auth_cmd,omitempty"`
Environment map[string]string `yaml:"environment,omitempty"`
EnableAPI string `yaml:"enable_api,omitempty"`
ExtAuth string `yaml:"ext_auth,omitempty"`
Port int `yaml:"port,omitempty"`
ServerAliveInterval time.Duration `yaml:"server_alive_interval,omitempty"`
}
// Server server config
type Server struct {
Name string `yaml:"name"`
Address string `yaml:"address"`
Port int `yaml:"port,omitempty"`
User string `yaml:"user,omitempty"`
Proxy string `yaml:"proxy,omitempty"`
Password string `yaml:"password,omitempty"`
PrivateKey string `yaml:"private_key,omitempty"`
PrivateKeyPassword string `yaml:"private_key_password,omitempty"`
HookCmd string `yaml:"hook_cmd,omitempty"`
HookStdout bool `yaml:"hook_stdout,omitempty"`
KeyboardAuthCmd string `yaml:"keyboard_auth_cmd,omitempty"`
Environment map[string]string `yaml:"environment,omitempty"`
EnableAPI string `yaml:"enable_api,omitempty"`
ServerAliveInterval time.Duration `yaml:"server_alive_interval,omitempty"`
Tags []string `yaml:"tags,omitempty"`
ConfigPath string `yaml:"config_path,omitempty"`
}
// Tags server tags
type Tags []string
// Servers mmh servers
type Servers []*Server
func (servers Servers) Len() int {
return len(servers)
}
func (servers Servers) Less(i, j int) bool {
return servers[i].Name < servers[j].Name
}
func (servers Servers) Swap(i, j int) {
servers[i], servers[j] = servers[j], servers[i]
}
// Config context config(eg: default.yaml)
type Config struct {
configPath string
Basic BasicServerConfig `yaml:"basic,omitempty"`
MaxProxy int `yaml:"max_proxy,omitempty"`
Servers Servers `yaml:"servers"`
Tags Tags `yaml:"tags,omitempty"`
}
// SetConfigPath set config file path
func (cfg *Config) SetConfigPath(configPath string) {
cfg.configPath = configPath
}
// Write write config
func (cfg *Config) Write() error {
if cfg.configPath == "" {
return errors.New("config path not set")
}
out, err := yaml.Marshal(cfg)
if err != nil {
return err
}
return ioutil.WriteFile(cfg.configPath, out, 0644)
}
// WriteTo write config to yaml file
func (cfg *Config) WriteTo(filePath string) error {
if filePath == "" {
return errors.New("file path is empty")
}
cfg.configPath = filePath
return cfg.Write()
}
// Load load config
func (cfg *Config) Load() error {
if cfg.configPath == "" {
return errors.New("config path not set")
}
buf, err := ioutil.ReadFile(cfg.configPath)
if err != nil {
return err
}
err = yaml.Unmarshal(buf, cfg)
if err != nil {
return err
}
cfg.mergeBasic()
return nil
}
// LoadFrom load config from yaml file
func (cfg *Config) LoadFrom(filePath string) error {
if filePath == "" {
return errors.New("file path is empty")
}
cfg.configPath = filePath
return cfg.Load()
}
func (cfg *Config) mergeBasic() {
for _, s := range cfg.Servers {
s.ConfigPath = cfg.configPath
if s.User == "" {
s.User = cfg.Basic.User
if s.User == "" {
s.User = "root"
}
}
if s.Password == "" {
s.Password = cfg.Basic.Password
}
if s.PrivateKey == "" {
s.PrivateKey = cfg.Basic.PrivateKey
}
if s.PrivateKeyPassword == "" {
s.PrivateKeyPassword = cfg.Basic.PrivateKeyPassword
}
if s.KeyboardAuthCmd == "" {
s.KeyboardAuthCmd = cfg.Basic.KeyboardAuthCmd
}
if s.EnableAPI == "" {
s.EnableAPI = cfg.Basic.EnableAPI
}
if s.Environment == nil {
s.Environment = cfg.Basic.Environment
if s.Environment == nil {
s.Environment = make(map[string]string)
}
}
if s.Port == 0 {
s.Port = cfg.Basic.Port
if s.Port == 0 {
s.Port = 22
}
}
if s.ServerAliveInterval == 0 {
s.ServerAliveInterval = cfg.Basic.ServerAliveInterval
if s.ServerAliveInterval == 0 {
s.ServerAliveInterval = 10 * time.Second
}
}
}
}
type ConfigInfo struct {
Name string
Path string
IsCurrent bool
}
type ConfigList []ConfigInfo
func (info ConfigList) Len() int {
return len(info)
}
func (info ConfigList) Less(i, j int) bool {
return info[i].Name < info[j].Name
}
func (info ConfigList) Swap(i, j int) {
info[i], info[j] = info[j], info[i]
}
type KeyBoardRequest struct {
User string `json:"user"`
Instruction string `json:"instruction"`
Questions []string `json:"questions"`
Echos []bool `json:"echos"`
}
// ConfigExample context config example
func ConfigExample() *Config {
return &Config{
Basic: BasicServerConfig{
User: "root",
Password: "password",
},
Servers: Servers{
{
Name: "prod11",
Address: "10.10.4.11",
Proxy: "prod12",
},
{
Name: "prod12",
Address: "10.10.4.12",
},
},
MaxProxy: 5,
}
}