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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::{collections::HashMap, error::Error};

use clap::Subcommand;
use serde_json::json;
use tracing::error;
use wash_lib::{
    cli::{input_vec_to_hashmap, CliConnectionOpts, CommandOutput, OutputKind},
    config::WashConnectionOptions,
};
use wasmcloud_secrets_types::SECRET_PREFIX;

use crate::appearance::spinner::Spinner;

#[derive(Debug, Clone, Subcommand)]
#[allow(clippy::enum_variant_names)]
pub enum ConfigCliCommand {
    /// Put a named configuration
    #[clap(name = "put", alias = "create", about = "Put named configuration")]
    PutCommand {
        #[clap(flatten)]
        opts: CliConnectionOpts,
        /// The name of the configuration to put
        #[clap(name = "name")]
        name: String,
        /// The configuration values to put, in the form of `key=value`. Can be specified multiple times, but must be specified at least once.
        #[clap(name = "config_value", required = true)]
        config_values: Vec<String>,
    },
    /// Get a named configuration
    #[clap(name = "get")]
    GetCommand {
        #[clap(flatten)]
        opts: CliConnectionOpts,
        /// The name of the configuration to get
        #[clap(name = "name")]
        name: String,
    },
    /// Delete a named configuration
    #[clap(name = "del", alias = "delete")]
    DelCommand {
        #[clap(flatten)]
        opts: CliConnectionOpts,
        /// The name of the configuration to delete
        #[clap(name = "name")]
        name: String,
    },
}

pub async fn handle_command(
    command: ConfigCliCommand,
    output_kind: OutputKind,
) -> anyhow::Result<CommandOutput> {
    match command {
        ConfigCliCommand::PutCommand {
            opts,
            name,
            config_values,
        } => {
            ensure_not_secret(&name)?;

            put_config(
                opts,
                &name,
                input_vec_to_hashmap(config_values)?,
                output_kind,
            )
            .await
        }
        ConfigCliCommand::GetCommand { opts, name } => {
            ensure_not_secret(&name)?;
            get_config(opts, &name, output_kind).await
        }
        ConfigCliCommand::DelCommand { opts, name } => {
            ensure_not_secret(&name)?;
            delete_config(opts, &name, output_kind).await
        }
    }
}

pub(crate) async fn put_config(
    opts: CliConnectionOpts,
    name: &str,
    values: HashMap<String, String>,
    output_kind: OutputKind,
) -> anyhow::Result<CommandOutput> {
    let sp: Spinner = Spinner::new(&output_kind)?;
    let is_secret = name.starts_with(SECRET_PREFIX);
    let msg = if is_secret {
        "Putting secret configuration ..."
    } else {
        "Putting configuration ..."
    };
    sp.update_spinner_message(msg.to_string());

    let wco: WashConnectionOptions = opts.try_into()?;
    let ctl_client = wco.into_ctl_client(None).await?;
    // Handle no responders by suggesting a host needs to be running
    let config_response = ctl_client
        .put_config(name, values)
        .await
        .map_err(suggest_run_host_error)?;

    sp.finish_and_clear();

    let message = if config_response.message.is_empty() && config_response.success {
        let mut out_name = name;
        let mut config_type = "Configuration";
        if is_secret {
            out_name = out_name
                .strip_prefix(format!("{SECRET_PREFIX}_").as_str())
                .unwrap_or(name);
            config_type = "Secret";
        };
        format!("{config_type} '{out_name}' put successfully.")
    } else {
        config_response
            .message
            .replace(format!("{SECRET_PREFIX}_").as_str(), "")
    };
    let json_out = HashMap::from_iter([
        ("success".to_string(), json!(config_response.success)),
        ("message".to_string(), json!(message)),
    ]);
    let output = CommandOutput::new(message, json_out);

    Ok(output)
}

pub(crate) async fn get_config(
    opts: CliConnectionOpts,
    name: &str,
    output_kind: OutputKind,
) -> anyhow::Result<CommandOutput> {
    let sp: Spinner = Spinner::new(&output_kind)?;
    let is_secret = is_secret(name);
    let config_type = if is_secret { "secret" } else { "configuration" };
    sp.update_spinner_message(format!("Getting {config_type}..."));

    let wco: WashConnectionOptions = opts.try_into()?;
    let ctl_client = wco.into_ctl_client(None).await?;

    let config_response = ctl_client
        .get_config(name)
        .await
        .map_err(suggest_run_host_error)?;

    sp.finish_and_clear();

    if !config_response.message.is_empty() && !config_response.success {
        error!("Error getting {config_type}: {}", config_response.message);
    };

    if let Some(config) = config_response.response {
        Ok(CommandOutput::new(
            format!("{:?}", config),
            config.into_iter().map(|(k, v)| (k, json!(v))).collect(),
        ))
    } else {
        Err(anyhow::anyhow!(
            "No {config_type} found for name: {}",
            name.replace(format!("{SECRET_PREFIX}_").as_str(), "")
        ))
    }
}

pub(crate) async fn delete_config(
    opts: CliConnectionOpts,
    name: &str,
    output_kind: OutputKind,
) -> anyhow::Result<CommandOutput> {
    let sp: Spinner = Spinner::new(&output_kind)?;
    let is_secret = is_secret(name);
    let config_type = if is_secret { "secret" } else { "configuration" };
    sp.update_spinner_message("Deleting {config_type}...".to_string());
    let wco: WashConnectionOptions = opts.try_into()?;
    let ctl_client = wco.into_ctl_client(None).await?;

    let config_response = ctl_client
        .delete_config(name)
        .await
        .map_err(suggest_run_host_error)?;

    sp.finish_and_clear();

    let message = if config_response.message.is_empty() && config_response.success {
        let mut out_name = name;
        if is_secret {
            out_name = out_name
                .strip_prefix(format!("{SECRET_PREFIX}_").as_str())
                .unwrap_or(name);
        };
        format!("{config_type} '{out_name}' deleted successfully.")
    } else {
        config_response
            .message
            .replace(format!("{SECRET_PREFIX}_").as_str(), "")
    };
    let json_out = HashMap::from_iter([
        ("success".to_string(), json!(config_response.success)),
        ("message".to_string(), json!(message)),
    ]);
    let output = CommandOutput::new(message, json_out);

    Ok(output)
}

/// Simple helper function to suggest running a host if no responders are found
fn suggest_run_host_error(e: Box<dyn Error + std::marker::Send + Sync>) -> anyhow::Error {
    let err_str = e.to_string();
    if err_str.contains("no responders") {
        anyhow::anyhow!("No responders found for config put request. Is a host running?")
    } else {
        anyhow::anyhow!(e)
    }
}

fn ensure_not_secret(name: &str) -> anyhow::Result<()> {
    if name.starts_with(SECRET_PREFIX) {
        anyhow::bail!("Configuration names cannot start with '{SECRET_PREFIX}'. Did you mean to use the 'secrets' command?");
    }
    Ok(())
}

pub(crate) fn is_secret(name: &str) -> bool {
    name.starts_with(SECRET_PREFIX)
}