wash-cli 0.20.2

wasmcloud Shell (wash) CLI tool
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use std::{
    collections::HashMap,
    io::{Error, ErrorKind},
    path::PathBuf,
    process::Command,
};

use anyhow::{bail, Result};
use clap::{Args, Subcommand};
use log::warn;
use serde_json::json;
use wash_lib::{
    cli::CommandOutput,
    config::{
        DEFAULT_LATTICE_PREFIX, DEFAULT_NATS_HOST, DEFAULT_NATS_PORT, DEFAULT_NATS_TIMEOUT_MS,
    },
    context::{fs::ContextDir, ContextManager, WashContext, HOST_CONFIG_NAME},
    id::ClusterSeed,
};

use wash_lib::{
    config::context_dir,
    context::ensure_host_config_context,
    generate::{
        interactive::{prompt_for_choice, user_question},
        project_variables::StringEntry,
    },
};

pub(crate) async fn handle_command(ctx_cmd: CtxCommand) -> Result<CommandOutput> {
    use CtxCommand::*;
    match ctx_cmd {
        List(cmd) => handle_list(cmd),
        Default(cmd) => handle_default(cmd),
        Edit(cmd) => handle_edit(cmd),
        New(cmd) => handle_new(cmd),
        Del(cmd) => handle_del(cmd),
    }
}

#[derive(Subcommand, Debug, Clone)]
pub(crate) enum CtxCommand {
    /// Lists all stored contexts (JSON files) found in the context directory, with the exception of index.json
    #[clap(name = "list")]
    List(ListCommand),
    /// Delete a stored context
    #[clap(name = "del")]
    Del(DelCommand),
    /// Create a new context
    #[clap(name = "new")]
    New(NewCommand),
    /// Set the default context
    #[clap(name = "default")]
    Default(DefaultCommand),
    /// Edit a context directly using a text editor
    #[clap(name = "edit")]
    Edit(EditCommand),
}

#[derive(Args, Debug, Clone)]
pub(crate) struct ListCommand {
    /// Location of context files for managing. Defaults to $WASH_CONTEXTS ($HOME/.wash/contexts)
    #[clap(long = "directory", env = "WASH_CONTEXTS", hide_env_values = true)]
    directory: Option<PathBuf>,
}

#[derive(Args, Debug, Clone)]
pub(crate) struct DelCommand {
    /// Location of context files for managing. Defaults to $WASH_CONTEXTS ($HOME/.wash/contexts)
    #[clap(long = "directory", env = "WASH_CONTEXTS", hide_env_values = true)]
    directory: Option<PathBuf>,

    /// Name of the context to delete. If not supplied, the user will be prompted to select an existing context
    #[clap(name = "name")]
    name: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub(crate) struct NewCommand {
    /// Name of the context, will be sanitized to ensure it's a valid filename
    #[clap(name = "name", required_unless_present("interactive"))]
    pub(crate) name: Option<String>,

    /// Location of context files for managing. Defaults to $WASH_CONTEXTS ($HOME/.wash/contexts)
    #[clap(long = "directory", env = "WASH_CONTEXTS", hide_env_values = true)]
    directory: Option<PathBuf>,

    /// Create the context in an interactive terminal prompt, instead of an autogenerated default context
    #[clap(long = "interactive", short = 'i')]
    interactive: bool,
}

#[derive(Args, Debug, Clone)]
pub(crate) struct DefaultCommand {
    /// Location of context files for managing. Defaults to $WASH_CONTEXTS ($HOME/.wash/contexts)
    #[clap(long = "directory", env = "WASH_CONTEXTS", hide_env_values = true)]
    directory: Option<PathBuf>,

    /// Name of the context to use for default. If not supplied, the user will be prompted to select a default
    #[clap(name = "name")]
    name: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub(crate) struct EditCommand {
    /// Location of context files for managing. Defaults to $WASH_CONTEXTS ($HOME/.wash/contexts)
    #[clap(long = "directory", env = "WASH_CONTEXTS", hide_env_values = true)]
    directory: Option<PathBuf>,

    /// Name of the context to edit, if not supplied the user will be prompted to select a context
    #[clap(name = "name")]
    pub(crate) name: Option<String>,

    /// Your terminal text editor of choice. This editor must be present in your $PATH, or an absolute filepath.
    #[clap(short = 'e', long = "editor", env = "EDITOR")]
    pub(crate) editor: String,
}

/// Lists all JSON files found in the context directory, with the exception of `index.json`
/// Being present in this list does not guarantee a valid context
fn handle_list(cmd: ListCommand) -> Result<CommandOutput> {
    let dir = ContextDir::new(context_dir(cmd.directory)?)?;
    ensure_host_config_context(&dir)?;

    let default_context = dir
        .default_context()?
        .unwrap_or_else(|| HOST_CONFIG_NAME.to_string());
    let contexts = dir.list_contexts()?;

    let text_contexts = contexts
        .iter()
        .map(|f| {
            if f == &default_context {
                format!("{f} (default)")
            } else {
                f.clone()
            }
        })
        .collect::<Vec<String>>()
        .join("\n");

    let mut map = HashMap::new();
    map.insert("contexts".to_string(), json!(contexts));
    map.insert("default".to_string(), json!(default_context));

    Ok(CommandOutput::new(
        format!(
            "== Contexts found in {} ==\n{}",
            dir.display(),
            text_contexts
        ),
        map,
    ))
}

/// Handles selecting a default context, which can be selected in the terminal or provided as an argument
fn handle_default(cmd: DefaultCommand) -> Result<CommandOutput> {
    let dir = ContextDir::new(context_dir(cmd.directory)?)?;
    ensure_host_config_context(&dir)?;

    let new_default = if let Some(n) = cmd.name {
        n
    } else {
        select_context(&dir, "Select a default context:")?.unwrap_or_default()
    };

    dir.set_default_context(&new_default)?;
    Ok(CommandOutput::from("Set new context successfully"))
}

/// Handles deleting an existing context
fn handle_del(cmd: DelCommand) -> Result<CommandOutput> {
    let dir = ContextDir::new(context_dir(cmd.directory)?)?;

    let ctx_to_delete = if let Some(n) = cmd.name {
        n
    } else {
        select_context(&dir, "Select a context to delete:")?.unwrap_or_default()
    };

    dir.delete_context(&ctx_to_delete)?;
    Ok(CommandOutput::from("Removed file successfully"))
}

/// Handles creating a new context by writing the default WashContext object to the specified path
fn handle_new(cmd: NewCommand) -> Result<CommandOutput> {
    let dir = ContextDir::new(context_dir(cmd.directory)?)?;

    let mut new_context = if cmd.interactive {
        prompt_for_context()?
    } else {
        WashContext::named(cmd.name.unwrap())
    };

    let options = sanitize_filename::Options {
        truncate: true,
        windows: true,
        replacement: "_",
    };

    // Ensure filename doesn't include uphill/downhill\ slashes, or reserved prefixes
    let sanitized = sanitize_filename::sanitize_with_options(&new_context.name, options);
    new_context.name = sanitized;
    dir.save_context(&new_context)?;
    Ok(CommandOutput::from(format!(
        "Created context {} with default values",
        new_context.name
    )))
}

/// Handles editing a context by opening the JSON file in the user's text editor of choice
fn handle_edit(cmd: EditCommand) -> Result<CommandOutput> {
    let dir = ContextDir::new(context_dir(cmd.directory)?)?;
    let editor = which::which(cmd.editor)?;

    ensure_host_config_context(&dir)?;

    let mut ctx_name = String::new();

    let ctx = if let Some(ctx) = cmd.name {
        let path = dir.get_context_path(&ctx)?;
        ctx_name = ctx;
        path
    } else if let Some(name) = select_context(&dir, "Select a context to edit:")? {
        let path = dir.get_context_path(&name)?;
        ctx_name = name;
        path
    } else {
        None
    };

    if let Some(path) = ctx {
        if ctx_name == HOST_CONFIG_NAME {
            warn!("Edits to the host_config context will be overwritten, make changes to the host config instead");
        }
        let status = Command::new(editor).arg(&path).status()?;

        match status.success() {
            true => Ok(CommandOutput::from("Finished editing context successfully")),
            false => bail!("Failed to edit context"),
        }
    } else {
        Err(Error::new(
            ErrorKind::NotFound,
            "Unable to find context supplied, please ensure it exists".to_string(),
        )
        .into())
    }
}

/// Prompts the user with the provided `contexts` choices and returns the user's response.
/// This can be used to determine which context to delete, edit, or set as a default, for example
fn select_context(dir: &ContextDir, prompt: &str) -> Result<Option<String>> {
    let default = dir
        .default_context()?
        .unwrap_or_else(|| HOST_CONFIG_NAME.to_string());

    let choices: Vec<String> = dir.list_contexts()?;

    let entry = StringEntry {
        default: Some(default),
        choices: Some(choices.clone()),
        regex: None,
    };

    if let Ok(choice) = prompt_for_choice(&entry, prompt) {
        Ok(choices.get(choice).map(|c| c.to_string()))
    } else {
        Ok(None)
    }
}

/// Prompts the user interactively for context values, returning a constructed context
fn prompt_for_context() -> Result<WashContext> {
    let name = user_question(
        "What do you want to name the context?",
        &Some("default".to_string()),
    )?;

    let cluster_seed = match user_question(
        "What cluster seed do you want to use to sign invocations?",
        &Some(String::new()),
    ) {
        Ok(s) if s.is_empty() => None,
        Ok(s) => Some(s.parse::<ClusterSeed>()?),
        _ => None,
    };
    let ctl_host = user_question(
        "What is the control interface connection host?",
        &Some(DEFAULT_NATS_HOST.to_string()),
    )?;
    let ctl_port = user_question(
        "What is the control interface connection port?",
        &Some(DEFAULT_NATS_PORT.to_string()),
    )?;
    let ctl_jwt = match user_question(
        "Enter your JWT that you use to authenticate to the control interface connection, if applicable",
        &Some(String::new()),
    ) {
        Ok(s) if s.is_empty() => None,
        Ok(s) => Some(s),
        _ => None,
    };
    let ctl_seed = match user_question(
        "Enter your user seed that you use to authenticate to the control interface connection, if applicable",
        &Some(String::new()),
    ) {
        Ok(s) if s.is_empty() => None,
        Ok(s) => Some(s),
        _ => None,
    };
    let ctl_credsfile = match user_question(
        "Enter the absolute path to control interface connection credsfile, if applicable",
        &Some(String::new()),
    ) {
        Ok(s) if s.is_empty() => None,
        Ok(s) => Some(s),
        _ => None,
    };
    let ctl_timeout = user_question(
        "What should the control interface timeout be (in milliseconds)?",
        &Some(DEFAULT_NATS_TIMEOUT_MS.to_string()),
    )?;

    let lattice_prefix = user_question(
        "What is the lattice prefix that the host will communicate on?",
        &Some(DEFAULT_LATTICE_PREFIX.to_string()),
    )?;

    let js_domain = match user_question(
        "What JetStream domain will the host be running, if any?",
        &Some(String::new()),
    ) {
        Ok(s) if s.is_empty() => None,
        Ok(s) => Some(s),
        _ => None,
    };

    let rpc_host = user_question(
        "What is the RPC host?",
        &Some(DEFAULT_NATS_HOST.to_string()),
    )?;
    let rpc_port = user_question(
        "What is the RPC connection port?",
        &Some(DEFAULT_NATS_PORT.to_string()),
    )?;
    let rpc_jwt = match user_question(
        "Enter your JWT that you use to authenticate to the RPC connection, if applicable",
        &Some(String::new()),
    ) {
        Ok(s) if s.is_empty() => None,
        Ok(s) => Some(s),
        _ => None,
    };
    let rpc_seed = match user_question(
        "Enter your user seed that you use to authenticate to the RPC connection, if applicable",
        &Some(String::new()),
    ) {
        Ok(s) if s.is_empty() => None,
        Ok(s) => Some(s),
        _ => None,
    };
    let rpc_credsfile = match user_question(
        "Enter the absolute path to RPC connection credsfile, if applicable",
        &Some(String::new()),
    ) {
        Ok(s) if s.is_empty() => None,
        Ok(s) => Some(s),
        _ => None,
    };
    let rpc_timeout = user_question(
        "What should the RPC timeout be (in milliseconds)?",
        &Some(DEFAULT_NATS_TIMEOUT_MS.to_string()),
    )?;

    Ok(WashContext {
        name,
        cluster_seed,
        ctl_host,
        ctl_port: ctl_port.parse().unwrap_or_default(),
        ctl_jwt,
        ctl_seed,
        ctl_credsfile: ctl_credsfile.map(PathBuf::from),
        ctl_timeout: ctl_timeout.parse()?,
        lattice_prefix,
        js_domain,
        rpc_host,
        rpc_port: rpc_port.parse().unwrap_or_default(),
        rpc_jwt,
        rpc_seed,
        rpc_credsfile: rpc_credsfile.map(PathBuf::from),
        rpc_timeout: rpc_timeout.parse()?,
    })
}

#[cfg(test)]
mod test {
    use super::*;
    use clap::Parser;

    #[derive(Parser)]
    struct Cmd {
        #[clap(subcommand)]
        cmd: CtxCommand,
    }
    #[test]
    // Enumerates all options of ctx subcommands to ensure
    // changes are not made to the ctx API
    fn test_ctx_comprehensive() {
        let cmd: Cmd = Parser::try_parse_from([
            "ctx",
            "new",
            "my_name",
            "--interactive",
            "--directory",
            "./contexts",
        ])
        .unwrap();

        match cmd.cmd {
            CtxCommand::New(cmd) => {
                assert_eq!(cmd.directory.unwrap(), PathBuf::from("./contexts"));
                assert!(cmd.interactive);
                assert_eq!(cmd.name.unwrap(), "my_name");
            }
            _ => panic!("ctx constructed incorrect command"),
        }

        let cmd: Cmd = Parser::try_parse_from([
            "ctx",
            "edit",
            "my_context",
            "--editor",
            "vim",
            "--directory",
            "./contexts",
        ])
        .unwrap();
        match cmd.cmd {
            CtxCommand::Edit(cmd) => {
                assert_eq!(cmd.directory.unwrap(), PathBuf::from("./contexts"));
                assert_eq!(cmd.editor, "vim");
                assert_eq!(cmd.name.unwrap(), "my_context");
            }
            _ => panic!("ctx constructed incorrect command"),
        }

        let cmd: Cmd =
            Parser::try_parse_from(["ctx", "del", "my_context", "--directory", "./contexts"])
                .unwrap();
        match cmd.cmd {
            CtxCommand::Del(cmd) => {
                assert_eq!(cmd.directory.unwrap(), PathBuf::from("./contexts"));
                assert_eq!(cmd.name.unwrap(), "my_context");
            }
            _ => panic!("ctx constructed incorrect command"),
        }

        let cmd: Cmd =
            Parser::try_parse_from(["ctx", "list", "--directory", "./contexts"]).unwrap();
        match cmd.cmd {
            CtxCommand::List(cmd) => {
                assert_eq!(cmd.directory.unwrap(), PathBuf::from("./contexts"));
            }
            _ => panic!("ctx constructed incorrect command"),
        }

        let cmd: Cmd =
            Parser::try_parse_from(["ctx", "default", "host_config", "--directory", "./contexts"])
                .unwrap();
        match cmd.cmd {
            CtxCommand::Default(cmd) => {
                assert_eq!(cmd.directory.unwrap(), PathBuf::from("./contexts"));
                assert_eq!(cmd.name.unwrap(), "host_config");
            }
            _ => panic!("ctx constructed incorrect command"),
        }
    }
}