-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
140 lines (117 loc) · 2.59 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/awilliams/linode"
"code.google.com/p/gcfg"
)
const configName = "linode-inventory.ini"
var args struct {
list bool
host bool
version bool
}
func init() {
flag.BoolVar(&args.list, "list", false, "Print Ansible formatted inventory")
flag.BoolVar(&args.host, "host", false, "no-op since all information is given via --list")
flag.BoolVar(&args.version, "v", false, "Print version")
}
var config *configuration
var linodeClient *linode.Client
const usage = "usage: %s [flag]\n"
func main() {
flag.Parse()
var err error
config, err = getConfig()
if err != nil {
fatal(err)
return
}
linodeClient = linode.NewClient(config.APIKey)
if args.list {
inv := newInventory(linodes())
inventoryJSON, err := inv.toJSON()
if err != nil {
fatal(err)
return
}
os.Stdout.Write(inventoryJSON)
return
}
if args.host {
// empty hash
fmt.Fprint(os.Stdout, "{}")
return
}
if args.version {
fmt.Printf("%s v%s\n", appName, appVersion)
return
}
fmt.Fprintf(os.Stderr, usage, appName)
flag.PrintDefaults()
}
type configuration struct {
APIKey string `gcfg:"api-key"`
DisplayGroup string `gcfg:"display-group"`
}
// returns true if displayGroup should be included in result set
func (c *configuration) filterDisplayGroup(displayGroup string) bool {
if c.DisplayGroup == "" {
return true
}
return c.DisplayGroup == displayGroup
}
func getConfig() (*configuration, error) {
// first check directory where the executable is located
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return nil, err
}
path := dir + "/" + configName
if _, err := os.Stat(path); os.IsNotExist(err) {
// fallback to working directory. This is usefull when using `go run`
path = configName
}
var config struct {
Linode configuration
}
err = gcfg.ReadFileInto(&config, path)
if err != nil {
return nil, err
}
return &config.Linode, nil
}
type linodeWithIPs struct {
node linode.Linode
ips []linode.LinodeIP
}
func linodes() map[int]*linodeWithIPs {
nodes, err := linodeClient.LinodeList()
if err != nil {
fatal(err)
}
m := make(map[int]*linodeWithIPs, len(nodes))
ids := make([]int, 0, len(nodes))
for _, n := range nodes {
if !config.filterDisplayGroup(n.DisplayGroup) {
continue
}
v := &linodeWithIPs{node: n}
m[n.ID] = v
ids = append(ids, n.ID)
}
ipMap, err := linodeClient.LinodeIPList(ids)
if err != nil {
fatal(err)
}
for nodeID, ips := range ipMap {
m[nodeID].ips = ips
}
return m
}
func fatal(v interface{}) {
fmt.Fprintln(os.Stderr, v)
os.Exit(1)
}