Skip to main content

juliaup/
command_config_versionsdbupdate.rs

1use crate::config_file::{load_config_db, load_mut_config_db, save_config_db};
2use crate::utils::{print_juliaup_style, JuliaupMessageType};
3use anyhow::{bail, Context, Result};
4
5pub fn run_command_config_versionsdbupdate(
6    value: Option<i64>,
7    quiet: bool,
8    paths: &crate::global_paths::GlobalPaths,
9) -> Result<()> {
10    match value {
11        Some(value) => {
12            if value < 0 {
13                bail!("Invalid argument.");
14            }
15
16            let mut config_file = load_mut_config_db(paths)
17                .with_context(|| "`config` command failed to load configuration data.")?;
18
19            let mut value_changed = false;
20
21            if value != config_file.data.settings.versionsdb_update_interval {
22                config_file.data.settings.versionsdb_update_interval = value;
23
24                value_changed = true;
25            }
26
27            save_config_db(&mut config_file)
28                .with_context(|| "Failed to save configuration file from `config` command.")?;
29
30            if !quiet {
31                if value_changed {
32                    print_juliaup_style(
33                        "Configure",
34                        &format!("Property 'versionsdbupdateinterval' set to '{}'", value),
35                        JuliaupMessageType::Success,
36                    );
37                } else {
38                    print_juliaup_style(
39                        "Configure",
40                        &format!(
41                            "Property 'versionsdbupdateinterval' is already set to '{}'",
42                            value
43                        ),
44                        JuliaupMessageType::Success,
45                    );
46                }
47            }
48        }
49        None => {
50            let config_file = load_config_db(paths, None)
51                .with_context(|| "`config` command failed to load configuration data.")?;
52
53            if !quiet {
54                eprintln!(
55                    "Property 'versionsdbupdateinterval' set to '{}'",
56                    config_file.data.settings.versionsdb_update_interval
57                );
58            }
59        }
60    };
61
62    Ok(())
63}