iroh_docs/cli/
authors.rs

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
//! Define the commands to manage authors.

use anyhow::{bail, Result};
use clap::Parser;
use derive_more::FromStr;
use futures_lite::StreamExt;

use super::{AuthorsClient, ConsoleEnv};
use crate::{cli::fmt_short, Author, AuthorId};

#[allow(missing_docs)]
/// Commands to manage authors.
#[derive(Debug, Clone, Parser)]
pub enum AuthorCommands {
    /// Set the active author (Note: only works within the Iroh console).
    Switch { author: AuthorId },
    /// Create a new author.
    Create {
        /// Switch to the created author (Note: only works in the Iroh console).
        #[clap(long)]
        switch: bool,
    },
    /// Delete an author.
    Delete { author: AuthorId },
    /// Export an author.
    Export { author: AuthorId },
    /// Import an author.
    Import { author: String },
    /// Print the default author for this node.
    Default {
        /// Switch to the default author (Note: only works in the Iroh console).
        #[clap(long)]
        switch: bool,
    },
    /// List authors.
    #[clap(alias = "ls")]
    List,
}

impl AuthorCommands {
    /// Runs the author command given an iroh client and console environment.
    pub async fn run(self, authors: &AuthorsClient, env: &ConsoleEnv) -> Result<()> {
        match self {
            Self::Switch { author } => {
                env.set_author(author)?;
                println!("Active author is now {}", fmt_short(author.as_bytes()));
            }
            Self::List => {
                let mut stream = authors.list().await?;
                while let Some(author_id) = stream.try_next().await? {
                    println!("{}", author_id);
                }
            }
            Self::Default { switch } => {
                if switch && !env.is_console() {
                    bail!("The --switch flag is only supported within the Iroh console.");
                }
                let author_id = authors.default().await?;
                println!("{}", author_id);
                if switch {
                    env.set_author(author_id)?;
                    println!("Active author is now {}", fmt_short(author_id.as_bytes()));
                }
            }
            Self::Create { switch } => {
                if switch && !env.is_console() {
                    bail!("The --switch flag is only supported within the Iroh console.");
                }

                let author_id = authors.create().await?;
                println!("{}", author_id);

                if switch {
                    env.set_author(author_id)?;
                    println!("Active author is now {}", fmt_short(author_id.as_bytes()));
                }
            }
            Self::Delete { author } => {
                authors.delete(author).await?;
                println!("Deleted author {}", fmt_short(author.as_bytes()));
            }
            Self::Export { author } => match authors.export(author).await? {
                Some(author) => {
                    println!("{}", author);
                }
                None => {
                    println!("No author found {}", fmt_short(author.as_bytes()));
                }
            },
            Self::Import { author } => match Author::from_str(&author) {
                Ok(author) => {
                    let id = author.id();
                    authors.import(author).await?;
                    println!("Imported {}", fmt_short(id.as_bytes()));
                }
                Err(err) => {
                    eprintln!("Invalid author key: {}", err);
                }
            },
        }
        Ok(())
    }
}