From 393dda981a74f9eb0dd3002a81214ebf5e6d8c2e Mon Sep 17 00:00:00 2001 From: will <64425242+willser@users.noreply.github.com> Date: Fri, 20 Oct 2023 12:41:32 +0800 Subject: [PATCH] refactor: better tip when exit --- src/lib.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1a6ae7e..b02faae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,11 +54,24 @@ pub struct Options { impl Default for Options { fn default() -> Options { Options { - btc_data_dir: std::env::var("btc_data_dir").unwrap_or_default(), - ordi_data_dir: std::env::var("ordi_data_dir").unwrap_or_default(), - btc_rpc_host: std::env::var("btc_rpc_host").unwrap_or_default(), - btc_rpc_user: std::env::var("btc_rpc_user").unwrap_or_default(), - btc_rpc_pass: std::env::var("btc_rpc_pass").unwrap_or_default(), + btc_data_dir: check_env("btc_data_dir"), + ordi_data_dir: check_env("ordi_data_dir"), + btc_rpc_host: check_env("btc_rpc_host"), + btc_rpc_user: check_env("btc_rpc_user"), + btc_rpc_pass: check_env("btc_rpc_pass"), + } + } +} + +fn check_env(env: &str) -> String { + match std::env::var(env) { + Ok(value) => value, + Err(_) => { + eprintln!( + "env `{}` doesn't exist,please read readme.md or .env.example in `https://github.com/hertarr/ordi` and set it.", + env + ); + std::process::exit(1) } } }