forked from vieter-v/vieter
Switched to proper default values system
parent
b70be0574e
commit
2aa2aa143c
23
Makefile
23
Makefile
|
@ -23,13 +23,7 @@ dvieter: $(SOURCES)
|
|||
# Run the debug build inside gdb
|
||||
.PHONY: gdb
|
||||
gdb: dvieter
|
||||
VIETER_API_KEY=test \
|
||||
VIETER_DOWNLOAD_DIR=data/downloads \
|
||||
VIETER_REPO_DIR=data/repo \
|
||||
VIETER_PKG_DIR=data/pkgs \
|
||||
VIETER_LOG_LEVEL=DEBUG \
|
||||
VIETER_REPOS_FILE=data/repos.json \
|
||||
gdb --args ./dvieter
|
||||
gdb --args './dvieter -f vieter.toml server'
|
||||
|
||||
# Optimised production build
|
||||
.PHONY: prod
|
||||
|
@ -46,22 +40,11 @@ c:
|
|||
# Run the server in the default 'data' directory
|
||||
.PHONY: run
|
||||
run: vieter
|
||||
VIETER_API_KEY=test \
|
||||
VIETER_DOWNLOAD_DIR=data/downloads \
|
||||
VIETER_REPO_DIR=data/repo \
|
||||
VIETER_PKG_DIR=data/pkgs \
|
||||
VIETER_LOG_LEVEL=DEBUG \
|
||||
VIETER_REPOS_FILE=data/repos.json \
|
||||
./vieter server
|
||||
./vieter -f vieter.toml server
|
||||
|
||||
.PHONY: run-prod
|
||||
run-prod: prod
|
||||
VIETER_API_KEY=test \
|
||||
VIETER_DOWNLOAD_DIR=data/downloads \
|
||||
VIETER_REPO_DIR=data/repo \
|
||||
VIETER_PKG_DIR=data/pkgs \
|
||||
VIETER_LOG_LEVEL=DEBUG \
|
||||
./pvieter server
|
||||
./pvieter -f vieter.toml server
|
||||
|
||||
# =====OTHER=====
|
||||
.PHONY: lint
|
||||
|
|
33
src/env.v
33
src/env.v
|
@ -12,8 +12,8 @@ const file_suffix = '_FILE'
|
|||
|
||||
pub struct ServerConfig {
|
||||
pub:
|
||||
log_level string [default: WARN]
|
||||
log_file string [default: 'vieter.log']
|
||||
log_level string = "WARN"
|
||||
log_file string = "vieter.log"
|
||||
pkg_dir string
|
||||
download_dir string
|
||||
api_key string
|
||||
|
@ -65,7 +65,18 @@ pub fn load<T>(path string) ?T {
|
|||
mut res := T{}
|
||||
|
||||
if os.exists(path) {
|
||||
res = toml.parse_file(path) ?.reflect<T>()
|
||||
// We don't use reflect here because reflect also sets any fields not
|
||||
// in the toml back to "empty", which we don't want
|
||||
doc := toml.parse_file(path) ?
|
||||
|
||||
$for field in T.fields {
|
||||
s := doc.value(field.name)
|
||||
|
||||
// We currently only support strings
|
||||
if s.type_name() == "string" {
|
||||
res.$(field.name) = s.string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$for field in T.fields {
|
||||
|
@ -80,21 +91,7 @@ pub fn load<T>(path string) ?T {
|
|||
// If there's no value from the toml file either, we try to find a
|
||||
// default value
|
||||
else if res.$(field.name) == '' {
|
||||
// We use the default instead, if it's present
|
||||
mut default := ''
|
||||
|
||||
for attr in field.attrs {
|
||||
if attr.starts_with('default: ') {
|
||||
default = attr[9..]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if default == '' {
|
||||
return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.")
|
||||
}
|
||||
|
||||
res.$(field.name) = default
|
||||
return error("Missing config variable '$field.name' with no provided default. Either add it to the config file or provide it using an environment variable.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
# This file contains settings used during development
|
||||
api_key = "test"
|
||||
download_dir = "data/downloads"
|
||||
repo_dir = "data/repo"
|
||||
pkg_dir = "data/pkgs"
|
||||
# log_level = "DEBUG"
|
||||
repos_file = "data/repos.json"
|
||||
|
Loading…
Reference in New Issue