-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkenv.go
156 lines (141 loc) · 5.05 KB
/
checkenv.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
const CHECKENV_VERSION = "v0.0.3"
type showSpec struct {
loadFrom map[string]interface{}
providersFull map[string]interface{}
providerVars map[string]map[string]interface{}
}
func parseShowSpec(args []string) *showSpec {
spec := showSpec{loadFrom: map[string]interface{}{}, providersFull: map[string]interface{}{}, providerVars: map[string]map[string]interface{}{}}
for _, arg := range args {
components := strings.Split(arg, "://")
// TODO(zomglings): Can environment variable names contain the characters "://"? I don't think so.
// However, it is possible that the provider filter arguments *could* contain those characters. That
// means that this logic is wrong. We should probably use a different separator.
providerSpec := components[0]
spec.loadFrom[providerSpec] = nil
if len(components) == 1 {
spec.providersFull[providerSpec] = nil
} else {
if _, ok := spec.providerVars[providerSpec]; !ok {
spec.providerVars[providerSpec] = map[string]interface{}{}
}
varSpec := strings.Join(components[1:], "://")
varNames := strings.Split(varSpec, ",")
for _, varName := range varNames {
spec.providerVars[providerSpec][varName] = nil
}
}
}
return &spec
}
func VariablesFromProviderSpec(providerSpec string) (map[string]string, error) {
components := strings.Split(providerSpec, "+")
provider := components[0]
var providerArgs string
if len(components) > 1 {
providerArgs = strings.Join(components[1:], "+")
}
plugin, pluginExists := RegisteredPlugins[provider]
if !pluginExists {
return map[string]string{}, fmt.Errorf("unregistered provider: %s", provider)
}
return plugin.Provider(providerArgs)
}
func main() {
pluginsCommand := "plugins"
pluginsFlags := flag.NewFlagSet("plugins", flag.ExitOnError)
pluginsHelp := pluginsFlags.Bool("h", false, "Use this flag if you want help with this command")
pluginsFlags.BoolVar(pluginsHelp, "help", false, "Use this flag if you want help with this command")
showCommand := "show"
showFlags := flag.NewFlagSet("show", flag.ExitOnError)
showHelp := showFlags.Bool("h", false, "Use this flag if you want help with this command")
showFlags.BoolVar(showHelp, "help", false, "Use this flag if you want help with this command")
showExport := showFlags.Bool("export", false, "Use this flag to prepend and \"export \" before every environment variable definition")
showRaw := showFlags.Bool("raw", false, "Use this flag to prevent comments output")
showValue := showFlags.Bool("value", false, "Print value only")
versionCommand := "version"
availableCommands := fmt.Sprintf("%s,%s,%s", pluginsCommand, showCommand, versionCommand)
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Please use one of the subcommands: %s\n", availableCommands)
os.Exit(2)
}
command := os.Args[1]
switch command {
case pluginsCommand:
pluginsFlags.Parse(os.Args[2:])
if *pluginsHelp {
fmt.Fprintf(os.Stderr, "Usage: %s %s\nTakes no arguments.\nLists available plugins with a brief description of each one.\n", os.Args[0], os.Args[1])
os.Exit(2)
}
fmt.Println("Available plugins:")
for name, plugin := range RegisteredPlugins {
fmt.Printf("%s\n\t%s\n", name, plugin.Help)
}
case showCommand:
showFlags.Parse(os.Args[2:])
if *showHelp || showFlags.NArg() == 0 {
fmt.Fprintf(os.Stderr, "Usage: %s %s [<provider_name>[+<provider_args>] ...] [<provider_name>[+<provider_args>]://<var_name_1>,<var_name_2>,...,<var_name_n> ...]\nShows the environment variables defined by the given providers.\n", os.Args[0], os.Args[1])
os.Exit(2)
}
spec := parseShowSpec(showFlags.Args())
providedVars := make(map[string]map[string]string)
for providerSpec := range spec.loadFrom {
vars, providerErr := VariablesFromProviderSpec(providerSpec)
if providerErr != nil {
log.Fatalf(providerErr.Error())
}
providedVars[providerSpec] = vars
}
exportPrefix := ""
if *showExport {
exportPrefix = "export "
}
for providerSpec := range spec.providersFull {
if !*showRaw {
fmt.Printf("# Generated with %s - all variables:\n", providerSpec)
}
for k, v := range providedVars[providerSpec] {
if !*showValue {
fmt.Printf("%s%s=%s\n", exportPrefix, k, v)
} else {
fmt.Printf("%s\n", v)
}
}
}
for providerSpec, queriedVars := range spec.providerVars {
if !*showRaw {
fmt.Printf("# Generated with %s - specific variables:\n", providerSpec)
}
definedVars := providedVars[providerSpec]
for k := range queriedVars {
v, ok := definedVars[k]
if !ok {
fmt.Printf("# UNDEFINED: %s\n", k)
} else {
if !*showValue {
fmt.Printf("%s%s=%s\n", exportPrefix, k, v)
} else {
fmt.Printf("%s\n", v)
}
}
}
}
case versionCommand:
pluginsFlags.Parse(os.Args[2:])
if *pluginsHelp {
fmt.Fprintf(os.Stderr, "Usage: %s %s\nShows version of checkenv.\n", os.Args[0], os.Args[1])
os.Exit(2)
}
fmt.Println(CHECKENV_VERSION)
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s. Please use one of the subcommands: %s.\n", command, availableCommands)
}
}