From fef5fc4b0efde7f9c55c02bb24106b72456fbcf8 Mon Sep 17 00:00:00 2001 From: FirelightFlagboy <30697622+FirelightFlagboy@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:14:44 +0100 Subject: [PATCH] Backport: Allow overwriting default config & data directory during CLI command `invite claim` (#9082) Closes #8937 --- cli/src/commands/invite/claim.rs | 39 ++++++++++++++++++++------------ cli/src/macro_opts.rs | 25 ++++++++++++++++++++ cli/src/utils.rs | 3 +++ newsfragments/8937.feature.rst | 1 + 4 files changed, 53 insertions(+), 15 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 dba8cacde48..1cec34e96d0 100644 --- a/cli/src/commands/invite/claim.rs +++ b/cli/src/commands/invite/claim.rs @@ -14,18 +14,17 @@ use libparsec::{ use crate::utils::*; -#[derive(clap::Parser)] -pub struct Args { - /// Server invitation address (e.g.: parsec3://127.0.0.1:41905/Org?no_ssl=true&action=claim_user&token=4e45cc21e7604af196173ff6c9184a1f) - #[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 { + /// Server invitation address (e.g.: parsec3://127.0.0.1:41905/Org?no_ssl=true&action=claim_user&token=4e45cc21e7604af196173ff6c9184a1f) + #[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 }, @@ -34,6 +33,8 @@ enum SaveMode { pub async fn main(args: Args) -> anyhow::Result<()> { let Args { + config_dir, + data_dir, addr, password_stdin, use_keyring, @@ -46,7 +47,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) => { @@ -67,10 +73,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``