Skip to main content

wekan_cli/commands/profile/
update.rs

1use clap::Args;
2
3use crate::{
4    command_result::{CommandSuccess, ProfileItem},
5    config::profiles::{Profile, ProfileStore, canonical_server, parse_profile_name},
6    credentials::CredentialStore,
7    error::AppError,
8};
9
10use super::{ensure_logged_out, profile_not_found};
11
12#[derive(Debug, Args)]
13pub struct UpdateArgs {
14    #[arg(value_parser = parse_profile_name)]
15    pub name: String,
16    pub url: String,
17}
18
19pub(super) fn execute(
20    args: UpdateArgs,
21    profile_store: &dyn ProfileStore,
22    credential_store: &dyn CredentialStore,
23) -> Result<CommandSuccess, AppError> {
24    let server = canonical_server(&args.url).map_err(AppError::from)?;
25    let mut mutation = profile_store.lock_mutation().map_err(AppError::from)?;
26    let credential_namespace = profile_store
27        .credential_namespace()
28        .map_err(AppError::from)?;
29    let current = mutation
30        .document()
31        .profile(&args.name)
32        .ok_or_else(|| profile_not_found(&args.name))?;
33    let active = mutation.document().active_profile() == Some(args.name.as_str());
34    if current.server() == server {
35        return Ok(CommandSuccess::ProfileUpdated(ProfileItem {
36            name: args.name,
37            server,
38            active,
39        }));
40    }
41
42    ensure_logged_out(
43        credential_store,
44        &args.name,
45        current.server(),
46        &credential_namespace,
47    )?;
48    let mut document = mutation.document().clone();
49    document.insert(args.name.clone(), Profile::new(server.clone()));
50    mutation.save(document)?;
51    Ok(CommandSuccess::ProfileUpdated(ProfileItem {
52        name: args.name,
53        server,
54        active,
55    }))
56}