Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: Improve messaging when config doesn't load
cloudhead committed 2 years ago
commit cc380532c63eefa321c578ba4495844b78f1366f
parent 87debf380e5b13f2d633e9e8c79929152b575d79
4 files changed +31 -25
modified radicle-cli/examples/rad-inspect-noauth.md
@@ -2,7 +2,7 @@ The `rad inspect` command can be run without being authenticated with radicle:

``` (fail)
$ rad self
-
✗ Error: Could not load radicle profile: no profile found at path [..]
+
✗ Error: Radicle profile not found in '[..]'.
✗ Hint: To setup your radicle profile, run `rad auth`.
```

modified radicle-cli/src/commands/help.rs
@@ -50,34 +50,38 @@ impl Args for Options {
}

pub fn run(_options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
-
    println!("Usage: rad <command> [--help]");
+
    term::print("Usage: rad <command> [--help]");

-
    if ctx.profile().is_err() {
-
        println!();
-
        println!(
-
            "{}",
-
            term::format::highlight("It looks like this is your first time using radicle.")
-
        );
-
        println!(
-
            "{}",
-
            term::format::highlight("To get started, use `rad auth` to authenticate.")
-
        );
-
        println!();
+
    if let Err(e) = ctx.profile() {
+
        term::blank();
+
        match e.downcast_ref() {
+
            Some(term::args::Error::WithHint { err, hint }) => {
+
                term::print(term::format::yellow(err));
+
                term::print(term::format::yellow(hint));
+
            }
+
            Some(e) => {
+
                term::error(e);
+
            }
+
            None => {
+
                term::error(e);
+
            }
+
        }
+
        term::blank();
    }

-
    println!("Common `rad` commands used in various situations:");
-
    println!();
+
    term::print("Common `rad` commands used in various situations:");
+
    term::blank();

    for help in COMMANDS {
-
        println!(
+
        term::info!(
            "\t{} {}",
            term::format::bold(format!("{:-12}", help.name)),
            term::format::dim(help.description)
        );
    }
-
    println!();
-
    println!("See `rad <command> --help` to learn about a specific command.");
-
    println!();
+
    term::blank();
+
    term::print("See `rad <command> --help` to learn about a specific command.");
+
    term::blank();

    Ok(())
}
modified radicle-cli/src/terminal.rs
@@ -121,11 +121,13 @@ where
pub fn profile() -> Result<Profile, anyhow::Error> {
    match Profile::load() {
        Ok(profile) => Ok(profile),
-
        Err(e) => Err(args::Error::WithHint {
-
            err: anyhow::anyhow!("Could not load radicle profile: {e}"),
+
        Err(radicle::profile::Error::NotFound(path)) => Err(args::Error::WithHint {
+
            err: anyhow::anyhow!("Radicle profile not found in '{}'.", path.display()),
            hint: "To setup your radicle profile, run `rad auth`.",
        }
        .into()),
+
        Err(radicle::profile::Error::Config(e)) => Err(e.into()),
+
        Err(e) => Err(anyhow::anyhow!("Could not load radicle profile: {e}")),
    }
}

modified radicle/src/profile.rs
@@ -153,10 +153,10 @@ pub enum Error {

#[derive(Debug, Error)]
pub enum ConfigError {
-
    #[error("failed to load node configuration from {0}: {1}")]
+
    #[error("failed to load configuration from {0}: {1}")]
    Io(PathBuf, io::Error),
-
    #[error("failed to decode node configuration from {0}: {1}")]
-
    Json(PathBuf, serde_json::Error),
+
    #[error("failed to load configuration from {0}: {1}")]
+
    Load(PathBuf, serde_json::Error),
}

/// Local radicle configuration.
@@ -200,7 +200,7 @@ impl Config {
    pub fn load(path: &Path) -> Result<Self, ConfigError> {
        match fs::File::open(path) {
            Ok(cfg) => {
-
                serde_json::from_reader(cfg).map_err(|e| ConfigError::Json(path.to_path_buf(), e))
+
                serde_json::from_reader(cfg).map_err(|e| ConfigError::Load(path.to_path_buf(), e))
            }
            Err(e) => {
                let Ok(user) = env::var("USER") else {