@@ -2,137 +2,67 @@ package main
22
33import (
44 "fmt"
5- "github.com/buger/cloud-ssh/provider"
6- "github.com/go-yaml/yaml"
7- "io/ioutil"
85 "log"
96 "os"
107 "os/exec"
118 "regexp"
12- "runtime"
139 "strconv"
1410 "strings"
11+ "sync"
1512)
1613
17- type CloudInstances map [string ]provider.Instances
18- type StrMap map [string ]string
19- type Config map [string ]StrMap
20-
21- func 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- }
14+ type Tag struct {
15+ Name , Value string
2716}
2817
29- func joinHostname (user string , hostname string ) string {
30- if user != "" {
31- return user + "@" + hostname
32- } else {
33- return hostname
34- }
35- }
36-
37- func 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 )
18+ type Instances map [string ][]Tag
19+ type CloudInstances map [string ]Instances
5520
56- return
57- }
5821
5922func getInstances (config Config ) (clouds CloudInstances ) {
60- clouds = make (CloudInstances )
23+ clouds = make (CloudInstances )
24+
25+ var wg sync.WaitGroup
26+ var mux sync.RWMutex
6127
6228 for name , cfg := range config {
6329 for k , v := range cfg {
6430 cfg ["name" ] = name
31+ cfg ["provider" ] = k
6532
6633 if k == "provider" {
6734 switch v {
6835 case "aws" :
69- clouds [name ] = provider .GetEC2Instances (cfg )
36+ wg .Add (1 )
37+ go func (name string , cfg StrMap ){
38+ mux .Lock ()
39+ clouds [name ] = getEC2Instances (cfg )
40+ mux .Unlock ()
41+ wg .Done ()
42+ }(name , cfg )
43+ case "digital_ocean" :
44+ wg .Add (1 )
45+ go func (name string , cfg StrMap ){
46+ mux .Lock ()
47+ clouds [name ] = getDigitalOceanInstances (cfg )
48+ mux .Unlock ()
49+ wg .Done ()
50+ }(name , cfg )
7051 default :
7152 log .Println ("Unknown provider: " , v )
7253 }
7354 }
7455 }
7556 }
7657
77- return
78- }
79-
80- func 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" )
89- }
90-
91- func 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- }
58+ wg .Wait ()
12959
13060 return
13161}
13262
13363func getMatchedInstances (clouds CloudInstances , filter string ) (matched []StrMap ) {
13464
135- // Fuzzy matching ( like SublimeText)
65+ // Fuzzy matching, like SublimeText
13666 filter = strings .Join (strings .Split (filter , "" ), ".*?" )
13767
13868 rHost := regexp .MustCompile (filter )
0 commit comments