aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-08-23 16:26:10 +0200
committerDaniel Mueller <deso@posteo.net>2020-09-03 08:43:06 -0700
commit99133bf07da7e0dfd558d0096ce0a4f08aa307c5 (patch)
tree7ae688a1261112f4537baf0fe9532992c81fe226 /src
parent0cb2ac01371523eae785c12ce88848039090c1e6 (diff)
downloadnitrocli-99133bf07da7e0dfd558d0096ce0a4f08aa307c5.tar.gz
nitrocli-99133bf07da7e0dfd558d0096ce0a4f08aa307c5.tar.bz2
Add Context::from_env constructor
This patch adds the from_env constructor to Context to make the main function easier to read.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs
index 79a9c0b..09ceedd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -131,6 +131,25 @@ pub struct Context<'io> {
pub config: config::Config,
}
+impl<'io> Context<'io> {
+ fn from_env<O, E>(stdout: &'io mut O, stderr: &'io mut E, config: config::Config) -> Context<'io>
+ where
+ O: io::Write,
+ E: io::Write,
+ {
+ Context {
+ stdout,
+ stderr,
+ admin_pin: env::var_os(NITROCLI_ADMIN_PIN),
+ user_pin: env::var_os(NITROCLI_USER_PIN),
+ new_admin_pin: env::var_os(NITROCLI_NEW_ADMIN_PIN),
+ new_user_pin: env::var_os(NITROCLI_NEW_USER_PIN),
+ password: env::var_os(NITROCLI_PASSWORD),
+ config,
+ }
+ }
+}
+
fn run<'ctx, 'io: 'ctx>(ctx: &'ctx mut Context<'io>, args: Vec<String>) -> i32 {
match handle_arguments(ctx, args) {
Ok(()) => 0,
@@ -150,16 +169,7 @@ fn main() {
let rc = match config::Config::load() {
Ok(config) => {
let args = env::args().collect::<Vec<_>>();
- let ctx = &mut Context {
- stdout: &mut stdout,
- stderr: &mut stderr,
- admin_pin: env::var_os(NITROCLI_ADMIN_PIN),
- user_pin: env::var_os(NITROCLI_USER_PIN),
- new_admin_pin: env::var_os(NITROCLI_NEW_ADMIN_PIN),
- new_user_pin: env::var_os(NITROCLI_NEW_USER_PIN),
- password: env::var_os(NITROCLI_PASSWORD),
- config,
- };
+ let ctx = &mut Context::from_env(&mut stdout, &mut stderr, config);
run(ctx, args)
}