Skip to content

Commit

Permalink
Add Context::from_env constructor
Browse files Browse the repository at this point in the history
This patch adds the from_env constructor to Context to make the main
function easier to read.
  • Loading branch information
robinkrahl authored and d-e-s-o committed Jan 11, 2021
1 parent 4f37ab7 commit 53d89aa
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down

0 comments on commit 53d89aa

Please sign in to comment.