wash_cli/common/
start_cmd.rs

1use crate::appearance::spinner::Spinner;
2
3use anyhow::Result;
4use wash_lib::cli::start::{handle_start_component, handle_start_provider, StartCommand};
5use wash_lib::cli::{CommandOutput, OutputKind};
6
7pub async fn handle_command(
8    command: StartCommand,
9    output_kind: OutputKind,
10) -> Result<CommandOutput> {
11    let sp: Spinner = Spinner::new(&output_kind)?;
12    let out: CommandOutput = match command {
13        StartCommand::Component(cmd) => {
14            let component_ref = &cmd.component_ref.to_string();
15
16            sp.update_spinner_message(format!(" Starting component {component_ref} ... "));
17
18            handle_start_component(cmd).await?
19        }
20        StartCommand::Provider(cmd) => {
21            let provider_ref = &cmd.provider_ref.to_string();
22
23            sp.update_spinner_message(format!(" Starting provider {provider_ref} ... "));
24
25            handle_start_provider(cmd).await?
26        }
27    };
28
29    Ok(out)
30}
31
32#[cfg(test)]
33mod test {
34    use super::*;
35
36    use crate::ctl::CtlCliCommand;
37
38    use clap::Parser;
39    use wash_lib::cli::start::{StartComponentCommand, StartProviderCommand};
40
41    #[derive(Parser)]
42    struct Cmd {
43        #[clap(subcommand)]
44        command: CtlCliCommand,
45    }
46
47    const CTL_HOST: &str = "127.0.0.1";
48    const CTL_PORT: &str = "4222";
49    const DEFAULT_LATTICE: &str = "default";
50
51    const HOST_ID: &str = "NCE7YHGI42RWEKBRDJZWXBEJJCFNE5YIWYMSTLGHQBEGFY55BKJ3EG3G";
52
53    #[test]
54    /// Enumerates multiple options of the `ctl` command to ensure API doesn't
55    /// change between versions. This test will fail if any subcommand of `wash ctl`
56    /// changes syntax, ordering of required elements, or flags.
57    fn test_ctl_comprehensive() -> Result<()> {
58        let start_component_all: Cmd = Parser::try_parse_from([
59            "ctl",
60            "start",
61            "component",
62            "--lattice",
63            DEFAULT_LATTICE,
64            "--ctl-host",
65            CTL_HOST,
66            "--ctl-port",
67            CTL_PORT,
68            "--timeout-ms",
69            "2001",
70            "--auction-timeout-ms",
71            "2002",
72            "--constraint",
73            "arch=x86_64",
74            "--host-id",
75            HOST_ID,
76            "wasmcloud.azurecr.io/component:v1",
77            "mycomponent",
78        ])?;
79        match start_component_all.command {
80            CtlCliCommand::Start(StartCommand::Component(StartComponentCommand {
81                opts,
82                host_id,
83                component_ref,
84                component_id,
85                constraints,
86                auction_timeout_ms,
87                ..
88            })) => {
89                assert_eq!(&opts.ctl_host.unwrap(), CTL_HOST);
90                assert_eq!(&opts.ctl_port.unwrap(), CTL_PORT);
91                assert_eq!(&opts.lattice.unwrap(), DEFAULT_LATTICE);
92                assert_eq!(auction_timeout_ms, 2002);
93                assert_eq!(host_id.unwrap(), HOST_ID.to_string());
94                assert_eq!(
95                    component_ref,
96                    "wasmcloud.azurecr.io/component:v1".to_string()
97                );
98                assert_eq!(component_id, "mycomponent".to_string());
99                assert_eq!(constraints.unwrap(), vec!["arch=x86_64".to_string()]);
100            }
101            cmd => panic!("ctl start component constructed incorrect command {cmd:?}"),
102        }
103        let start_provider_all: Cmd = Parser::try_parse_from([
104            "ctl",
105            "start",
106            "provider",
107            "--lattice",
108            DEFAULT_LATTICE,
109            "--ctl-host",
110            CTL_HOST,
111            "--ctl-port",
112            CTL_PORT,
113            "--timeout-ms",
114            "2001",
115            "--auction-timeout-ms",
116            "2002",
117            "--constraint",
118            "arch=x86_64",
119            "--host-id",
120            HOST_ID,
121            "--link-name",
122            "default",
123            "--skip-wait",
124            "wasmcloud.azurecr.io/provider:v1",
125            "providerv1",
126        ])?;
127        match start_provider_all.command {
128            CtlCliCommand::Start(StartCommand::Provider(StartProviderCommand {
129                opts,
130                host_id,
131                provider_ref,
132                provider_id,
133                link_name,
134                constraints,
135                auction_timeout_ms,
136                config,
137                skip_wait,
138            })) => {
139                assert_eq!(&opts.ctl_host.unwrap(), CTL_HOST);
140                assert_eq!(&opts.ctl_port.unwrap(), CTL_PORT);
141                assert_eq!(&opts.lattice.unwrap(), DEFAULT_LATTICE);
142                assert_eq!(opts.timeout_ms, 2001);
143                assert_eq!(auction_timeout_ms, 2002);
144                assert_eq!(link_name, "default".to_string());
145                assert_eq!(constraints.unwrap(), vec!["arch=x86_64".to_string()]);
146                assert_eq!(host_id.unwrap(), HOST_ID.to_string());
147                assert_eq!(provider_ref, "wasmcloud.azurecr.io/provider:v1".to_string());
148                assert_eq!(provider_id, "providerv1".to_string());
149                assert!(config.is_empty());
150                assert!(skip_wait);
151            }
152            cmd => panic!("ctl start provider constructed incorrect command {cmd:?}"),
153        }
154        Ok(())
155    }
156}