Skip to main content

cli/sys/
profile_commands.rs

1use anyhow::Result;
2
3use crate::{colors, config::Config};
4
5use super::detect::detect_os_id;
6
7pub async fn handle_profile_enable(config: &Config, item_id: &str, dry_run: bool) -> Result<()> {
8    handle_profile_enable_approved(config, item_id, dry_run, true).await
9}
10
11pub async fn handle_profile_disable(config: &Config, item_id: &str, dry_run: bool) -> Result<()> {
12    handle_profile_disable_approved(config, item_id, dry_run, true).await
13}
14
15pub async fn handle_profile_enable_approved(
16    config: &Config,
17    item_id: &str,
18    dry_run: bool,
19    yes: bool,
20) -> Result<()> {
21    handle_profile_state(config, item_id, true, dry_run, yes).await
22}
23
24pub async fn handle_profile_disable_approved(
25    config: &Config,
26    item_id: &str,
27    dry_run: bool,
28    yes: bool,
29) -> Result<()> {
30    handle_profile_state(config, item_id, false, dry_run, yes).await
31}
32
33async fn handle_profile_state(
34    config: &Config,
35    item_id: &str,
36    enabled: bool,
37    dry_run: bool,
38    yes: bool,
39) -> Result<()> {
40    crate::config::print_presets_note(config);
41    let os_id = detect_os_id().await?;
42    let sys_shell: &'static str = config.shell_type.into();
43    let plan_request = shine_core::runtime::SysProfilePlanRequest {
44        os_id: os_id.clone(),
45        item_id: item_id.to_string(),
46        enabled,
47    };
48    let (runtime, reviewed) = if dry_run {
49        (crate::core_runtime::from_config(config).await?, None)
50    } else {
51        let reviewed = crate::lifecycle_plan::review_plans(
52            config,
53            [crate::lifecycle_plan::LifecyclePlanRequest::sys_profile(
54                plan_request.clone(),
55            )],
56            yes,
57        )
58        .await?
59        .into_iter()
60        .next()
61        .expect("one reviewed Sys profile Plan");
62        let runtime = crate::lifecycle_plan::prepare_runtime(config, &reviewed).await?;
63        (runtime, Some(reviewed))
64    };
65    let report = if let Some(reviewed) = reviewed {
66        match crate::lifecycle_plan::execute_reviewed(
67            config,
68            runtime,
69            reviewed,
70            shine_core::frontend::ExecutionOptions::default(),
71            &mut shine_core::runtime::NullObserver,
72            &mut crate::presentation::TerminalInteraction,
73        )
74        .await?
75        {
76            shine_core::frontend::OperationDetails::SysProfile(report) => *report,
77            _ => unreachable!("reviewed operation result type"),
78        }
79    } else {
80        runtime
81            .preview_sys_profile(shine_core::runtime::SysProfileStateRequest {
82                os_id,
83                item_id: item_id.to_string(),
84                enabled,
85                dry_run,
86            })
87            .await?
88    };
89    let action = if enabled { "enable" } else { "disable" };
90    if dry_run {
91        println!(
92            "{} sys/{item_id}: would {action} shell integration for {sys_shell}",
93            colors::dim("[dry-run]")
94        );
95        return Ok(());
96    }
97    let outcome = report
98        .outcome
99        .expect("Core profile mutation returns an outcome");
100    println!(
101        "{} sys/{item_id}: shell integration {} ({})",
102        colors::symbol("✓"),
103        if enabled { "enabled" } else { "disabled" },
104        outcome.detail
105    );
106    Ok(())
107}