From 21ef262edeeb159f855ae4cd2083e6bce11090ae Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 6 Apr 2022 17:16:27 +0200 Subject: [PATCH 1/2] Migrated rest of cli commands --- cli.v | 84 --------------------------------------------------- src/git/cli.v | 40 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 84 deletions(-) delete mode 100644 cli.v diff --git a/cli.v b/cli.v deleted file mode 100644 index 256b856..0000000 --- a/cli.v +++ /dev/null @@ -1,84 +0,0 @@ -import os -import toml -import net.http - -struct Config { - address string [required] - api_key string [required] -} - -fn list(conf Config) ? { - mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ? - req.add_custom_header('X-API-Key', conf.api_key) ? - - res := req.do() ? - - println(res.text) -} - -fn add(conf Config, args []string) ? { - if args.len < 2 { - eprintln('Not enough arguments.') - exit(1) - } - - if args.len > 2 { - eprintln('Too many arguments.') - exit(1) - } - - mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=${args[0]}&branch=${args[1]}', '') ? - req.add_custom_header('X-API-Key', conf.api_key) ? - - res := req.do() ? - - println(res.text) -} - -fn remove(conf Config, args []string) ? { - if args.len < 2 { - eprintln('Not enough arguments.') - exit(1) - } - - if args.len > 2 { - eprintln('Too many arguments.') - exit(1) - } - - mut req := http.new_request(http.Method.delete, '$conf.address/api/repos?url=${args[0]}&branch=${args[1]}', '') ? - req.add_custom_header('X-API-Key', conf.api_key) ? - - res := req.do() ? - - println(res.text) -} - -fn main() { - conf_path := os.expand_tilde_to_home('~/.vieterrc') - - if !os.is_file(conf_path) { - exit(1) - } - - conf := toml.parse_file(conf_path) ?.reflect() - - args := os.args[1..] - - if args.len == 0 { - eprintln('No action provided.') - exit(1) - } - - action := args[0] - - match action { - 'list' { list(conf) ? } - 'add' { add(conf, args[1..]) ? } - 'remove' { remove(conf, args[1..]) ? } - else { - eprintln("Invalid action '$action'.") - exit(1) - } - } -} diff --git a/src/git/cli.v b/src/git/cli.v index a91f5e2..2b164e1 100644 --- a/src/git/cli.v +++ b/src/git/cli.v @@ -23,6 +23,28 @@ pub fn cmd() cli.Command { list(conf) ? } }, + cli.Command{ + name: 'add' + required_args: 2 + usage: 'url branch' + description: 'Add a new repository.' + execute: fn (cmd cli.Command) ? { + conf := env.load() ? + + add(conf, cmd.args[0], cmd.args[1]) ? + } + }, + cli.Command{ + name: 'remove' + required_args: 2 + usage: 'url branch' + description: 'Remove a repository.' + execute: fn (cmd cli.Command) ? { + conf := env.load() ? + + remove(conf, cmd.args[0], cmd.args[1]) ? + } + }, ] } } @@ -35,3 +57,21 @@ fn list(conf Config) ? { println(res.text) } + +fn add(conf Config, url string, branch string) ? { + mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=$url&branch=$branch', '') ? + req.add_custom_header('X-API-Key', conf.api_key) ? + + res := req.do() ? + + println(res.text) +} + +fn remove(conf Config, url string, branch string) ? { + mut req := http.new_request(http.Method.delete, '$conf.address/api/repos?url=$url&branch=$branch', '') ? + req.add_custom_header('X-API-Key', conf.api_key) ? + + res := req.do() ? + + println(res.text) +} From e9d7858380162b3cf36683e91f73e1671f80d368 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 6 Apr 2022 17:51:06 +0200 Subject: [PATCH 2/2] env module now properly supports config file --- src/build/cli.v | 3 ++- src/env.v | 63 ++++++++++++++++++++++++++++-------------------- src/git/cli.v | 15 ++++++++---- src/server/cli.v | 3 ++- 4 files changed, 51 insertions(+), 33 deletions(-) diff --git a/src/build/cli.v b/src/build/cli.v index bb3c016..a32a6e7 100644 --- a/src/build/cli.v +++ b/src/build/cli.v @@ -8,7 +8,8 @@ pub fn cmd() cli.Command { name: 'build' description: 'Run the build process.' execute: fn (cmd cli.Command) ? { - conf := env.load() ? + config_file := cmd.flags.get_string('config-file') ? + conf := env.load(config_file) ? build(conf) ? } diff --git a/src/env.v b/src/env.v index 3babd55..62678e6 100644 --- a/src/env.v +++ b/src/env.v @@ -1,6 +1,7 @@ module env import os +import toml // The prefix that every environment variable should have const prefix = 'VIETER_' @@ -32,9 +33,9 @@ fn get_env_var(field_name string) ?string { env_var := os.getenv(env_var_name) env_file := os.getenv(env_file_name) - // If both aren't set, we report them missing + // If both are missing, we return an empty string if env_var == '' && env_file == '' { - return error('Either $env_var_name or $env_file_name is required.') + return '' } // If they're both set, we report a conflict @@ -56,37 +57,47 @@ fn get_env_var(field_name string) ?string { } } -fn load_in_place(mut o T) ? { - $for field in T.fields { - o.$(field.name) = get_env_var(field.name) or { - // 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 err - } - - default - } - } -} - // load attempts to create the given type from environment variables. For // each field, the corresponding env var is its name in uppercase prepended // with the hardcoded prefix. If this one isn't present, it looks for the env // var with the file_suffix suffix. -pub fn load() ?T { +pub fn load(path string) ?T { mut res := T{} - load_in_place(mut res) ? + if os.exists(path) { + res = toml.parse_file(path) ?.reflect() + } + $for field in T.fields { + $if field.typ is string { + env_value := get_env_var(field.name) ? + + // The value of the env var will always be chosen over the config + // file + if env_value != '' { + res.$(field.name) = env_value + } + // 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 res } diff --git a/src/git/cli.v b/src/git/cli.v index 2b164e1..6030612 100644 --- a/src/git/cli.v +++ b/src/git/cli.v @@ -18,7 +18,8 @@ pub fn cmd() cli.Command { name: 'list' description: 'List the current repos.' execute: fn (cmd cli.Command) ? { - conf := env.load() ? + config_file := cmd.flags.get_string('config-file') ? + conf := env.load(config_file) ? list(conf) ? } @@ -29,7 +30,8 @@ pub fn cmd() cli.Command { usage: 'url branch' description: 'Add a new repository.' execute: fn (cmd cli.Command) ? { - conf := env.load() ? + config_file := cmd.flags.get_string('config-file') ? + conf := env.load(config_file) ? add(conf, cmd.args[0], cmd.args[1]) ? } @@ -40,7 +42,8 @@ pub fn cmd() cli.Command { usage: 'url branch' description: 'Remove a repository.' execute: fn (cmd cli.Command) ? { - conf := env.load() ? + config_file := cmd.flags.get_string('config-file') ? + conf := env.load(config_file) ? remove(conf, cmd.args[0], cmd.args[1]) ? } @@ -59,7 +62,8 @@ fn list(conf Config) ? { } fn add(conf Config, url string, branch string) ? { - mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=$url&branch=$branch', '') ? + mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=$url&branch=$branch', + '') ? req.add_custom_header('X-API-Key', conf.api_key) ? res := req.do() ? @@ -68,7 +72,8 @@ fn add(conf Config, url string, branch string) ? { } fn remove(conf Config, url string, branch string) ? { - mut req := http.new_request(http.Method.delete, '$conf.address/api/repos?url=$url&branch=$branch', '') ? + mut req := http.new_request(http.Method.delete, '$conf.address/api/repos?url=$url&branch=$branch', + '') ? req.add_custom_header('X-API-Key', conf.api_key) ? res := req.do() ? diff --git a/src/server/cli.v b/src/server/cli.v index 2b51c29..367878a 100644 --- a/src/server/cli.v +++ b/src/server/cli.v @@ -8,7 +8,8 @@ pub fn cmd() cli.Command { name: 'server' description: 'Start the Vieter server' execute: fn (cmd cli.Command) ? { - conf := env.load() ? + config_file := cmd.flags.get_string('config-file') ? + conf := env.load(config_file) ? server(conf) ? }