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) ``` (fail)
$ rad self $ 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`. ✗ 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<()> { 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() { if let Err(e) = ctx.profile() {
println!(); term::blank();
println!( match e.downcast_ref() {
"{}", Some(term::args::Error::WithHint { err, hint }) => {
term::format::highlight("It looks like this is your first time using radicle.") term::print(term::format::yellow(err));
); term::print(term::format::yellow(hint));
println!( }
"{}", Some(e) => {
term::format::highlight("To get started, use `rad auth` to authenticate.") term::error(e);
); }
println!(); None => {
term::error(e);
}
}
term::blank();
} }
println!("Common `rad` commands used in various situations:"); term::print("Common `rad` commands used in various situations:");
println!(); term::blank();
for help in COMMANDS { for help in COMMANDS {
println!( term::info!(
"\t{} {}", "\t{} {}",
term::format::bold(format!("{:-12}", help.name)), term::format::bold(format!("{:-12}", help.name)),
term::format::dim(help.description) term::format::dim(help.description)
); );
} }
println!(); term::blank();
println!("See `rad <command> --help` to learn about a specific command."); term::print("See `rad <command> --help` to learn about a specific command.");
println!(); term::blank();
Ok(()) Ok(())
} }

View File

@ -121,11 +121,13 @@ where
pub fn profile() -> Result<Profile, anyhow::Error> { pub fn profile() -> Result<Profile, anyhow::Error> {
match Profile::load() { match Profile::load() {
Ok(profile) => Ok(profile), Ok(profile) => Ok(profile),
Err(e) => Err(args::Error::WithHint { Err(radicle::profile::Error::NotFound(path)) => Err(args::Error::WithHint {
err: anyhow::anyhow!("Could not load radicle profile: {e}"), err: anyhow::anyhow!("Radicle profile not found in '{}'.", path.display()),
hint: "To setup your radicle profile, run `rad auth`.", hint: "To setup your radicle profile, run `rad auth`.",
} }
.into()), .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)] #[derive(Debug, Error)]
pub enum ConfigError { pub enum ConfigError {
#[error("failed to load node configuration from {0}: {1}")] #[error("failed to load configuration from {0}: {1}")]
Io(PathBuf, io::Error), Io(PathBuf, io::Error),
#[error("failed to decode node configuration from {0}: {1}")] #[error("failed to load configuration from {0}: {1}")]
Json(PathBuf, serde_json::Error), Load(PathBuf, serde_json::Error),
} }
/// Local radicle configuration. /// Local radicle configuration.
@ -200,7 +200,7 @@ impl Config {
pub fn load(path: &Path) -> Result<Self, ConfigError> { pub fn load(path: &Path) -> Result<Self, ConfigError> {
match fs::File::open(path) { match fs::File::open(path) {
Ok(cfg) => { 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) => { Err(e) => {
let Ok(user) = env::var("USER") else { let Ok(user) = env::var("USER") else {