Skip to content

Commit f7a1229

Browse files
committed
Should include cloudname to search
1 parent 0bb69d2 commit f7a1229

File tree

5 files changed

+175
-177
lines changed

5 files changed

+175
-177
lines changed

arguments_parser.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
package main
22

33
import (
4-
"strings"
4+
"strings"
55
)
66

77
func splitHostname(str string) (user string, hostname string) {
8-
if arr := strings.Split(str, "@"); len(arr) > 1 {
9-
return arr[0], arr[1]
10-
} else {
11-
return "", str
12-
}
8+
if arr := strings.Split(str, "@"); len(arr) > 1 {
9+
return arr[0], arr[1]
10+
} else {
11+
return "", str
12+
}
1313
}
1414

1515
func joinHostname(user string, hostname string) string {
16-
if user != "" {
17-
return user + "@" + hostname
18-
} else {
19-
return hostname
20-
}
16+
if user != "" {
17+
return user + "@" + hostname
18+
} else {
19+
return hostname
20+
}
2121
}
2222

2323
// Go though arguments, and find one with host
2424
func getTargetHostname(args []string) (user string, hostname string, arg_idx int) {
25-
for idx, arg := range args {
26-
if !strings.HasPrefix(arg, "-") {
27-
if idx == 0 {
28-
hostname = arg
29-
arg_idx = idx
30-
break
31-
} else {
32-
if !strings.HasPrefix(args[idx-1], "-") {
33-
hostname = arg
34-
arg_idx = idx
35-
break
36-
}
37-
}
38-
}
39-
}
25+
for idx, arg := range args {
26+
if !strings.HasPrefix(arg, "-") {
27+
if idx == 0 {
28+
hostname = arg
29+
arg_idx = idx
30+
break
31+
} else {
32+
if !strings.HasPrefix(args[idx-1], "-") {
33+
hostname = arg
34+
arg_idx = idx
35+
break
36+
}
37+
}
38+
}
39+
}
4040

41-
user, hostname = splitHostname(hostname)
41+
user, hostname = splitHostname(hostname)
4242

43-
return
44-
}
43+
return
44+
}

aws.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
11
package main
22

33
import (
4-
"log"
5-
"launchpad.net/goamz/aws"
6-
"launchpad.net/goamz/ec2"
4+
"launchpad.net/goamz/aws"
5+
"launchpad.net/goamz/ec2"
6+
"log"
77
)
88

99
func getEC2Instances(config map[string]string) (instances Instances) {
10-
instances = make(Instances)
10+
instances = make(Instances)
1111

12-
if _,ok := config["access_key"]; !ok {
13-
log.Fatal("Missing access_key for ", config["name"], " AWS cloud")
14-
}
12+
if _, ok := config["access_key"]; !ok {
13+
log.Fatal("Missing access_key for ", config["name"], " AWS cloud")
14+
}
1515

16-
if _,ok := config["secret_key"]; !ok {
17-
log.Fatal("Missing secret_key for ", config["name"], " AWS cloud")
18-
}
16+
if _, ok := config["secret_key"]; !ok {
17+
log.Fatal("Missing secret_key for ", config["name"], " AWS cloud")
18+
}
1919

20-
if _,ok := config["region"]; !ok {
21-
config["region"] = "us-east-1"
22-
}
20+
if _, ok := config["region"]; !ok {
21+
config["region"] = "us-east-1"
22+
}
2323

24-
auth := aws.Auth{config["access_key"], config["secret_key"]}
24+
auth := aws.Auth{config["access_key"], config["secret_key"]}
2525

26-
e := ec2.New(auth, aws.Regions[config["region"]])
27-
resp, err := e.Instances(nil, nil)
26+
e := ec2.New(auth, aws.Regions[config["region"]])
27+
resp, err := e.Instances(nil, nil)
2828

29-
if err != nil {
30-
log.Println(err)
31-
return
32-
}
29+
if err != nil {
30+
log.Println(err)
31+
return
32+
}
3333

34-
for _, res := range resp.Reservations {
35-
for _, inst := range res.Instances {
36-
37-
if inst.DNSName != "" {
38-
var tags []Tag
34+
for _, res := range resp.Reservations {
35+
for _, inst := range res.Instances {
3936

40-
for _, tag := range inst.Tags {
41-
tags = append(tags, Tag{tag.Key, tag.Value})
42-
}
37+
if inst.DNSName != "" {
38+
var tags []Tag
4339

44-
for _, sg := range inst.SecurityGroups {
45-
tags = append(tags, Tag{"Security group", sg.Name})
46-
}
40+
for _, tag := range inst.Tags {
41+
tags = append(tags, Tag{tag.Key, tag.Value})
42+
}
4743

48-
instances[inst.DNSName] = tags
49-
}
50-
}
51-
}
44+
for _, sg := range inst.SecurityGroups {
45+
tags = append(tags, Tag{"Security group", sg.Name})
46+
}
5247

53-
return
54-
}
48+
instances[inst.DNSName] = tags
49+
}
50+
}
51+
}
52+
53+
return
54+
}

config.go

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
11
package main
22

33
import (
4-
"os"
5-
"fmt"
6-
"io/ioutil"
7-
"log"
8-
"github.com/go-yaml/yaml"
9-
"runtime"
4+
"fmt"
5+
"github.com/go-yaml/yaml"
6+
"io/ioutil"
7+
"log"
8+
"os"
9+
"runtime"
1010
)
1111

1212
type Config map[string]StrMap
1313
type StrMap map[string]string
1414

1515
func userHomeDir() string {
16-
if runtime.GOOS == "windows" {
17-
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
18-
if home == "" {
19-
home = os.Getenv("USERPROFILE")
20-
}
21-
return home
22-
}
23-
return os.Getenv("HOME")
16+
if runtime.GOOS == "windows" {
17+
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
18+
if home == "" {
19+
home = os.Getenv("USERPROFILE")
20+
}
21+
return home
22+
}
23+
return os.Getenv("HOME")
2424
}
2525

2626
func readConfig() (config Config) {
27-
config = make(Config)
27+
config = make(Config)
2828

29-
prefferedPaths := []string{
30-
"./cloud-ssh.yaml",
31-
userHomeDir() + "/.ssh/cloud-ssh.yaml",
32-
"/etc/cloud-ssh.yaml",
33-
}
29+
prefferedPaths := []string{
30+
"./cloud-ssh.yaml",
31+
userHomeDir() + "/.ssh/cloud-ssh.yaml",
32+
"/etc/cloud-ssh.yaml",
33+
}
3434

35-
var content []byte
35+
var content []byte
3636

37-
for _, path := range prefferedPaths {
38-
if _, err := os.Stat(path); err == nil {
39-
fmt.Println("Found config:", path)
40-
content, err = ioutil.ReadFile(path)
37+
for _, path := range prefferedPaths {
38+
if _, err := os.Stat(path); err == nil {
39+
fmt.Println("Found config:", path)
40+
content, err = ioutil.ReadFile(path)
4141

42-
if err != nil {
43-
log.Fatal("Error while reading config: ", err)
44-
}
45-
}
46-
}
42+
if err != nil {
43+
log.Fatal("Error while reading config: ", err)
44+
}
45+
}
46+
}
4747

48-
if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
49-
config["default"] = make(StrMap)
50-
config["default"]["access_key"] = os.Getenv("AWS_ACCESS_KEY_ID")
51-
config["default"]["secret_key"] = os.Getenv("AWS_SECRET_ACCESS_KEY")
52-
config["default"]["region"] = os.Getenv("AWS_REGION")
53-
config["default"]["provider"] = "aws"
54-
}
48+
if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
49+
config["default"] = make(StrMap)
50+
config["default"]["access_key"] = os.Getenv("AWS_ACCESS_KEY_ID")
51+
config["default"]["secret_key"] = os.Getenv("AWS_SECRET_ACCESS_KEY")
52+
config["default"]["region"] = os.Getenv("AWS_REGION")
53+
config["default"]["provider"] = "aws"
54+
}
5555

56-
if len(content) == 0 {
57-
if len(config) == 0 {
58-
fmt.Println("Can't find any configuration or ENV variables. Check http://github.com/buger/cloud-ssh for documentation.")
59-
}
60-
return
61-
} else if err := yaml.Unmarshal(content, &config); err != nil {
62-
log.Fatal(err)
63-
}
56+
if len(content) == 0 {
57+
if len(config) == 0 {
58+
fmt.Println("Can't find any configuration or ENV variables. Check http://github.com/buger/cloud-ssh for documentation.")
59+
}
60+
return
61+
} else if err := yaml.Unmarshal(content, &config); err != nil {
62+
log.Fatal(err)
63+
}
6464

65-
return
66-
}
65+
return
66+
}

diginal_ocean.go

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,58 @@
11
package main
22

33
import (
4-
"log"
5-
"github.com/jmoiron/jsonq"
6-
"encoding/json"
7-
"net/http"
4+
"encoding/json"
5+
"github.com/jmoiron/jsonq"
6+
"log"
7+
"net/http"
88
)
99

1010
func getDigitalOceanInstances(config map[string]string) (instances Instances) {
11-
instances = make(Instances)
11+
instances = make(Instances)
1212

13-
if _,ok := config["client_id"]; !ok {
14-
log.Fatal("Missing client_id for ", config["name"], " DigitalOcean cloud")
15-
}
13+
if _, ok := config["client_id"]; !ok {
14+
log.Fatal("Missing client_id for ", config["name"], " DigitalOcean cloud")
15+
}
1616

17-
if _,ok := config["api_key"]; !ok {
18-
log.Fatal("Missing api_key for ", config["name"], " DigitalOcean cloud")
19-
}
17+
if _, ok := config["api_key"]; !ok {
18+
log.Fatal("Missing api_key for ", config["name"], " DigitalOcean cloud")
19+
}
2020

21+
resp, err := http.Get("https://api.digitalocean.com/droplets/?client_id=" + config["client_id"] + "&api_key=" + config["api_key"])
2122

22-
resp, err := http.Get("https://api.digitalocean.com/droplets/?client_id="+config["client_id"]+"&api_key="+config["api_key"])
23+
if err != nil {
24+
log.Println("DigitalOcean API error:", err)
25+
return
26+
}
2327

24-
if err != nil {
25-
log.Println("DigitalOcean API error:", err)
26-
return
27-
}
28+
defer resp.Body.Close()
2829

29-
defer resp.Body.Close()
30+
data := map[string]interface{}{}
31+
dec := json.NewDecoder(resp.Body)
32+
dec.Decode(&data)
33+
jq := jsonq.NewQuery(data)
3034

31-
data := map[string]interface{}{}
32-
dec := json.NewDecoder(resp.Body)
33-
dec.Decode(&data)
34-
jq := jsonq.NewQuery(data)
35+
status, err := jq.String("status")
3536

36-
status, err := jq.String("status")
37+
if status == "ERROR" {
38+
err_msg, _ := jq.String("error_message")
3739

38-
if status == "ERROR" {
39-
err_msg, _ := jq.String("error_message")
40+
log.Println("DigitalOcean API error: ", err_msg)
41+
return
42+
}
4043

41-
log.Println("DigitalOcean API error: ", err_msg)
42-
return
43-
}
44+
droplets, err := jq.ArrayOfObjects("droplets")
4445

45-
droplets, err := jq.ArrayOfObjects("droplets")
46+
if err != nil {
47+
log.Println(err)
48+
return
49+
}
4650

47-
if err != nil {
48-
log.Println(err)
49-
return
50-
}
51+
for _, droplet := range droplets {
52+
instances[droplet["ip_address"].(string)] = []Tag{
53+
Tag{"Name", droplet["name"].(string)},
54+
}
55+
}
5156

52-
for _, droplet := range droplets {
53-
instances[droplet["ip_address"].(string)] = []Tag{
54-
Tag{"Name", droplet["name"].(string),},
55-
}
56-
}
57-
58-
return
59-
}
57+
return
58+
}

0 commit comments

Comments
 (0)