From fa154f6b09e642bd8014ceabac9cd794abb2bdee Mon Sep 17 00:00:00 2001 From: Firelight Flagboy Date: Mon, 2 Dec 2024 10:27:32 +0100 Subject: [PATCH] Allow overwriting default config & data directory during CLI command `invite claim` Closes #8937 --- cli/src/commands/invite/claim.rs | 41 +++++++++++++++++++------------- cli/src/macro_opts.rs | 25 +++++++++++++++++++ cli/src/utils.rs | 3 +++ newsfragments/8937.feature.rst | 1 + 4 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 newsfragments/8937.feature.rst diff --git a/cli/src/commands/invite/claim.rs b/cli/src/commands/invite/claim.rs index 82275fe8536..2dcd75af0f5 100644 --- a/cli/src/commands/invite/claim.rs +++ b/cli/src/commands/invite/claim.rs @@ -14,19 +14,18 @@ use libparsec::{ use crate::utils::*; -#[derive(clap::Parser)] -pub struct Args { - // cspell:disable-next-line - /// Server invitation address (e.g.: parsec3://127.0.0.1:41997/Org?no_ssl=true&a=claim_shamir_recovery&p=xBA2FaaizwKy4qG5cGDFlXaL`) - #[arg(short, long)] - addr: ParsecInvitationAddr, - /// Read the password from stdin instead of a TTY. - #[arg(long, default_value_t)] - password_stdin: bool, - /// Use keyring to store the password for the device. - #[arg(long, default_value_t, conflicts_with = "password_stdin")] - use_keyring: bool, -} +crate::clap_parser_with_shared_opts_builder!( + #[with = config_dir, data_dir, password_stdin] + pub struct Args { + // cspell:disable-next-line + /// Server invitation address (e.g.: parsec3://127.0.0.1:41997/Org?no_ssl=true&a=claim_shamir_recovery&p=xBA2FaaizwKy4qG5cGDFlXaL`) + #[arg(short, long)] + addr: ParsecInvitationAddr, + /// Use keyring to store the password for the device. + #[arg(long, default_value_t, conflicts_with = "password_stdin")] + use_keyring: bool, + } +); enum SaveMode { Password { read_from_stdin: bool }, @@ -35,6 +34,8 @@ enum SaveMode { pub async fn main(args: Args) -> anyhow::Result<()> { let Args { + config_dir, + data_dir, addr, password_stdin, use_keyring, @@ -47,7 +48,12 @@ pub async fn main(args: Args) -> anyhow::Result<()> { read_from_stdin: password_stdin, } }; - let ctx = step0(addr).await?; + let config = ClientConfig { + config_dir, + data_base_dir: data_dir, + ..Default::default() + }; + let ctx = step0(addr, config).await?; match ctx { UserOrDeviceClaimInitialCtx::User(ctx) => { @@ -68,10 +74,13 @@ pub async fn main(args: Args) -> anyhow::Result<()> { } /// Step 0: retrieve info -async fn step0(addr: ParsecInvitationAddr) -> anyhow::Result { +async fn step0( + addr: ParsecInvitationAddr, + config: ClientConfig, +) -> anyhow::Result { let mut handle = start_spinner("Retrieving invitation info".into()); - let ctx = claimer_retrieve_info(Arc::new(ClientConfig::default().into()), addr, None).await?; + let ctx = claimer_retrieve_info(Arc::new(config.into()), addr, None).await?; handle.stop_with_newline(); diff --git a/cli/src/macro_opts.rs b/cli/src/macro_opts.rs index 6bdd9d16644..362fd383af9 100644 --- a/cli/src/macro_opts.rs +++ b/cli/src/macro_opts.rs @@ -58,6 +58,31 @@ macro_rules! clap_parser_with_shared_opts_builder { } ); }; + // data dir option + ( + #[with = data_dir $(,$modifier:ident)*] + $(#[$struct_attr:meta])* + $visibility:vis struct $name:ident { + $( + $(#[$field_attr:meta])* + $field_vis:vis $field:ident: $field_type:ty, + )* + } + ) => { + $crate::clap_parser_with_shared_opts_builder!( + #[with = $($modifier),*] + $(#[$struct_attr])* + $visibility struct $name { + #[doc = "Parsec data directory"] + #[arg(short, long, default_value_os_t = libparsec::get_default_data_base_dir(), env = $crate::utils::PARSEC_DATA_DIR)] + pub(crate) data_dir: std::path::PathBuf, + $( + $(#[$field_attr])* + $field_vis $field: $field_type, + )* + } + ); + }; // Device option ( #[with = device $(,$modifier:ident)*] diff --git a/cli/src/utils.rs b/cli/src/utils.rs index 7480abaf5f8..52e8b2ced2f 100644 --- a/cli/src/utils.rs +++ b/cli/src/utils.rs @@ -16,6 +16,9 @@ use spinners::{Spinner, Spinners, Stream}; /// Environment variable to set the Parsec config directory /// Should not be confused with [`libparsec::PARSEC_BASE_CONFIG_DIR`] pub const PARSEC_CONFIG_DIR: &str = "PARSEC_CONFIG_DIR"; +/// Environment variable to set the Parsec data directory +/// Should not be confused with [`libparsec::PARSEC_BASE_DATA_DIR`] +pub const PARSEC_DATA_DIR: &str = "PARSEC_DATA_DIR"; pub const GREEN: &str = "\x1B[92m"; pub const RED: &str = "\x1B[91m"; diff --git a/newsfragments/8937.feature.rst b/newsfragments/8937.feature.rst new file mode 100644 index 00000000000..6a005bfb138 --- /dev/null +++ b/newsfragments/8937.feature.rst @@ -0,0 +1 @@ +Allows overwriting default config & data directory during CLI command ``invite claim``