-
Notifications
You must be signed in to change notification settings - Fork 6
/
options.go
60 lines (52 loc) · 1.63 KB
/
options.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
package ec2fzf
import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type Options struct {
Regions []string
UsePrivateIp bool
Template string
PreviewTemplate string
Filters []string
}
func ParseOptions() Options {
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath("$HOME/.config/ec2-fzf")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
} else {
panic(err)
}
}
pflag.StringSlice("region", []string{"us-east-1"}, "The AWS region")
pflag.Bool("use-private-ip", true, "Return the private ip of the instance selected")
pflag.StringSlice("filters", []string{}, "Filters to apply with the ec2 api call")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
viper.RegisterAlias("UsePrivateIp", "use-private-ip")
viper.RegisterAlias("regions", "region")
viper.SetDefault("Region", "us-east-1")
viper.SetDefault("UsePrivateIp", false)
viper.SetDefault("Template", `{{ .InstanceId }}: {{index .Tags "Name"}}`)
viper.SetDefault("PreviewTemplate", `
Instance Id: {{.InstanceId}}
Name: {{index .Tags "Name"}}
Private IP: {{.PrivateIpAddress}}
Public IP: {{.PublicIpAddress}}
Tags:
{{ range $key, $value := .Tags }}
{{ indent 2 $key }}: {{ $value }}
{{- end -}}
`,
)
return Options{
Regions: viper.GetStringSlice("Regions"),
UsePrivateIp: viper.GetBool("UsePrivateIp"),
Template: viper.GetString("Template"),
PreviewTemplate: viper.GetString("PreviewTemplate"),
Filters: viper.GetStringSlice("Filters"),
}
}