Skip to content

Commit 7085432

Browse files
committed
Add support for default username
1 parent 4d74b11 commit 7085432

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
cloud-ssh.yaml
3+
4+
Dockerfile

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func readConfig() (config Config) {
4242
if err != nil {
4343
log.Fatal("Error while reading config: ", err)
4444
}
45+
46+
break
4547
}
4648
}
4749

main.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func getInstances(config Config) (clouds CloudInstances) {
2626

2727
for name, cfg := range config {
2828
for k, v := range cfg {
29-
3029
if k == "provider" {
3130
switch v {
3231
case "aws":
@@ -58,7 +57,6 @@ func getInstances(config Config) (clouds CloudInstances) {
5857
}
5958

6059
func getMatchedInstances(clouds CloudInstances, filter string) (matched []StrMap) {
61-
6260
// Fuzzy matching, like SublimeText
6361
filter = strings.Join(strings.Split(filter, ""), ".*?")
6462

@@ -70,7 +68,7 @@ func getMatchedInstances(clouds CloudInstances, filter string) (matched []StrMap
7068
if rHost.MatchString(cloud + tag.Value) {
7169
matched = append(matched, StrMap{
7270
"cloud": cloud,
73-
"addr": addr,
71+
"addr": addr,
7472
"tag_name": tag.Name,
7573
"tag_value": tag.Value,
7674
})
@@ -98,14 +96,13 @@ func main() {
9896

9997
match := getMatchedInstances(instances, hostname)
10098

99+
var matched_instance map[string]string
100+
101101
if len(match) == 0 {
102102
fmt.Println("Can't find cloud instance, trying to connect anyway")
103103
} else if len(match) == 1 {
104-
hostname = match[0]["addr"]
105-
fmt.Println("Found clound instance:")
106-
fmt.Println(formatMatchedInstance(match[0]))
104+
matched_instance = match[0]
107105
} else {
108-
fmt.Println("Found multiple instances:")
109106
for i, host := range match {
110107
fmt.Println(strconv.Itoa(i+1)+") ", formatMatchedInstance(host))
111108
}
@@ -118,10 +115,26 @@ func main() {
118115
log.Fatal("Wrong index")
119116
}
120117

121-
hostname = match[i-1]["addr"]
118+
matched_instance = match[i-1]
122119
}
123120

124-
args[arg_idx] = joinHostname(user, hostname)
121+
if matched_instance != nil {
122+
hostname = matched_instance["addr"]
123+
default_user := config[matched_instance["cloud"]]["default_user"]
124+
125+
if len(user) == 0 && len(default_user) > 0 {
126+
user = default_user
127+
}
128+
129+
fmt.Println("Connecting to instance:")
130+
fmt.Println(formatMatchedInstance(matched_instance))
131+
}
132+
133+
if len(args) == 0 {
134+
args = append(args, joinHostname(user, hostname))
135+
} else {
136+
args[arg_idx] = joinHostname(user, hostname)
137+
}
125138

126139
cmd := exec.Command("ssh", args...)
127140
cmd.Stdin = os.Stdin

0 commit comments

Comments
 (0)