-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcloud.go
71 lines (65 loc) · 1.47 KB
/
gcloud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// GCloud - Go Packages for Cloud Services.
// Copyright (c) 2013 Garrett Woodworth (https://github.com/gwoo).
package main
import (
"encoding/json"
"errors"
"flag"
//"fmt"
c "github.com/gcloud/compute"
p "github.com/gcloud/providers"
_ "github.com/gcloud/providers/digitalocean"
_ "github.com/gcloud/providers/vbox"
)
var provider = flag.String("p", "vbox", "Provider to use for actions.")
var name = flag.String("name", "", "Name of the object.")
func DoServers(provider string, method string) ([]byte, error) {
s := &c.Servers{Provider: provider}
switch method {
case "list":
r, err := s.List()
return respond(r, err)
case "show":
r, err := s.Show(*name)
return respond(r, err)
case "add":
r, err := s.Create(s.NewServer(p.Map{"name": *name}))
return respond(r, err)
case "destroy":
r, err := s.Destroy(*name)
return respond(r, err)
}
return nil, errors.New("Missing method for Servers.")
}
func main() {
flag.Parse()
method := flag.Arg(0)
pkg := flag.Arg(1)
switch pkg {
case "servers":
result, err := DoServers(*provider, method)
if err != nil {
println(string(err.Error()))
return
}
println(string(result))
}
}
func respond(r interface{}, err error) ([]byte, error) {
if err != nil {
return nil, err
}
switch response := r.(type) {
case nil:
return nil, err
case []byte:
return response, err
case bool:
return []byte(`true`), err
case string:
return []byte(response), err
default:
return json.Marshal(r)
}
return nil, err
}