Skip to content

Commit

Permalink
Allow overwriting default config & data directory during CLI command …
Browse files Browse the repository at this point in the history
…`invite claim`

Closes #8937
  • Loading branch information
FirelightFlagboy committed Dec 2, 2024
1 parent ef2a52f commit fa154f6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
41 changes: 25 additions & 16 deletions cli/src/commands/invite/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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,
Expand All @@ -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) => {
Expand All @@ -68,10 +74,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 fa154f6

Please sign in to comment.