From 999bb4b59744c4f24a537af43821c3665077db4b Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Tue, 19 Jul 2022 13:43:14 +0200 Subject: [PATCH] Liq/Keeper: allow --dotenv= arg Makes it easier to run the tools with different configs, without either passing everything explicitly or making separate directories for .env files. --- keeper/src/main.rs | 21 ++++++++++++++++++--- liquidator/src/main.rs | 20 ++++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/keeper/src/main.rs b/keeper/src/main.rs index aa9526c81..4838af32a 100644 --- a/keeper/src/main.rs +++ b/keeper/src/main.rs @@ -23,6 +23,16 @@ use tokio::time; // - I'm really annoyed about Keypair not being clonable. Seems everyone works around that manually. Should make a PR to solana to newtype it and provide that function. // keypair_from_arg_or_env could be a function +#[derive(Parser, Debug)] +#[clap()] +struct CliDotenv { + // When --dotenv is passed, read the specified dotenv file before parsing args + #[clap(long)] + dotenv: std::path::PathBuf, + + remaining_args: Vec, +} + #[derive(Parser)] #[clap()] struct Cli { @@ -64,9 +74,14 @@ fn main() -> Result<(), anyhow::Error> { env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"), ); - dotenv::dotenv().ok(); - - let cli = Cli::parse(); + let args = if let Ok(cli_dotenv) = CliDotenv::try_parse() { + dotenv::from_path(cli_dotenv.dotenv)?; + cli_dotenv.remaining_args + } else { + dotenv::dotenv().ok(); + std::env::args_os().collect() + }; + let cli = Cli::parse_from(args); let payer = keypair_from_path(&cli.payer); diff --git a/liquidator/src/main.rs b/liquidator/src/main.rs index ac7c15156..5e42d17f7 100644 --- a/liquidator/src/main.rs +++ b/liquidator/src/main.rs @@ -53,6 +53,16 @@ impl AnyhowWrap for Result { } } +#[derive(Parser, Debug)] +#[clap()] +struct CliDotenv { + // When --dotenv is passed, read the specified dotenv file before parsing args + #[clap(long)] + dotenv: std::path::PathBuf, + + remaining_args: Vec, +} + #[derive(Parser)] #[clap()] struct Cli { @@ -106,8 +116,14 @@ pub fn encode_address(addr: &Pubkey) -> String { #[tokio::main] async fn main() -> anyhow::Result<()> { - dotenv::dotenv().ok(); - let cli = Cli::parse(); + let args = if let Ok(cli_dotenv) = CliDotenv::try_parse() { + dotenv::from_path(cli_dotenv.dotenv)?; + cli_dotenv.remaining_args + } else { + dotenv::dotenv().ok(); + std::env::args_os().collect() + }; + let cli = Cli::parse_from(args); let liqor_owner = keypair_from_path(&cli.liqor_owner);