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
|
# Run the debug build inside gdb
|
||||||
.PHONY: gdb
|
.PHONY: gdb
|
||||||
gdb: dvieter
|
gdb: dvieter
|
||||||
VIETER_API_KEY=test \
|
gdb --args './dvieter -f vieter.toml server'
|
||||||
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
|
|
||||||
|
|
||||||
# Optimised production build
|
# Optimised production build
|
||||||
.PHONY: prod
|
.PHONY: prod
|
||||||
|
@ -46,22 +40,11 @@ c:
|
||||||
# Run the server in the default 'data' directory
|
# Run the server in the default 'data' directory
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: vieter
|
run: vieter
|
||||||
VIETER_API_KEY=test \
|
./vieter -f vieter.toml server
|
||||||
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
|
|
||||||
|
|
||||||
.PHONY: run-prod
|
.PHONY: run-prod
|
||||||
run-prod: prod
|
run-prod: prod
|
||||||
VIETER_API_KEY=test \
|
./pvieter -f vieter.toml server
|
||||||
VIETER_DOWNLOAD_DIR=data/downloads \
|
|
||||||
VIETER_REPO_DIR=data/repo \
|
|
||||||
VIETER_PKG_DIR=data/pkgs \
|
|
||||||
VIETER_LOG_LEVEL=DEBUG \
|
|
||||||
./pvieter server
|
|
||||||
|
|
||||||
# =====OTHER=====
|
# =====OTHER=====
|
||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
|
|
33
src/env.v
33
src/env.v
|
@ -12,8 +12,8 @@ const file_suffix = '_FILE'
|
||||||
|
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
pub:
|
pub:
|
||||||
log_level string [default: WARN]
|
log_level string = "WARN"
|
||||||
log_file string [default: 'vieter.log']
|
log_file string = "vieter.log"
|
||||||
pkg_dir string
|
pkg_dir string
|
||||||
download_dir string
|
download_dir string
|
||||||
api_key string
|
api_key string
|
||||||
|
@ -65,7 +65,18 @@ pub fn load<T>(path string) ?T {
|
||||||
mut res := T{}
|
mut res := T{}
|
||||||
|
|
||||||
if os.exists(path) {
|
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 {
|
$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
|
// If there's no value from the toml file either, we try to find a
|
||||||
// default value
|
// default value
|
||||||
else if res.$(field.name) == '' {
|
else if res.$(field.name) == '' {
|
||||||
// We use the default instead, if it's present
|
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.")
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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