wash-lib 0.25.1

wasmCloud Shell (wash) libraries
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
//! Implementations for managing contexts within a directory on a filesystem

use std::fs::File;
use std::io::BufReader;
use std::ops::Deref;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};

use crate::config::{cfg_dir, DEFAULT_CTX_DIR_NAME};

use super::{ContextManager, WashContext, HOST_CONFIG_NAME};

const DEFAULT: &str = "default";

/// A concrete type representing a path to a context directory
pub struct ContextDir(PathBuf);

impl AsRef<Path> for ContextDir {
    fn as_ref(&self) -> &Path {
        &self.0
    }
}

impl Deref for ContextDir {
    type Target = Path;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl ContextDir {
    /// Creates and initializes a new `ContextDir` at ~/.wash/contexts
    pub fn new() -> Result<ContextDir> {
        Self::from_dir(None::<&Path>)
    }

    /// Creates and initializes a new [`ContextDir`] at the specified path. If a path is not provided, defaults to ~/.wash/contexts
    pub fn from_dir(path: Option<impl AsRef<Path>>) -> Result<ContextDir> {
        let path = if let Some(path) = path {
            path.as_ref().to_path_buf()
        } else {
            default_context_dir()?
        };

        let exists = path.exists();
        if exists && !path.is_dir() {
            anyhow::bail!(
                "{} is not a directory (or cannot be accessed)",
                path.display()
            )
        } else if !exists {
            std::fs::create_dir_all(&path).context("failed to create context directory")?;
        }

        // Make sure we have the fully qualified path at this point
        let context_dir = path
            .canonicalize()
            .context("failed to canonicalize context directory path")?;

        // Initialize the default context if it doesn't exist
        let default_path = context_dir.join(DEFAULT);
        if !default_path.exists() {
            initialize_context_dir(&context_dir, &default_path)?;
        }

        Ok(ContextDir(context_dir))
    }

    /// Returns a list of paths to all contexts in the context directory
    pub fn list_context_paths(&self) -> Result<Vec<PathBuf>> {
        let entries = std::fs::read_dir(&self.0)?;

        let paths = entries
            .filter_map(|entry| entry.ok().map(|e| e.path()))
            .filter(|path| {
                path.extension()
                    .and_then(|os| os.to_str())
                    .unwrap_or_default()
                    == "json"
            })
            // Filter old index.json files. TODO: remove me after a few releases
            .filter(|path| {
                path.file_stem()
                    .and_then(|os| os.to_str())
                    .unwrap_or_default()
                    != "index"
            })
            .collect();
        Ok(paths)
    }

    /// Returns the full path on disk for the named context
    pub fn get_context_path(&self, name: &str) -> Result<Option<PathBuf>> {
        Ok(self
            .list_context_paths()?
            .into_iter()
            .find(|p| p.file_stem().unwrap_or_default() == name))
    }
}

fn default_context_dir() -> Result<PathBuf> {
    Ok(cfg_dir()?.join(DEFAULT_CTX_DIR_NAME))
}

fn initialize_context_dir(context_dir: &Path, default_path: &PathBuf) -> Result<()> {
    let mut default_context_name = HOST_CONFIG_NAME.to_string();

    // TEMPORARY (TM): look for and parse existing index.json, to preserve backwards compatibility
    if let Ok(index_file) = File::open(context_dir.join("index.json")) {
        #[derive(serde::Deserialize)]
        struct DefaultContext {
            name: String,
        }

        if let Ok(old_default_context) =
            serde_json::from_reader::<_, DefaultContext>(BufReader::new(index_file))
        {
            default_context_name = old_default_context.name;
        }
    }
    // END TEMPORARY (TM)

    std::fs::write(default_path, default_context_name.as_bytes()).with_context(|| {
        format!(
            "failed to write default context to `{}`",
            default_path.display(),
        )
    })?;

    let host_config_path = context_dir.join(format!("{default_context_name}.json"));
    if !host_config_path.exists() {
        let host_config_context = WashContext::named(default_context_name);
        std::fs::write(
            &host_config_path,
            serde_json::to_vec(&host_config_context)
                .context("failed to serialize host_config context")?,
        )
        .with_context(|| {
            format!(
                "failed to write host_config context to `{}`",
                host_config_path.display()
            )
        })?;
    }

    Ok(())
}

impl ContextManager for ContextDir {
    /// Returns the name of the currently set default context
    fn default_context_name(&self) -> Result<String> {
        let raw = std::fs::read(self.0.join(DEFAULT)).context("failed to read default context")?;
        let name = std::str::from_utf8(&raw).context("failed to read default context")?;
        Ok(name.to_string())
    }

    /// Sets the current default context to the given name
    fn set_default_context(&self, name: &str) -> Result<()> {
        self.load_context(name).context("context does not exist")?;

        let default_path = self.0.join(DEFAULT);
        std::fs::write(&default_path, name.as_bytes()).with_context(|| {
            format!(
                "failed to write default context to `{}`",
                default_path.display()
            )
        })
    }

    /// Saves the given context to the context directory. The file will be named `{ctx.name}.json`
    fn save_context(&self, ctx: &WashContext) -> Result<()> {
        let filepath = context_path_from_name(&self.0, &ctx.name);
        std::fs::write(
            &filepath,
            serde_json::to_vec(&ctx).context("failed to serialize context")?,
        )
        .with_context(|| {
            format!(
                "failed to save context `{}` to `{}`",
                ctx.name,
                filepath.display()
            )
        })
    }

    fn delete_context(&self, name: &str) -> Result<()> {
        let path = context_path_from_name(&self.0, name);
        std::fs::remove_file(path).context("failed to remove context")?;
        if self.default_context_name()? == name {
            self.set_default_context(HOST_CONFIG_NAME)?; // reset default
        }
        Ok(())
    }

    /// Loads the currently set default context
    fn load_default_context(&self) -> Result<WashContext> {
        self.load_context(&self.default_context_name()?)
    }

    /// Loads the named context from disk
    fn load_context(&self, name: &str) -> Result<WashContext> {
        let path = context_path_from_name(&self.0, name);
        let file = std::fs::File::open(&path)
            .with_context(|| format!("failed to open context file [{}]", path.display()))?;
        let reader = BufReader::new(file);
        serde_json::from_reader(reader).context("failed to parse context")
    }

    fn list_contexts(&self) -> Result<Vec<String>> {
        Ok(self
            .list_context_paths()?
            .into_iter()
            .filter_map(|p| {
                p.file_stem()
                    .unwrap_or_default()
                    .to_os_string()
                    .into_string()
                    .ok()
            })
            .collect())
    }
}

/// Helper function to properly format the path to a context JSON file
fn context_path_from_name(dir: impl AsRef<Path>, name: &str) -> PathBuf {
    dir.as_ref().join(format!("{name}.json"))
}

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

    #[test]
    fn round_trip_happy_path() {
        let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
        let contexts_path = tempdir.path().join("contexts");
        let ctx_dir = ContextDir::from_dir(Some(&contexts_path))
            .expect("Should be able to create context dir");

        assert!(
            contexts_path.exists() && contexts_path.is_dir(),
            "Non-existent directory should have been created"
        );

        let mut orig_ctx = WashContext {
            name: "happy_path".to_string(),
            lattice: "foobar".to_string(),
            ..Default::default()
        };

        ctx_dir
            .save_context(&orig_ctx)
            .expect("Should be able to save a context to disk");

        let filenames: std::collections::HashSet<String> = contexts_path
            .read_dir()
            .unwrap()
            .filter_map(|entry| entry.unwrap().file_name().clone().into_string().ok())
            .collect();
        let expected_filenames = std::collections::HashSet::from([
            "default".to_string(),
            "host_config.json".to_string(),
            "happy_path.json".to_string(),
        ]);

        assert_eq!(
            filenames, expected_filenames,
            "Newly created context should exist"
        );

        // Now load the context from disk and compare
        let loaded = ctx_dir
            .load_context("happy_path")
            .expect("Should be able to load context from disk");
        assert!(
            orig_ctx.name == loaded.name && orig_ctx.lattice == loaded.lattice,
            "Should have loaded the correct context from disk"
        );

        // Save one more context
        orig_ctx.name = "happy_gilmore".to_string();
        orig_ctx.lattice = "baz".to_string();
        ctx_dir
            .save_context(&orig_ctx)
            .expect("Should be able to save second context");

        assert_eq!(
            contexts_path.read_dir().unwrap().count(),
            4,
            "Directory should have 4 entries"
        );

        ctx_dir
            .set_default_context("happy_gilmore")
            .expect("Should be able to set default context");
        assert_eq!(
            ctx_dir
                .default_context_name()
                .expect("Should be able to load default context"),
            "happy_gilmore",
            "Default context should be correct"
        );

        // Load the default context
        let loaded = ctx_dir
            .load_default_context()
            .expect("Should be able to load default context from disk");
        assert!(
            orig_ctx.name == loaded.name && orig_ctx.lattice == loaded.lattice,
            "Should have loaded the correct context from disk"
        );

        assert_eq!(
            contexts_path.read_dir().unwrap().count(),
            4,
            "Directory should have a new entry from the default context"
        );

        assert!(
            contexts_path.join("default").exists(),
            "default file should exist in directory after setting default context"
        );

        // List the contexts
        let list = ctx_dir
            .list_contexts()
            .expect("Should be able to list contexts");
        assert_eq!(list.len(), 3, "Should only list 3 contexts");
        for ctx in list {
            assert!(
                ctx == "happy_path" || ctx == "happy_gilmore" || ctx == "host_config",
                "Should have found only the contexts we created"
            );
        }

        ctx_dir
            .set_default_context("happy_path")
            .expect("Should be able to set default context");

        assert_eq!(
            ctx_dir
                .default_context_name()
                .expect("Should be able to load default context"),
            "happy_path",
            "Default context should be correct"
        );

        // Delete a context
        ctx_dir
            .delete_context("happy_path")
            .expect("Should be able to delete context");

        assert!(
            !contexts_path.read_dir().unwrap().any(|p| p
                .unwrap()
                .path()
                .as_os_str()
                .to_str()
                .unwrap()
                .contains("happy_path")),
            "Context should have been removed from directory"
        );
    }

    #[test]
    fn load_non_existent_contexts() {
        let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
        let ctx_dir =
            ContextDir::from_dir(Some(&tempdir)).expect("Should be able to create context dir");

        ctx_dir
            .load_default_context()
            .expect("The default context should be automatically created");

        ctx_dir
            .load_context("idontexist")
            .expect_err("Loading a non-existent context should error");
    }

    #[test]
    fn default_context_with_no_settings() {
        let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
        let ctx_dir =
            ContextDir::from_dir(Some(&tempdir)).expect("Should be able to create context dir");

        assert_eq!(
            ctx_dir
                .default_context_name()
                .expect("Should be able to get a default context with nothing set"),
            "host_config",
            "Unset context should return none",
        );

        ctx_dir
            .set_default_context("idontexist")
            .expect_err("Should not be able to set a default context that doesn't exist");
    }

    const PRE_REFACTOR_CONTEXT: &str = r#"{"name":"host_config","cluster_seed":"SCAJ3HQZCDA562YW3VUHHIAUJ2SUCYUNGDCP5DBKQOTEZ6ZZGBKT5NI3DQ","ctl_host":"127.0.0.1","ctl_port":5893,"ctl_jwt":"","ctl_seed":"","ctl_credsfile":null,"ctl_timeout":2000,"ctl_lattice_prefix":"default","rpc_host":"127.0.0.1","rpc_port":5893,"rpc_jwt":"","rpc_seed":"","rpc_credsfile":null,"rpc_timeout":2000,"rpc_lattice_prefix":"default"}"#;

    #[test]
    fn works_with_existing() {
        let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
        std::fs::write(
            tempdir.path().join("host_config.json"),
            PRE_REFACTOR_CONTEXT,
        )
        .expect("Unable to write test data to disk");
        let ctx_dir =
            ContextDir::from_dir(Some(&tempdir)).expect("Should be able to create context dir");

        let ctx = ctx_dir
            .load_context("host_config")
            .expect("Should be able to load a pre-existing context");

        assert!(
            ctx.name == "host_config" && ctx.ctl_port == 5893,
            "Should read the correct data from disk"
        );
    }

    #[test]
    fn delete_default_context() {
        let tempdir = tempfile::tempdir().expect("Unable to create tempdir");
        let ctx_dir =
            ContextDir::from_dir(Some(&tempdir)).expect("Should be able to create context dir");

        let mut ctx = WashContext {
            name: "deleteme".to_string(),
            ..Default::default()
        };

        ctx_dir
            .save_context(&ctx)
            .expect("Should be able to save a context to disk");
        ctx.name = "keepme".to_string();
        ctx_dir
            .save_context(&ctx)
            .expect("Should be able to save a context to disk");

        ctx_dir
            .set_default_context("deleteme")
            .expect("Should be able to set default context");

        assert_eq!(
            tempdir.path().read_dir().unwrap().count(),
            4,
            "Directory should have 4 entries"
        );

        ctx_dir
            .delete_context("deleteme")
            .expect("Should be able to delete context");

        assert_eq!(
            tempdir.path().read_dir().unwrap().count(),
            3,
            "Directory should have 3 entries"
        );

        assert_eq!(
            ctx_dir
                .default_context_name()
                .expect("Should be able to get default context"),
            "host_config",
            "default context should be reset"
        );
    }
}