wash 0.43.0

wasmCloud Shell (wash) - CLI tool and library for wasmCloud development
Documentation
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Common config constants and functions for loading, finding, and consuming configuration data
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::LazyLock as Lazy;

use anyhow::{Context, Result};
use async_nats::Client;
use etcetera::{app_strategy, AppStrategy, AppStrategyArgs};
use tokio::io::AsyncReadExt;
use wasmcloud_control_interface::{Client as CtlClient, ClientBuilder as CtlClientBuilder};

use crate::lib::context::WashContext;

pub const WASMCLOUD_PID_FILE: &str = "wasmcloud.pid";
pub const WADM_PID_FILE: &str = "wadm.pid";
pub const DEFAULT_NATS_HOST: &str = "127.0.0.1";
pub const DEFAULT_NATS_PORT: &str = "4222";
pub const DEFAULT_LATTICE: &str = "default";
pub const DEFAULT_NATS_TIMEOUT_MS: u64 = 2_000;
pub const DEFAULT_START_COMPONENT_TIMEOUT_MS: u64 = 10_000;
pub const DEFAULT_COMPONENT_OPERATION_TIMEOUT_MS: u64 = 5_000;
pub const DEFAULT_START_PROVIDER_TIMEOUT_MS: u64 = 60_000;

pub static WASH_DIRECTORIES: Lazy<WashAppStrategy> = Lazy::new(|| {
    let args = AppStrategyArgs {
        top_level_domain: "com".to_string(),
        author: "wasmCloud".to_string(),
        app_name: "wash".to_string(),
    };

    let strategy = app_strategy::choose_app_strategy(args).expect("failed to get AppStrategy");
    #[cfg(target_os = "windows")]
    {
        WashAppStrategy::Windows(strategy)
    }
    #[cfg(not(target_os = "windows"))]
    {
        WashAppStrategy::Xdg(strategy)
    }
});

pub enum WashAppStrategy {
    Xdg(app_strategy::Xdg),
    Windows(app_strategy::Windows),
}

impl AppStrategy for WashAppStrategy {
    fn home_dir(&self) -> &std::path::Path {
        match self {
            Self::Xdg(s) => s.home_dir(),
            Self::Windows(s) => s.home_dir(),
        }
    }

    fn config_dir(&self) -> PathBuf {
        var_path("WASH_CONFIG_DIR").unwrap_or_else(|_| match self {
            Self::Xdg(s) => s.config_dir(),
            Self::Windows(s) => s.config_dir(),
        })
    }

    fn data_dir(&self) -> PathBuf {
        var_path("WASH_DATA_DIR").unwrap_or_else(|_| match self {
            Self::Xdg(s) => s.data_dir(),
            Self::Windows(s) => s.data_dir(),
        })
    }

    fn cache_dir(&self) -> PathBuf {
        var_path("WASH_CACHE_DIR").unwrap_or_else(|_| match self {
            Self::Xdg(s) => s.cache_dir(),
            Self::Windows(s) => s.cache_dir(),
        })
    }

    fn state_dir(&self) -> Option<PathBuf> {
        match self {
            Self::Xdg(s) => s.state_dir(),
            Self::Windows(s) => s.state_dir(),
        }
    }

    fn runtime_dir(&self) -> Option<PathBuf> {
        match self {
            Self::Xdg(s) => s.runtime_dir(),
            Self::Windows(s) => s.runtime_dir(),
        }
    }
}

impl WashAppStrategy {
    /// Creates the directory in which to store configuration in and returns the path to it.
    pub fn create_config_dir(&self) -> Result<PathBuf> {
        let path = self.config_dir();
        fs::create_dir_all(&path)
            .with_context(|| format!("failed to create directory `{}`", path.display()))?;
        Ok(path)
    }

    /// Creates the directory in which to store configuration in and returns the path to it
    /// concatenated with a filename.
    pub fn create_in_config_dir<P: AsRef<OsStr>>(&self, path: P) -> Result<PathBuf> {
        self.create_config_dir()?;
        Ok(self.in_config_dir(path))
    }

    /// Returns the path to the directory where to store keys.
    pub fn keys_dir(&self) -> PathBuf {
        var_path("WASH_KEYS_DIR").unwrap_or_else(|_| self.in_data_dir("keys"))
    }

    /// Concatenates a path in the keys directory.
    pub fn in_keys_dir<P: AsRef<std::ffi::OsStr>>(&self, path: P) -> PathBuf {
        let mut basepath = self.keys_dir();
        basepath.push(Path::new(&path));
        basepath
    }

    /// Creates the directory in which to store keys in and returns the path to it.
    pub fn create_keys_dir(&self) -> Result<PathBuf> {
        let path = self.keys_dir();
        fs::create_dir_all(&path)
            .with_context(|| format!("failed to create directory `{}`", path.display()))?;
        Ok(path)
    }

    /// Creates the directory in which to store keys in and returns the path to it
    /// concatenated with a filename.
    pub fn create_in_keys_dir<P: AsRef<OsStr>>(&self, path: P) -> Result<PathBuf> {
        self.create_keys_dir()?;
        Ok(self.in_keys_dir(path))
    }

    /// Returns the path to the directory where to store logs.
    pub fn logs_dir(&self) -> PathBuf {
        var_path("WASH_LOGS_DIR").unwrap_or_else(|_| WASH_DIRECTORIES.in_data_dir("logs"))
    }

    /// Concatenates a path in the logs directory.
    pub fn in_logs_dir<P: AsRef<std::ffi::OsStr>>(&self, path: P) -> PathBuf {
        let mut basepath = self.logs_dir();
        basepath.push(Path::new(&path));
        basepath
    }

    /// Creates the directory in which to store logs in and returns the path to it.
    pub fn create_logs_dir(&self) -> Result<PathBuf> {
        let path = self.logs_dir();
        fs::create_dir_all(&path)
            .with_context(|| format!("failed to create directory `{}`", path.display()))?;
        Ok(path)
    }

    /// Creates the directory in which to store logs in and returns the path to it
    /// concatenated with a filename.
    pub fn create_in_logs_dir<P: AsRef<OsStr>>(&self, path: P) -> Result<PathBuf> {
        self.create_logs_dir()?;
        Ok(self.in_logs_dir(path))
    }

    /// Returns the path to the directory where to store downloads.
    pub fn downloads_dir(&self) -> PathBuf {
        var_path("WASH_DOWNLOADS_DIR").unwrap_or_else(|_| WASH_DIRECTORIES.in_data_dir("downloads"))
    }

    /// Concatenates a path in the downloads directory.
    pub fn in_downloads_dir<P: AsRef<std::ffi::OsStr>>(&self, path: P) -> PathBuf {
        let mut basepath = self.downloads_dir();
        basepath.push(Path::new(&path));
        basepath
    }

    /// Creates the directory in which to store downloads in and returns the path to it.
    pub fn create_downloads_dir(&self) -> Result<PathBuf> {
        let path = self.downloads_dir();
        fs::create_dir_all(&path)
            .with_context(|| format!("failed to create directory `{}`", path.display()))?;
        Ok(path)
    }

    /// Creates the directory in which to store downloads in and returns the path to it
    /// concatenated with a filename.
    pub fn create_in_downloads_dir<P: AsRef<OsStr>>(&self, path: P) -> Result<PathBuf> {
        self.create_downloads_dir()?;
        Ok(self.in_downloads_dir(path))
    }

    /// Returns the path to the directory where to store plugins.
    pub fn plugins_dir(&self) -> PathBuf {
        var_path("WASH_PLUGINS_DIR").unwrap_or_else(|_| WASH_DIRECTORIES.in_data_dir("plugins"))
    }

    /// Concatenates a path in the plugins directory.
    pub fn in_plugins_dir<P: AsRef<std::ffi::OsStr>>(&self, path: P) -> PathBuf {
        let mut basepath = self.plugins_dir();
        basepath.push(Path::new(&path));
        basepath
    }

    /// Creates the directory in which to store plugins in and returns the path to it.
    pub fn create_plugins_dir(&self) -> Result<PathBuf> {
        let path = self.plugins_dir();
        fs::create_dir_all(&path)
            .with_context(|| format!("failed to create directory `{}`", path.display()))?;
        Ok(path)
    }

    /// Creates the directory in which to store plugins in and returns the path to it
    /// concatenated with a filename.
    pub fn create_in_plugins_dir<P: AsRef<OsStr>>(&self, path: P) -> Result<PathBuf> {
        self.create_plugins_dir()?;
        Ok(self.in_plugins_dir(path))
    }

    /// Returns the path to the directory where to store the package cache.
    pub fn package_cache_dir(&self) -> PathBuf {
        var_path("WASH_PACKAGE_CACHE_DIR")
            .unwrap_or_else(|_| WASH_DIRECTORIES.in_cache_dir("package_cache"))
    }

    /// Concatenates a path in the package cache directory.
    pub fn in_package_cache_dir<P: AsRef<std::ffi::OsStr>>(&self, path: P) -> PathBuf {
        let mut basepath = self.package_cache_dir();
        basepath.push(Path::new(&path));
        basepath
    }

    /// Creates the directory in which to store cached packages in and returns the path to it.
    pub fn create_package_cache_dir(&self) -> Result<PathBuf> {
        let path = self.package_cache_dir();
        fs::create_dir_all(&path)
            .with_context(|| format!("failed to create directory `{}`", path.display()))?;
        Ok(path)
    }

    /// Creates the directory in which to store the package cache in and returns the path to it
    /// concatenated with a filename.
    pub fn create_in_package_cache_dir<P: AsRef<OsStr>>(&self, path: P) -> Result<PathBuf> {
        self.create_package_cache_dir()?;
        Ok(self.in_package_cache_dir(path))
    }

    /// Returns the path to the directory where to store the development stuff.
    pub fn dev_dir(&self) -> PathBuf {
        var_path("WASH_DEV_DIR").unwrap_or_else(|_| WASH_DIRECTORIES.in_data_dir("dev"))
    }

    /// Concatenates a path in the development directory.
    pub fn in_dev_dir<P: AsRef<std::ffi::OsStr>>(&self, path: P) -> PathBuf {
        let mut basepath = self.dev_dir();
        basepath.push(Path::new(&path));
        basepath
    }

    /// Creates the directory in which to store dev stuff in and returns the path to it.
    pub fn create_dev_dir(&self) -> Result<PathBuf> {
        let path = self.dev_dir();
        fs::create_dir_all(&path)
            .with_context(|| format!("failed to create directory `{}`", path.display()))?;
        Ok(path)
    }

    /// Creates the directory in which to store dev in and returns the path to it
    /// concatenated with a filename.
    pub fn create_in_dev_dir<P: AsRef<OsStr>>(&self, path: P) -> Result<PathBuf> {
        self.create_dev_dir()?;
        Ok(self.in_dev_dir(path))
    }

    /// Returns the path to the directory where to store contexts.
    pub fn context_dir(&self) -> PathBuf {
        var_path("WASH_CONTEXT_DIR").unwrap_or_else(|_| WASH_DIRECTORIES.in_config_dir("contexts"))
    }

    /// Concatenates a path in the context directory.
    pub fn in_context_dir<P: AsRef<std::ffi::OsStr>>(&self, path: P) -> PathBuf {
        let mut basepath = self.context_dir();
        basepath.push(Path::new(&path));
        basepath
    }

    /// Creates the directory in which to store contexts in and returns the path to it.
    pub fn create_context_dir(&self) -> Result<PathBuf> {
        let path = self.context_dir();
        fs::create_dir_all(&path)
            .with_context(|| format!("failed to create directory `{}`", path.display()))?;
        Ok(path)
    }

    /// Creates the directory in which to store context in and returns the path to it
    /// concatenated with a filename.
    pub fn create_in_context_dir<P: AsRef<OsStr>>(&self, path: P) -> Result<PathBuf> {
        self.create_context_dir()?;
        Ok(self.in_context_dir(path))
    }
}

fn var_path(key: &str) -> Result<PathBuf> {
    std::env::var_os(key)
        .map(PathBuf::from)
        .context("variable not found")
        .and_then(replace_path)
}

fn replace_path<P: AsRef<Path>>(path: P) -> Result<PathBuf> {
    let path = path.as_ref();
    Ok(match path.starts_with("~/") {
        true => {
            let home = etcetera::home_dir().context("no home directory found. Please set $HOME")?;
            home.join(path.strip_prefix("~/").unwrap())
        }
        false => path.to_path_buf(),
    })
}

/// The path to the running wasmCloud Host PID file for wash
pub fn host_pid_file() -> Result<PathBuf> {
    WASH_DIRECTORIES.create_in_downloads_dir(WASMCLOUD_PID_FILE)
}

/// The path to the running wadm PID file for wash
pub fn wadm_pid_file() -> Result<PathBuf> {
    WASH_DIRECTORIES.create_in_downloads_dir(WADM_PID_FILE)
}

#[derive(Clone, Default)]
/// Connection options for a Wash instance
pub struct WashConnectionOptions {
    /// CTL Host for connection, defaults to 127.0.0.1 for local nats
    pub ctl_host: Option<String>,

    /// CTL Port for connections, defaults to 4222 for local nats
    pub ctl_port: Option<String>,

    /// JWT file for CTL authentication. Must be supplied with `ctl_seed`.
    pub ctl_jwt: Option<String>,

    /// Seed file or literal for CTL authentication. Must be supplied with `ctl_jwt`.
    pub ctl_seed: Option<String>,

    /// Credsfile for CTL authentication. Combines `ctl_seed` and `ctl_jwt`.
    /// See [the NATS documentation](https://docs.nats.io/using-nats/developer/connecting/creds) for details.
    pub ctl_credsfile: Option<PathBuf>,

    /// Path to a file containing a CA certificate to use for TLS connections
    pub ctl_tls_ca_file: Option<PathBuf>,

    /// Perform TLS handshake before expecting the server greeting.
    pub ctl_tls_first: Option<bool>,

    /// JS domain for wasmcloud control interface. Defaults to None
    pub js_domain: Option<String>,

    /// Lattice name for wasmcloud control interface, defaults to "default"
    pub lattice: Option<String>,

    /// Timeout length to await a control interface response, defaults to 2000 milliseconds
    pub timeout_ms: u64,

    /// Wash context
    pub ctx: WashContext,
}

impl WashConnectionOptions {
    /// Create a control client from connection options
    pub async fn into_ctl_client(self, auction_timeout_ms: Option<u64>) -> Result<CtlClient> {
        let lattice = self.lattice.unwrap_or_else(|| self.ctx.lattice.clone());

        let ctl_host = self.ctl_host.unwrap_or_else(|| self.ctx.ctl_host.clone());
        let ctl_port = self
            .ctl_port
            .unwrap_or_else(|| self.ctx.ctl_port.to_string());
        let ctl_jwt = self.ctl_jwt.or_else(|| self.ctx.ctl_jwt.clone());
        let ctl_seed = self.ctl_seed.or_else(|| self.ctx.ctl_seed.clone());
        let ctl_credsfile = self
            .ctl_credsfile
            .or_else(|| self.ctx.ctl_credsfile.clone());
        let ctl_tls_ca_file = self
            .ctl_tls_ca_file
            .or_else(|| self.ctx.ctl_tls_ca_file.clone());
        let ctl_tls_first = self
            .ctl_tls_first
            .unwrap_or_else(|| self.ctx.ctl_tls_first.unwrap_or(false));
        let auction_timeout_ms = auction_timeout_ms.unwrap_or(self.timeout_ms);

        let nc = create_nats_client_from_opts(
            &ctl_host,
            &ctl_port,
            ctl_jwt,
            ctl_seed,
            ctl_credsfile,
            ctl_tls_ca_file,
            ctl_tls_first,
        )
        .await
        .context("Failed to create NATS client")?;

        let mut builder = CtlClientBuilder::new(nc)
            .lattice(lattice)
            .timeout(tokio::time::Duration::from_millis(self.timeout_ms))
            .auction_timeout(tokio::time::Duration::from_millis(auction_timeout_ms));

        if let Ok(topic_prefix) = std::env::var("WASMCLOUD_CTL_TOPIC_PREFIX") {
            builder = builder.topic_prefix(topic_prefix);
        }

        let ctl_client = builder.build();

        Ok(ctl_client)
    }

    /// Create a NATS client from `WashConnectionOptions`
    pub async fn into_nats_client(self) -> Result<Client> {
        let ctl_host = self.ctl_host.unwrap_or_else(|| self.ctx.ctl_host.clone());
        let ctl_port = self
            .ctl_port
            .unwrap_or_else(|| self.ctx.ctl_port.to_string());
        let ctl_jwt = self.ctl_jwt.or_else(|| self.ctx.ctl_jwt.clone());
        let ctl_seed = self.ctl_seed.or_else(|| self.ctx.ctl_seed.clone());
        let ctl_credsfile = self
            .ctl_credsfile
            .or_else(|| self.ctx.ctl_credsfile.clone());
        let ctl_tls_ca_file = self
            .ctl_tls_ca_file
            .or_else(|| self.ctx.ctl_tls_ca_file.clone());
        let ctl_tls_first = self
            .ctl_tls_first
            .unwrap_or_else(|| self.ctx.ctl_tls_first.unwrap_or(false));
        let nc = create_nats_client_from_opts(
            &ctl_host,
            &ctl_port,
            ctl_jwt,
            ctl_seed,
            ctl_credsfile,
            ctl_tls_ca_file,
            ctl_tls_first,
        )
        .await?;

        Ok(nc)
    }

    /// Either returns the opts.lattice or opts.ctx.lattice... if both are absent/None,  returns the default lattice prefix (`DEFAULT_LATTICE`).
    #[must_use]
    pub fn get_lattice(&self) -> String {
        self.lattice
            .clone()
            .unwrap_or_else(|| self.ctx.lattice.clone())
    }
}

/// Reads the content of a string if it is a valid file path, otherwise returning the string
async fn extract_arg_value(arg: &str) -> Result<String> {
    match tokio::fs::File::open(arg).await {
        Ok(mut f) => {
            let mut value = String::new();
            f.read_to_string(&mut value)
                .await
                .with_context(|| format!("Failed to read file {}", &arg))?;
            Ok(value)
        }
        Err(_) => Ok(arg.into()),
    }
}

/// Create a NATS client from NATS-related options
pub async fn create_nats_client_from_opts(
    host: &str,
    port: &str,
    jwt: Option<String>,
    seed: Option<String>,
    credsfile: Option<PathBuf>,
    tls_ca_file: Option<PathBuf>,
    tls_first: bool,
) -> Result<Client> {
    let nats_url = format!("{host}:{port}");
    use async_nats::{ConnectOptions, ToServerAddrs};

    let nc = if let Some(jwt_file) = jwt {
        let jwt_contents = extract_arg_value(&jwt_file)
            .await
            .with_context(|| format!("Failed to extract jwt contents from {}", &jwt_file))?;
        let kp = std::sync::Arc::new(if let Some(seed) = seed {
            nkeys::KeyPair::from_seed(
                &extract_arg_value(&seed)
                    .await
                    .with_context(|| format!("Failed to extract seed value {}", &seed))?,
            )
            .with_context(|| format!("Failed to create keypair from seed value {}", &seed))?
        } else {
            nkeys::KeyPair::new_user()
        });

        // You must provide the JWT via a closure
        let mut opts = async_nats::ConnectOptions::with_jwt(jwt_contents, move |nonce| {
            let key_pair = kp.clone();
            async move { key_pair.sign(&nonce).map_err(async_nats::AuthError::new) }
        });

        if let Some(ca_file) = tls_ca_file {
            opts = opts.add_root_certificates(ca_file).require_tls(true);
        }

        if tls_first {
            opts = opts.tls_first();
        }

        opts.name("wash-lib")
            .connect(&nats_url)
            .await
            .with_context(|| {
                format!(
                    "Failed to connect to NATS server {}:{} while creating client",
                    &host, &port
                )
            })?
    } else if let Some(credsfile_path) = credsfile {
        let mut opts = ConnectOptions::with_credentials_file(credsfile_path.clone())
            .await
            .with_context(|| {
                format!(
                    "Failed to authenticate to NATS with credentials file {:?}",
                    &credsfile_path
                )
            })?;

        if let Some(ca_file) = tls_ca_file {
            opts = opts.add_root_certificates(ca_file).require_tls(true);
        }

        if tls_first {
            opts = opts.tls_first();
        }

        opts.name("wash-lib")
            .connect(&nats_url)
            .await
            .with_context(|| {
                format!(
                    "Failed to connect to NATS {} with credentials file {:?}",
                    &nats_url, &credsfile_path
                )
            })?
    } else {
        let mut opts = ConnectOptions::new();

        // If clear text credentials are explicitly embedded in the url by the user, use it
        if let Ok(mut addrs) = nats_url.to_server_addrs() {
            if let Some(addr) = addrs.next() {
                if addr.has_user_pass() {
                    if let (Some(user), Some(pass)) = (addr.username(), addr.password()) {
                        opts = opts.user_and_password(user.to_string(), pass.to_string());
                    }
                }
            }
        }

        if let Some(ca_file) = tls_ca_file {
            opts = opts.add_root_certificates(ca_file).require_tls(true);
        }

        if tls_first {
            opts = opts.tls_first();
        }

        opts.name("wash-lib")
            .connect(&nats_url)
            .await
            .with_context(|| format!("Failed to connect to NATS {}", &nats_url))?
    };
    Ok(nc)
}