/
flag.go
161 lines (131 loc) · 4.61 KB
/
flag.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
157
158
159
160
161
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"k8s.io/client-go/util/homedir"
)
var (
homeDir string
configPath string
version = "dev"
commit = ""
date = ""
builtBy = ""
)
func init() {
homeDir = homedir.HomeDir()
configPath = filepath.Join(homeDir, ".eks-node-viewer")
}
type Flags struct {
Context string
NodeSelector string
ExtraLabels string
NodeSort string
Style string
Kubeconfig string
Resources string
DisablePricing bool
ShowAttribution bool
Version bool
}
func ParseFlags() (Flags, error) {
flagSet := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
var flags Flags
cfg, err := loadConfigFile()
if err != nil {
return Flags{}, fmt.Errorf("load config file: %w", err)
}
flagSet.BoolVar(&flags.Version, "v", false, "Display eks-node-viewer version")
flagSet.BoolVar(&flags.Version, "version", false, "Display eks-node-viewer version")
contextDefault := cfg.getValue("context", "")
flagSet.StringVar(&flags.Context, "context", contextDefault, "Name of the kubernetes context to use")
nodeSelectorDefault := cfg.getValue("node-selector", "")
flagSet.StringVar(&flags.NodeSelector, "node-selector", nodeSelectorDefault, "Node label selector used to filter nodes, if empty all nodes are selected ")
extraLabelsDefault := cfg.getValue("extra-labels", "")
flagSet.StringVar(&flags.ExtraLabels, "extra-labels", extraLabelsDefault, "A comma separated set of extra node labels to display")
nodeSort := cfg.getValue("node-sort", "creation=dsc")
flagSet.StringVar(&flags.NodeSort, "node-sort", nodeSort, "Sort order for the nodes, either 'creation' or a label name. The sort order defaults to ascending and can be controlled by appending =asc or =dsc to the value.")
style := cfg.getValue("style", "#04B575,#FFFF00,#FF0000")
flagSet.StringVar(&flags.Style, "style", style, "Three color to use for styling 'good','ok' and 'bad' values. These are also used in the gradients displayed from bad -> good.")
// flag overrides env. var. and env. var. overrides config file
kubeconfigDefault := getStringEnv("KUBECONFIG", cfg.getValue("kubeconfig", filepath.Join(homeDir, ".kube", "config")))
flagSet.StringVar(&flags.Kubeconfig, "kubeconfig", kubeconfigDefault, "Absolute path to the kubeconfig file")
resourcesDefault := cfg.getValue("resources", "cpu")
flagSet.StringVar(&flags.Resources, "resources", resourcesDefault, "List of comma separated resources to monitor")
disablePricingDefault := cfg.getBoolValue("disable-pricing", false)
flagSet.BoolVar(&flags.DisablePricing, "disable-pricing", disablePricingDefault, "Disable pricing lookups")
flagSet.BoolVar(&flags.ShowAttribution, "attribution", false, "Show the Open Source Attribution")
if err := flagSet.Parse(os.Args[1:]); err != nil {
return Flags{}, err
}
return flags, nil
}
// --- env vars ---
func getStringEnv(envName string, defaultValue string) string {
env, ok := os.LookupEnv(envName)
if !ok {
return defaultValue
}
return env
}
// --- config file ---
type configFile map[string]string
func (c configFile) getValue(key string, defaultValue string) string {
if val, ok := c[key]; ok {
return val
}
return defaultValue
}
func (c configFile) getBoolValue(key string, defaultValue bool) bool {
if val, ok := c[key]; ok {
if boolVal, err := strconv.ParseBool(val); err == nil {
return boolVal
}
}
return defaultValue
}
func loadConfigFile() (configFile, error) {
fileContent := make(map[string]string)
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
return fileContent, nil
}
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") {
continue
}
lineKV := strings.SplitN(line, "=", 2)
if len(lineKV) == 2 {
key := strings.TrimSpace(lineKV[0])
value := strings.TrimSpace(lineKV[1])
fileContent[key] = value
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return fileContent, nil
}