forked from awilliams/linode-inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (114 loc) · 2.49 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
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 {
version bool
ini string
}
func init() {
flag.BoolVar(&args.version, "v", false, "Print version")
flag.StringVar(&args.ini, "f", configName, "Specify config file")
}
var config *configuration
var linodeClient *linode.Client
const usage = "usage: %s [flag]\n"
func main() {
flag.Parse()
var err error
config, err = getConfig(args.ini)
if err != nil {
if args.ini != configName {
fatal(err)
return
} else {
flag.PrintDefaults()
return
}
}
if args.version {
fmt.Printf("%s v%s\n", appName, appVersion)
return
} else {
linodeClient = linode.NewClient(config.APIKey)
inv := newInventory(linodes())
inventoryJSON, err := inv.toJSON()
if err != nil {
fatal(err)
return
}
os.Stdout.Write(inventoryJSON)
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(filename string) (*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 + "/" + filename
if _, err := os.Stat(path); os.IsNotExist(err) {
// fallback to working directory. This is usefull when using `go run`
path = filename
}
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)
}