-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
129 lines (117 loc) · 3.02 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/ReSTARTR/ec2-ls-hosts/client"
"github.com/ReSTARTR/ec2-ls-hosts/creds"
"gopkg.in/ini.v1"
)
var (
version string
)
func loadRegionInAwsConfig(profile string) string {
cfg, err := creds.LoadAwsConfig()
if err == nil {
return cfg.Section(profile).Key("region").Value()
}
return ""
}
func loadConfig() (cfg *ini.File, err error) {
cfg, err = ini.LooseLoad(
os.Getenv("HOME")+"/.ls-hosts",
"/etc/ls-hosts.conf",
)
if err != nil {
return
}
return
}
// string to map[string]string
func parseFilterString(s string) map[string]string {
filters := make(map[string]string, 5)
for _, kv := range strings.Split(s, ",") {
a := strings.Split(kv, ":")
if len(a) > 1 {
v := a[1:len(a)]
filters[a[0]] = strings.Join(v, ":")
}
}
return filters
}
// string to []string
func parseFieldsString(str string) []string {
var fields []string
for _, c := range strings.Split(str, ",") {
fields = append(fields, c)
}
return fields
}
func optionsFromFile() *client.Options {
opt := client.NewOptions()
if cfg, err := loadConfig(); err == nil {
opt.Profile = cfg.Section("options").Key("profile").Value()
opt.Region = cfg.Section("options").Key("region").Value()
opt.TagFilters = parseFilterString(cfg.Section("options").Key("tags").Value())
opt.Fields = parseFieldsString(cfg.Section("options").Key("fields").Value())
opt.Credentials = cfg.Section("options").Key("creds").Value()
opt.Noheader = (cfg.Section("options").Key("noheader").Value() == "true")
}
return opt
}
func NewTableWriter() *tabwriter.Writer {
table := new(tabwriter.Writer)
table.Init(os.Stdout, 1, 8, 1, '\t', 0)
return table
}
func main() {
// parse options
profile := flag.String("profile", "default", "profile name of aws credentials")
filters := flag.String("filters", "", "key1:value1,key2:value2,...")
tagFilters := flag.String("tags", "", "key1:value1,key2:value2,...")
fields := flag.String("fields", "", "column1,column2,...")
regionString := flag.String("region", "", "region name")
credsString := flag.String("creds", "", "env, shared, iam")
noheader := flag.Bool("noheader", false, "hide header")
v := flag.Bool("v", false, "show version")
flag.Parse()
if *v {
fmt.Println("version: " + version)
os.Exit(0)
}
opt := optionsFromFile()
opt.Profile = *profile
awsConfigRegion := loadRegionInAwsConfig(opt.Profile)
if awsConfigRegion != "" {
opt.Region = awsConfigRegion
}
// merge optoins from cmdline
if *filters != "" {
for k, v := range parseFilterString(*filters) {
opt.Filters[k] = v
}
}
if *tagFilters != "" {
for k, v := range parseFilterString(*tagFilters) {
opt.TagFilters[k] = v
}
}
if *fields != "" {
opt.Fields = parseFieldsString(*fields)
}
if *regionString != "" {
opt.Region = *regionString
}
if *credsString != "" {
opt.Credentials = *credsString
}
opt.Noheader = opt.Noheader || *noheader
// run
w := NewTableWriter()
err := client.Describe(opt, w)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
}