/
conf.go
244 lines (225 loc) · 7.91 KB
/
conf.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Package conf sets the configurations of this program using the host system
// environment variables.
package conf
import (
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"text/tabwriter"
"github.com/caarlos0/env/v7"
"github.com/charmbracelet/lipgloss"
"github.com/gookit/color"
)
const (
EnvPrefix = "DF2_" // EnvPrefix is the prefix applied to all environment variable names.
LiveServer = "DF2_HOST" // LiveServer is environment variable name to identify the live web server.
GapUser = "df2" // GapUser is the Go Application Paths username.
)
// Config environment overrides for the Defacto2 tool.
// There are no envDefault attributes in this struct,
// instead they're found in the func Defaults().
type Config struct {
IsProduction bool `env:"PRODUCTION" help:"Use the production mode to log all errors and warnings to a file"` //nolint:lll
MaxProcs uint `env:"MAXPROCS" help:"Limit the number of operating system threads the program can use"` //nolint:lll
DBName string `env:"DBNAME" help:"Name of the database to use"`
DBUser string `env:"DBUSER" help:"Database connection user name"`
DBPass string `env:"DBPASS" help:"Database connection password"`
DBHost string `env:"DBHOST" help:"Database connection host address"`
DBPort uint `env:"DBPORT" help:"Database connection TCP port"`
WebRoot string `env:"ROOT" help:"Path to the root directory of the website"`
Downloads string `env:"DOWNLOAD" help:"Path containing UUID named files served as downloads"`
Images string `env:"IMG000" help:"Path containing screenshots and previews"`
Thumbs string `env:"IMG400" help:"Path containing 400x400 thumbnails of the screenshots"`
Backups string `env:"BACKUP" help:"Path containing backup archives or previously removed files"`
Emulator string `env:"EMULATOR" help:"Path containing the DOSee emulation files"`
HTMLExports string `env:"HTML" help:"Path to save the HTML files generated by this tool"`
IncomingFiles string `env:"INCOMING" help:"Path containing user uploaded files"`
IncomingImgs string `env:"INCOMINGIMG" help:"Path containing screenshots of user uploaded files"`
HTMLViews string `env:"VIEWS" help:"Path to save the HTML files generated by this tool"`
SQLDumps string `env:"SQLDUMP" help:"Path containing database data exports as SQL dumps"`
Timeout uint `env:"TIMEOUT" help:"The timeout in seconds value for database connections"`
}
// Defaults for the Config environment struct.
// Directory paths are different based on weather the DF2_HOST environment variable is set.
// When set, a /opt/ parent directory is used as the root, otherwise the user home directory is used.
func Defaults() Config {
// remote server defaults
const root = string(os.PathSeparator)
opt := filepath.Join(root, "opt")
assets := filepath.Join(opt, "assets-defacto2")
webRoot := filepath.Join(opt, "Defacto2-2020", "ROOT")
incoming := filepath.Join(webRoot, "incoming", "user_submissions")
// local developer overrides
value, ok := os.LookupEnv(LiveServer)
if !ok || value == "" {
home, _ := os.UserHomeDir()
assets = filepath.Join(home, "assets-defacto2")
incoming = filepath.Join(home, "user_submissions")
opt = filepath.Join(home, "opt")
webRoot = filepath.Join(home, "github", "Defacto2-2020", "ROOT")
}
const (
mysqlPort = 3306
timeoutSec = 30
)
init := Config{
// program settings
IsProduction: false,
MaxProcs: 0,
// database connection
DBName: "defacto2-inno",
DBUser: "root",
DBPass: "password",
DBHost: "localhost",
DBPort: mysqlPort,
Timeout: timeoutSec, // Timeout value matches the 30s timeout for unit tests.
// directory paths
WebRoot: webRoot,
Downloads: filepath.Join(assets, "downloads"),
Images: filepath.Join(assets, "images000"),
Thumbs: filepath.Join(assets, "images400"),
Backups: filepath.Join(webRoot, "files", "backups"),
Emulator: filepath.Join(webRoot, "files", "emularity.zip"),
HTMLExports: filepath.Join(webRoot, "files", "html"),
HTMLViews: filepath.Join(webRoot, "views"),
IncomingFiles: filepath.Join(incoming, "files"),
IncomingImgs: filepath.Join(incoming, "previews"),
SQLDumps: filepath.Join(opt, "backup"),
}
if ok && value != "" {
init.DBHost = value
}
return init
}
// TestData returns the directory paths but with the temporary directory as root.
// This is intended for directories unit tests.
func TestData() Config {
tmp := filepath.Join(os.TempDir(), "df2-mocker")
assets := filepath.Join(tmp, "assets-defacto2")
webRoot := filepath.Join(tmp, "github", "Defacto2-2020", "ROOT")
incoming := filepath.Join(webRoot, "incoming", "user_submissions")
return Config{
// directory paths
WebRoot: webRoot,
Downloads: filepath.Join(assets, "downloads"),
Images: filepath.Join(assets, "images000"),
Thumbs: filepath.Join(assets, "images400"),
Backups: filepath.Join(webRoot, "files", "backups"),
Emulator: filepath.Join(webRoot, "files", "emularity.zip"),
HTMLExports: filepath.Join(webRoot, "files", "html"),
HTMLViews: filepath.Join(webRoot, "views"),
IncomingFiles: filepath.Join(incoming, "files"),
IncomingImgs: filepath.Join(incoming, "previews"),
SQLDumps: filepath.Join(tmp, "backup"),
}
}
func Options() env.Options {
return env.Options{
Prefix: EnvPrefix,
}
}
// TODO: use the dir path colouriser to display paths
// TODO: replace dir../dir...go Init() func.
func (c Config) String(w io.Writer) { //nolint:funlen
if w == nil {
w = io.Discard
}
const (
minwidth = 2
tabwidth = 4
padding = 2
padchar = ' '
flags = 0
h1 = "Variable"
h2 = "Value"
h3 = "Variable"
h4 = "Value type"
h5 = "Help"
line = "─"
donotuse = 5
)
// TODO:
// list configurations in use (ie non-blanks)
// DBNAME defacto2-inno (italic, wrap HELP)
// then list all environment variables, types and help
tw := tabwriter.NewWriter(w, minwidth, tabwidth, padding, padchar, flags)
style := lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("228")).
PaddingLeft(padding).
PaddingRight(padding).
Margin(1)
fmt.Fprintln(tw, style.Render("Environment variables and configurations"))
fmt.Fprintf(tw, "\t%s\t%s\t\t\n", h1, h2)
fmt.Fprintf(tw, "\t%s\t%s\t\t\n",
strings.Repeat(line, len(h1)), strings.Repeat(line, len(h2)))
fields := reflect.VisibleFields(reflect.TypeOf(c))
values := reflect.ValueOf(c)
for _, field := range fields {
if !field.IsExported() {
continue
}
val, def := values.FieldByName(field.Name), field.Tag.Get("envDefault")
fmt.Fprintf(tw, "\t%s\t%v\t%v\t\n",
EnvPrefix+field.Tag.Get("env"),
val,
match(fmt.Sprint(val), def),
)
}
fmt.Fprintln(tw)
tw.Flush()
tw = tabwriter.NewWriter(w, minwidth, tabwidth, padding, padchar, flags)
fmt.Fprintf(tw, "\t%s\t%s\t%s\n", h3, h4, h5)
fmt.Fprintf(tw, "\t%s\t%s\t%s\n",
strings.Repeat(line, len(h3)), strings.Repeat(line, len(h4)), strings.Repeat(line, len(h5)))
for j, field := range fields {
if !field.IsExported() {
continue
}
if j == donotuse {
fmt.Fprintf(tw, "\t\t\t\t\n")
fmt.Fprintf(tw, "\t\t\t These variables below are not recommended.\t\n")
}
fmt.Fprintf(tw, "\t%s\t%s\t",
field.Tag.Get("env"),
types(field.Type),
)
sp := ""
if field.Tag.Get("avoid") != "" {
sp = " "
}
fmt.Fprintf(tw, "%s%s%s.\n",
avoid(field.Tag.Get("avoid")),
sp,
field.Tag.Get("help"),
)
}
tw.Flush()
}
func avoid(x string) string {
if x == "true" {
c := color.New(color.FgRed, color.Bold)
return c.Sprint("✗")
}
return ""
}
func match(x, y string) string {
if x != y {
c := color.New(color.FgGreen, color.Bold)
return c.Sprint("✓")
}
return ""
}
func types(t reflect.Type) string {
switch t.Kind() { //nolint:exhaustive
case reflect.Bool:
return "true|false"
case reflect.Uint:
return "number"
default:
return t.String()
}
}