@@ -3,6 +3,7 @@ package config
33import (
44 "fmt"
55 "regexp"
6+ "sort"
67 "strconv"
78 "strings"
89)
@@ -25,10 +26,16 @@ type Host struct {
2526}
2627
2728func (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
3135func (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+
68127func (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}
0 commit comments