Skip to content

Commit 07604dc

Browse files
committed
add default user, port, timeout config
1 parent 0fa1a78 commit 07604dc

File tree

5 files changed

+81
-29
lines changed

5 files changed

+81
-29
lines changed

cmd/host.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func addHostInit() {
5353

5454
addHostCmd.Flags().Bool("help", false, "help for this command.")
5555
addHostCmd.Flags().StringVarP(&hostName, "name", "n", "", "host name")
56-
addHostCmd.Flags().StringVarP(&hostUser, "user", "u", "root", "login username")
56+
addHostCmd.Flags().StringVarP(&hostUser, "user", "u", "", "login username")
5757
addHostCmd.Flags().StringVarP(&hostIP, "host", "h", "", "remote host ip")
58-
addHostCmd.Flags().Uint16VarP(&hostPort, "port", "p", 22, "remote host port")
58+
addHostCmd.Flags().Uint16VarP(&hostPort, "port", "p", 0, "remote host port")
5959
addHostCmd.Flags().StringVarP(&hostJump, "jump", "j", "", "proxy jump")
6060
addHostCmd.Flags().StringVarP(&hostTags, "tags", "t", "", "tags")
6161
addHostCmd.Flags().IntVarP(&hostTimeout, "timeout", "", 0, "timeout")

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func initConfig() {
108108
viper.SetConfigType("yaml")
109109

110110
if err := viper.ReadInConfig(); err == nil {
111-
if err := config.InitConfig(); err != nil {
111+
if err := config.LoadConfig(); err != nil {
112112
fmt.Printf("load config file failed! %s err: %s", viper.ConfigFileUsed(), err.Error())
113113
os.Exit(1)
114114
}

config/config.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ import (
1616
)
1717

1818
type ConfigType struct {
19-
ModulesDir string `yaml:"modulesDir"`
20-
SSHAuthSock string `yaml:"sshAuthSock"`
21-
DefaultJump string `yaml:"defaultJump"`
19+
ModulesDir string `yaml:"modulesDir,omitempty"`
20+
SSHAuthSock string `yaml:"sshAuthSock,omitempty"`
21+
DefaultTimeout int `yaml:"defaultTimeout,omitempty"`
22+
DefaultUser string `yaml:"defaultUser,omitempty"`
23+
DefaultPort uint16 `yaml:"defaultPort,omitempty"`
24+
DefaultJump string `yaml:"defaultJump,omitempty"`
2225
Hosts map[string]*Host `yaml:"hosts"`
2326
Parallel int `yaml:"-"`
2427
OverlayTimeout int `yaml:"-"`
@@ -40,7 +43,7 @@ func getSSHAuthSock() (sock string) {
4043
return sock
4144
}
4245

43-
func InitConfig() error {
46+
func LoadConfig() error {
4447
if err := viper.Unmarshal(Config); err != nil {
4548
return err
4649
}
@@ -51,6 +54,7 @@ func InitConfig() error {
5154

5255
for name, host := range Config.Hosts {
5356
host.Name = name
57+
host.TagsFormat()
5458
if err := host.Parse(); err != nil {
5559
return err
5660
}
@@ -107,7 +111,7 @@ func ConfigHostsFilter(name string, user string, tags string) (hosts []*Host, er
107111

108112
hasTags := false
109113
for _, tag := range strings.Split(tags, ",") {
110-
if strings.Contains(host.Tags, tag) {
114+
if tag == "all" || strings.Contains(host.Tags, tag) {
111115
hasTags = true
112116
break
113117
}

config/host.go

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"fmt"
55
"regexp"
6+
"sort"
67
"strconv"
78
"strings"
89
)
@@ -25,10 +26,16 @@ type Host struct {
2526
}
2627

2728
func (host *Host) EndPoint() string {
29+
if host.Port == 0 {
30+
return host.Host
31+
}
2832
return fmt.Sprintf("%v:%v", host.Host, host.Port)
2933
}
3034

3135
func (host *Host) String() string {
36+
if host.User == "" {
37+
return host.EndPoint()
38+
}
3239
return fmt.Sprintf("%v@%v", host.User, host.EndPoint())
3340
}
3441

@@ -45,7 +52,40 @@ func (host *Host) Row() string {
4552
host.Name, host.Host, host.User, host.Port, host.Jump, host.Tags, host.Timeout)
4653
}
4754

48-
func (host *Host) Overlay() {
55+
func (host *Host) SetDefaultValue() {
56+
if host.User == "" {
57+
if Config.DefaultUser != "" {
58+
host.User = Config.DefaultUser
59+
} else {
60+
host.User = "root"
61+
}
62+
}
63+
64+
if host.Port == 0 {
65+
if Config.DefaultPort > 0 {
66+
host.Port = Config.DefaultPort
67+
} else {
68+
host.Port = 22
69+
}
70+
}
71+
72+
if host.Jump == "" && Config.DefaultJump != "" {
73+
host.Jump = Config.DefaultJump
74+
host.JumpList = Config.JumpHosts
75+
}
76+
77+
if host.Timeout < 0 {
78+
if Config.DefaultTimeout >= 0 {
79+
host.Timeout = Config.DefaultTimeout
80+
} else {
81+
host.Timeout = 0
82+
}
83+
}
84+
}
85+
86+
func (host *Host) SetOverlayValue() {
87+
host.SetDefaultValue()
88+
4989
if Config.OverlayUser != "" {
5090
host.User = Config.OverlayUser
5191
}
@@ -65,6 +105,25 @@ func (host *Host) Overlay() {
65105
}
66106
}
67107

108+
func (host *Host) TagsFormat() {
109+
var tags []string
110+
for _, tag := range strings.Split(host.Tags, ",") {
111+
if tag != "" && tag != "all" {
112+
existed := false
113+
for _, tag_ := range tags {
114+
if tag == tag_ {
115+
existed = true
116+
break
117+
}
118+
}
119+
if existed == false {
120+
tags = append(tags, tag)
121+
}
122+
}
123+
}
124+
host.Tags = strings.Join(sort.StringSlice(tags), ",")
125+
}
126+
68127
func (host *Host) parse(isJump bool) (err error) {
69128
// user, host
70129
if parts := strings.Split(host.Addr, "@"); len(parts) > 2 {
@@ -75,9 +134,6 @@ func (host *Host) parse(isJump bool) (err error) {
75134
} else {
76135
host.Host = host.Addr
77136
}
78-
if host.User == "" {
79-
host.User = "root"
80-
}
81137

82138
if host.Host == "" {
83139
return fmt.Errorf("Host required non-empty string.")
@@ -96,9 +152,6 @@ func (host *Host) parse(isJump bool) (err error) {
96152
host.Port = uint16(port)
97153
}
98154
}
99-
if host.Port == 0 {
100-
host.Port = 22
101-
}
102155

103156
// tags
104157
if err := CheckTags(host.Tags); err != nil {
@@ -112,7 +165,7 @@ func (host *Host) parse(isJump bool) (err error) {
112165

113166
// jump
114167
if !isJump {
115-
if host.Jump == "" && host.Jump != "none" {
168+
if host.Jump != "" && host.Jump != "none" {
116169
for _, hostString := range strings.Split(host.Jump, ",") {
117170
if len(hostString) == 0 {
118171
continue
@@ -129,9 +182,8 @@ func (host *Host) parse(isJump bool) (err error) {
129182
}
130183
}
131184

132-
// timeout
133-
if host.Timeout < 0 {
134-
host.Timeout = 0
185+
if isJump {
186+
host.SetDefaultValue()
135187
}
136188
return err
137189
}
@@ -156,17 +208,12 @@ func AddHost(name string, user string, ip string, port uint16, jump string, tags
156208
if _, exist := Config.Hosts[name]; exist {
157209
return fmt.Errorf("host name \"%s\" already existed!", name)
158210
}
159-
if user == "" {
160-
user = "root"
161-
}
162-
if port == 0 {
163-
port = 22
164-
}
165-
if timeout < 0 {
166-
timeout = 0
167-
}
168211
host := &Host{Name: name, User: user, Host: ip, Port: port, Jump: jump, Tags: tags, Timeout: timeout}
169212
host.Addr = host.String()
213+
host.TagsFormat()
214+
if host.Timeout < 0 {
215+
host.Timeout = 0
216+
}
170217
Config.Hosts[name] = host
171218
return saveConfig()
172219
}
@@ -198,6 +245,7 @@ func UpdateHost(name string, user string, ip string, port uint16, jump string, t
198245
host.Timeout = timeout
199246
}
200247
host.Addr = host.String()
248+
host.TagsFormat()
201249
Config.Hosts[name] = host
202250
return saveConfig()
203251
}

ssh/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (cfg *SSHConfigType) start(targets []string) error {
115115
}
116116

117117
for _, task := range cfg.Tasks {
118-
task.Target.Overlay()
118+
task.Target.SetOverlayValue()
119119
message := fmt.Sprintf("-----> [%d / %d] %s %s <-----",
120120
task.Index+1, len(cfg.Tasks), task.Target.String(), task.Message)
121121
terminalWidth := GetTerminalWidth()

0 commit comments

Comments
 (0)