Skip to main content

wekan_cli/commands/profile/
add.rs

1use clap::Args;
2
3use crate::{
4    command_result::{CommandSuccess, ProfileItem},
5    config::profiles::{Profile, ProfileStore, canonical_server, parse_profile_name},
6    error::{AppError, ErrorCode},
7};
8
9use super::profile_error;
10
11#[derive(Debug, Args)]
12pub struct AddArgs {
13    #[arg(value_parser = parse_profile_name)]
14    pub name: String,
15    pub url: String,
16    /// Make this profile active, even when other profiles already exist.
17    #[arg(long)]
18    pub r#use: bool,
19}
20
21pub(super) fn execute(
22    args: AddArgs,
23    profile_store: &dyn ProfileStore,
24) -> Result<CommandSuccess, AppError> {
25    let server = canonical_server(&args.url).map_err(AppError::from)?;
26    let mut mutation = profile_store.lock_mutation().map_err(AppError::from)?;
27    if mutation.document().profile(&args.name).is_some() {
28        return Err(profile_error(
29            ErrorCode::ProfileAlreadyExists,
30            &args.name,
31            format!("profile `{}` already exists", args.name),
32        ));
33    }
34
35    let mut document = mutation.document().clone();
36    let activate = document.is_empty() || args.r#use;
37    document.insert(args.name.clone(), Profile::new(server.clone()));
38    if activate {
39        document.set_active_profile(Some(args.name.clone()));
40    }
41    mutation.save(document)?;
42
43    Ok(CommandSuccess::ProfileAdded(ProfileItem {
44        name: args.name,
45        server,
46        active: activate,
47    }))
48}