From 632c6d1cd924ff58689ff7e393fc46359c376b25 Mon Sep 17 00:00:00 2001 From: Sam Hedin Date: Mon, 22 Jun 2020 17:32:44 +0200 Subject: [PATCH] Nicer errors for autoenv commands --- crates/nu-cli/src/commands/autoenv_trust.rs | 7 ++++++- crates/nu-cli/src/commands/autoenv_untrust.rs | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/nu-cli/src/commands/autoenv_trust.rs b/crates/nu-cli/src/commands/autoenv_trust.rs index 4a6454c233..c905b2da13 100644 --- a/crates/nu-cli/src/commands/autoenv_trust.rs +++ b/crates/nu-cli/src/commands/autoenv_trust.rs @@ -45,7 +45,12 @@ impl WholeStreamCommand for AutoenvTrust { } }; - let content = std::fs::read_to_string(&file_to_trust)?; + let content = std::fs::read_to_string(&file_to_trust).or_else(|_| { + Err(ShellError::untagged_runtime_error( + "No .nu-env file in the given directory", + )) + })?; + let mut hasher = DefaultHasher::new(); content.hash(&mut hasher); diff --git a/crates/nu-cli/src/commands/autoenv_untrust.rs b/crates/nu-cli/src/commands/autoenv_untrust.rs index 2d336d11a7..455c232545 100644 --- a/crates/nu-cli/src/commands/autoenv_untrust.rs +++ b/crates/nu-cli/src/commands/autoenv_untrust.rs @@ -70,7 +70,13 @@ impl WholeStreamCommand for AutoenvUnTrust { let mut allowed: Trusted = toml::from_str(doc.as_str()).unwrap_or_else(|_| Trusted::new()); let file_to_untrust = file_to_untrust.to_string_lossy().to_string(); - allowed.files.remove(&file_to_untrust); + + if allowed.files.remove(&file_to_untrust).is_none() { + return + Err(ShellError::untagged_runtime_error( + "No .nu-env file to untrust in the given directory", + )); + } fs::write(config_path, toml::to_string(&allowed).unwrap()) .expect("Couldn't write to toml file");