@@ -2,201 +2,199 @@ package main
22
33import (
44 "fmt"
5- "github.com/go-yaml/yaml"
65 "github.com/buger/cloud-ssh/provider"
6+ "github.com/go-yaml/yaml"
7+ "io/ioutil"
8+ "log"
79 "os"
810 "os/exec"
9- "log"
10- "strings"
11- "runtime"
12- "io/ioutil"
13- "regexp"
14- "strconv"
11+ "regexp"
12+ "runtime"
13+ "strconv"
14+ "strings"
1515)
1616
1717type CloudInstances map [string ]provider.Instances
1818type StrMap map [string ]string
1919type Config map [string ]StrMap
2020
2121func splitHostname (str string ) (user string , hostname string ) {
22- if arr := strings .Split (str , "@" ); len (arr ) > 1 {
23- return arr [0 ], arr [1 ]
24- } else {
25- return "" , str
26- }
22+ if arr := strings .Split (str , "@" ); len (arr ) > 1 {
23+ return arr [0 ], arr [1 ]
24+ } else {
25+ return "" , str
26+ }
2727}
2828
2929func joinHostname (user string , hostname string ) string {
30- if user != "" {
31- return user + "@" + hostname
32- } else {
33- return hostname
34- }
30+ if user != "" {
31+ return user + "@" + hostname
32+ } else {
33+ return hostname
34+ }
3535}
3636
3737func getTargetHostname (args []string ) (user string , hostname string , arg_idx int ) {
38- for idx , arg := range args {
39- if ! strings .HasPrefix (arg , "-" ) {
40- if idx == 0 {
41- hostname = arg
42- arg_idx = idx
43- break
44- } else {
45- if ! strings .HasPrefix (args [idx - 1 ], "-" ) {
46- hostname = arg
47- arg_idx = idx
48- break
49- }
50- }
51- }
52- }
53-
54- user , hostname = splitHostname (hostname )
55-
56- return
38+ for idx , arg := range args {
39+ if ! strings .HasPrefix (arg , "-" ) {
40+ if idx == 0 {
41+ hostname = arg
42+ arg_idx = idx
43+ break
44+ } else {
45+ if ! strings .HasPrefix (args [idx - 1 ], "-" ) {
46+ hostname = arg
47+ arg_idx = idx
48+ break
49+ }
50+ }
51+ }
52+ }
53+
54+ user , hostname = splitHostname (hostname )
55+
56+ return
5757}
5858
59- func getInstances (config Config ) (clouds CloudInstances ) {
60- clouds = make (CloudInstances )
61-
62- for name , cfg := range config {
63- for k ,v := range cfg {
64- cfg ["name" ] = name
65-
66- if k == "provider" {
67- switch v {
68- case "aws" :
69- clouds [name ] = provider .GetEC2Instances (cfg )
70- default :
71- log .Println ("Unknown provider: " , v )
72- }
73- }
74- }
75- }
76-
77- return
59+ func getInstances (config Config ) (clouds CloudInstances ) {
60+ clouds = make (CloudInstances )
61+
62+ for name , cfg := range config {
63+ for k , v := range cfg {
64+ cfg ["name" ] = name
65+
66+ if k == "provider" {
67+ switch v {
68+ case "aws" :
69+ clouds [name ] = provider .GetEC2Instances (cfg )
70+ default :
71+ log .Println ("Unknown provider: " , v )
72+ }
73+ }
74+ }
75+ }
76+
77+ return
7878}
7979
8080func userHomeDir () string {
81- if runtime .GOOS == "windows" {
82- home := os .Getenv ("HOMEDRIVE" ) + os .Getenv ("HOMEPATH" )
83- if home == "" {
84- home = os .Getenv ("USERPROFILE" )
85- }
86- return home
87- }
88- return os .Getenv ("HOME" )
81+ if runtime .GOOS == "windows" {
82+ home := os .Getenv ("HOMEDRIVE" ) + os .Getenv ("HOMEPATH" )
83+ if home == "" {
84+ home = os .Getenv ("USERPROFILE" )
85+ }
86+ return home
87+ }
88+ return os .Getenv ("HOME" )
8989}
9090
9191func readConfig () (config Config ) {
92- config = make (Config )
93-
94- prefferedPaths := []string {
95- "./cloud-ssh.yaml" ,
96- userHomeDir () + "/.ssh/cloud-ssh.yaml" ,
97- "/etc/cloud-ssh.yaml" ,
98- }
99-
100- var content []byte
101-
102- for _ , path := range prefferedPaths {
103- if _ , err := os .Stat (path ); err == nil {
104- fmt .Println ("Found config:" , path )
105- content , err = ioutil .ReadFile (path )
106-
107- if err != nil {
108- log .Fatal ("Error while reading config: " , err )
109- }
110- }
111- }
112-
113- if os .Getenv ("AWS_ACCESS_KEY_ID" ) != "" && os .Getenv ("AWS_SECRET_ACCESS_KEY" ) != "" {
114- config ["default" ] = make (StrMap )
115- config ["default" ]["access_key" ] = os .Getenv ("AWS_ACCESS_KEY_ID" )
116- config ["default" ]["secret_key" ] = os .Getenv ("AWS_SECRET_ACCESS_KEY" )
117- config ["default" ]["region" ] = os .Getenv ("AWS_REGION" )
118- config ["default" ]["provider" ] = "aws"
119- }
120-
121- if len (content ) == 0 {
122- if len (config ) == 0 {
123- fmt .Println ("Can't find any configuration or ENV variables. Check http://github.com/buger/cloud-ssh for documentation." )
124- }
125- return
126- } else if err := yaml .Unmarshal (content , & config ); err != nil {
127- log .Fatal (err )
128- }
129-
130- return
92+ config = make (Config )
93+
94+ prefferedPaths := []string {
95+ "./cloud-ssh.yaml" ,
96+ userHomeDir () + "/.ssh/cloud-ssh.yaml" ,
97+ "/etc/cloud-ssh.yaml" ,
98+ }
99+
100+ var content []byte
101+
102+ for _ , path := range prefferedPaths {
103+ if _ , err := os .Stat (path ); err == nil {
104+ fmt .Println ("Found config:" , path )
105+ content , err = ioutil .ReadFile (path )
106+
107+ if err != nil {
108+ log .Fatal ("Error while reading config: " , err )
109+ }
110+ }
111+ }
112+
113+ if os .Getenv ("AWS_ACCESS_KEY_ID" ) != "" && os .Getenv ("AWS_SECRET_ACCESS_KEY" ) != "" {
114+ config ["default" ] = make (StrMap )
115+ config ["default" ]["access_key" ] = os .Getenv ("AWS_ACCESS_KEY_ID" )
116+ config ["default" ]["secret_key" ] = os .Getenv ("AWS_SECRET_ACCESS_KEY" )
117+ config ["default" ]["region" ] = os .Getenv ("AWS_REGION" )
118+ config ["default" ]["provider" ] = "aws"
119+ }
120+
121+ if len (content ) == 0 {
122+ if len (config ) == 0 {
123+ fmt .Println ("Can't find any configuration or ENV variables. Check http://github.com/buger/cloud-ssh for documentation." )
124+ }
125+ return
126+ } else if err := yaml .Unmarshal (content , & config ); err != nil {
127+ log .Fatal (err )
128+ }
129+
130+ return
131131}
132132
133-
134133func getMatchedInstances (clouds CloudInstances , filter string ) (matched []StrMap ) {
135134
136- // Fuzzy matching (like SublimeText)
137- filter = strings .Join (strings .Split (filter ,"" ), ".*?" )
138-
139- rHost := regexp .MustCompile (filter )
140-
141- for cloud , instances := range clouds {
142- for addr , tags := range instances {
143- for _ , tag := range tags {
144- if rHost .MatchString (tag .Value ) {
145- matched = append (matched , StrMap {
146- "cloud" : cloud ,
147- "addr" : addr ,
148- "tag_name" : tag .Name ,
149- "tag_value" : tag .Value ,
150- })
151-
152- break
153- }
154- }
155- }
156- }
157-
158- return
135+ // Fuzzy matching (like SublimeText)
136+ filter = strings .Join (strings .Split (filter , "" ), ".*?" )
137+
138+ rHost := regexp .MustCompile (filter )
139+
140+ for cloud , instances := range clouds {
141+ for addr , tags := range instances {
142+ for _ , tag := range tags {
143+ if rHost .MatchString (tag .Value ) {
144+ matched = append (matched , StrMap {
145+ "cloud" : cloud ,
146+ "addr" : addr ,
147+ "tag_name" : tag .Name ,
148+ "tag_value" : tag .Value ,
149+ })
150+
151+ break
152+ }
153+ }
154+ }
155+ }
156+
157+ return
159158}
160159
161160func formatMatchedInstance (inst StrMap ) string {
162- return "Cloud: " + inst ["cloud" ] + "\t Matched by: " + inst ["tag_name" ] + "=" + inst ["tag_value" ] + "\t Addr: " + inst ["addr" ]
161+ return "Cloud: " + inst ["cloud" ] + "\t Matched by: " + inst ["tag_name" ] + "=" + inst ["tag_value" ] + "\t Addr: " + inst ["addr" ]
163162}
164163
165164func main () {
166- config := readConfig ()
167- instances := getInstances (config )
168-
169- args := os .Args [1 :len (os .Args )]
170-
171- user , hostname , arg_idx := getTargetHostname (args )
172-
173- match := getMatchedInstances (instances , hostname )
174-
175-
176- if len (match ) == 0 {
177- fmt .Println ("Can't find cloud instance, trying to connect anyway" )
178- } else if len (match ) == 1 {
179- hostname = match [0 ]["addr" ]
180- fmt .Println ("Found clound instance:" )
181- fmt .Println (formatMatchedInstance (match [0 ]))
182- } else {
183- fmt .Println ("Found multiple instances:" )
184- for i , host := range match {
185- fmt .Println (strconv .Itoa (i + 1 )+ ") " , formatMatchedInstance (host ))
186- }
187- fmt .Print ("Choose instance: " )
188-
189- var i int
190- _ , err := fmt .Scanf ("%d" , & i )
191-
192- if err != nil || i > len (match )+ 1 {
193- log .Fatal ("Wrong index" )
194- }
195-
196- hostname = match [i - 1 ]["addr" ]
197- }
198-
199- args [arg_idx ] = joinHostname (user , hostname )
165+ config := readConfig ()
166+ instances := getInstances (config )
167+
168+ args := os .Args [1 :len (os .Args )]
169+
170+ user , hostname , arg_idx := getTargetHostname (args )
171+
172+ match := getMatchedInstances (instances , hostname )
173+
174+ if len (match ) == 0 {
175+ fmt .Println ("Can't find cloud instance, trying to connect anyway" )
176+ } else if len (match ) == 1 {
177+ hostname = match [0 ]["addr" ]
178+ fmt .Println ("Found clound instance:" )
179+ fmt .Println (formatMatchedInstance (match [0 ]))
180+ } else {
181+ fmt .Println ("Found multiple instances:" )
182+ for i , host := range match {
183+ fmt .Println (strconv .Itoa (i + 1 )+ ") " , formatMatchedInstance (host ))
184+ }
185+ fmt .Print ("Choose instance: " )
186+
187+ var i int
188+ _ , err := fmt .Scanf ("%d" , & i )
189+
190+ if err != nil || i > len (match )+ 1 {
191+ log .Fatal ("Wrong index" )
192+ }
193+
194+ hostname = match [i - 1 ]["addr" ]
195+ }
196+
197+ args [arg_idx ] = joinHostname (user , hostname )
200198
201199 cmd := exec .Command ("ssh" , args ... )
202200 cmd .Stdin = os .Stdin
0 commit comments