cli: Improve messaging when config doesn't load

Instead of telling users to setup their profile, point out the actual
problem with the config.
This commit is contained in:
cloudhead 2023-12-11 15:10:51 +01:00
parent 87debf380e
commit cc380532c6
No known key found for this signature in database
4 changed files with 31 additions and 25 deletions

View File

@ -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`.
```

View File

@ -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(())
}

View File

@ -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}")),
}
}

View File

@ -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 {