Skip to content

Commit

Permalink
Backport: Allow overwriting default config & data directory during CL…
Browse files Browse the repository at this point in the history
…I command `invite claim` (#9082)

Closes #8937
  • Loading branch information
FirelightFlagboy authored Dec 9, 2024
1 parent 6cb0899 commit fef5fc4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
39 changes: 24 additions & 15 deletions cli/src/commands/invite/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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,
Expand All @@ -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) => {
Expand All @@ -67,10 +73,13 @@ pub async fn main(args: Args) -> anyhow::Result<()> {
}

/// Step 0: retrieve info
async fn step0(addr: ParsecInvitationAddr) -> anyhow::Result<UserOrDeviceClaimInitialCtx> {
async fn step0(
addr: ParsecInvitationAddr,
config: ClientConfig,
) -> anyhow::Result<UserOrDeviceClaimInitialCtx> {
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();

Expand Down
25 changes: 25 additions & 0 deletions cli/src/macro_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*]
Expand Down
3 changes: 3 additions & 0 deletions cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions newsfragments/8937.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allows overwriting default config & data directory during CLI command ``invite claim``

0 comments on commit fef5fc4

Please sign in to comment.