forked from alex-ant/envs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvs.go
153 lines (124 loc) · 3.16 KB
/
envs.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
package envs
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/olekukonko/tablewriter"
)
var envsVal = flag.Bool("envs", false, "Display a list of accepted flags and environment variables")
const (
maxRowLength int = 30
envsFlag string = "envs"
mdFile string = "envs.md"
)
func wordWrap(str string) (newStr string) {
for i, v := range str {
if i > 0 && i%30 == 0 {
newStr += "\n"
}
newStr += string(v)
}
return
}
// GetAllFlags defines corresponding environment variables for each flag and
// returns a table representing all the application flags and environment variables.
func GetAllFlags() error {
// Check if flags are parsed.
if !flag.Parsed() {
return errors.New("flags are not parsed")
}
// Define the table.
var tableBuff *bytes.Buffer
var table *tablewriter.Table
// Assign table variables if table representation is required.
if *envsVal {
// Stop the process after displaying the table.
defer os.Exit(0)
tableBuff = new(bytes.Buffer)
table = tablewriter.NewWriter(tableBuff)
}
// Loop through the flags and fill up the table.
var data [][]string
mdData := "|Flag|Env. variable|Default value|Description|\n|:----|:----|:---|:---|\n"
flag.VisitAll(func(currentFlag *flag.Flag) {
// Skip reserved "envs" flag.
if currentFlag.Name == envsFlag {
return
}
// Define the corresponding environment variable.
envVar := strings.ToUpper(strings.Replace(currentFlag.Name, "-", "_", -1))
// Overwrite flag value if the environment variable is set.
envVarValue := os.Getenv(envVar)
if envVarValue != "" {
flag.Set(currentFlag.Name, envVarValue)
}
// Append to data if table representation is required.
if *envsVal {
// Populate table data.
data = append(data, []string{
currentFlag.Name,
envVar,
wordWrap(currentFlag.DefValue),
wordWrap(currentFlag.Value.String()),
currentFlag.Usage,
})
// Populate MD data.
mdData += fmt.Sprintf(
"|%s|%s|%s|%s|\n",
currentFlag.Name,
envVar,
currentFlag.DefValue,
currentFlag.Usage,
)
}
})
// Skip further execution if table representation is not required.
if !*envsVal {
return nil
}
// Write MD data to file
writeMDErr := ioutil.WriteFile(mdFile, []byte(mdData), 0666)
if writeMDErr != nil {
return writeMDErr
}
// Check whether the default value column is empty.
defaultValIndex := 2
defaultEmpty := true
for _, row := range data {
if row[defaultValIndex] != "" {
defaultEmpty = false
break
}
}
// Delete column if empty for all rows.
if defaultEmpty {
for i := 0; i < len(data); i++ {
data[i] = append(data[i][:defaultValIndex], data[i][defaultValIndex+1:]...)
}
}
// Assemble header slice.
header := []string{
"Flag",
"Environment Var",
"Default Value",
"Current Value",
"Description",
}
if defaultEmpty {
header = append(header[:defaultValIndex], header[defaultValIndex+1:]...)
}
table.SetHeader(header)
// Append bulk data to the table.
table.AppendBulk(data)
// Render the table.
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetBorder(false)
table.Render()
// Print table data.
fmt.Println(tableBuff.String())
return nil
}