summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge/src/cli/config/cli.rs
blob: dff15e471073fe8a29e74fce50a8b3c9941f08bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::cli::config::{commands::*, config_key::*};
use crate::command::*;
use crate::ConfigError;
use tedge_config::ConfigRepository;

#[derive(clap::Subcommand, Debug)]
pub enum ConfigCmd {
    /// Get the value of the provided configuration key
    Get {
        /// Configuration key. Run `tedge config list --doc` for available keys
        key: ConfigKey,
    },

    /// Set or update the provided configuration key with the given value
    Set {
        /// Configuration key. Run `tedge config list --doc` for available keys
        key: ConfigKey,

        /// Configuration value.
        value: String,
    },

    /// Unset the provided configuration key
    Unset {
        /// Configuration key. Run `tedge config list --doc` for available keys
        key: ConfigKey,
    },

    /// Print the configuration keys and their values
    List {
        /// Prints all the configuration keys, even those without a configured value
        #[clap(long = "all")]
        is_all: bool,

        /// Prints all keys and descriptions with example values
        #[clap(long = "doc")]
        is_doc: bool,
    },
}

impl BuildCommand for ConfigCmd {
    fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, ConfigError> {
        let config = context.config_repository.load()?;
        let config_repository = context.config_repository;

        match self {
            ConfigCmd::Get { key } => Ok(GetConfigCommand {
                config_key: key,
                config,
            }
            .into_boxed()),
            ConfigCmd::Set { key, value } => Ok(SetConfigCommand {
                config_key: key,
                value,
                config_repository,
            }
            .into_boxed()),
            ConfigCmd::Unset { key } => Ok(UnsetConfigCommand {
                config_key: key,
                config_repository,
            }
            .into_boxed()),
            ConfigCmd::List { is_all, is_doc } => Ok(ListConfigCommand {
                is_all,
                is_doc,
                config_keys: ConfigKey::list_all(),
                config,
            }
            .into_boxed()),
        }
    }
}