better error handling for nu_command::env::conig::utils::get_editor (#6430)

This commit is contained in:
Scott Boggs 2022-08-28 05:56:55 -04:00 committed by GitHub
parent f1e7a01b2e
commit f1d72e2670
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 8 deletions

View file

@ -58,7 +58,7 @@ impl Command for ConfigEnv {
nu_config.push("env.nu");
let name = Spanned {
item: get_editor(engine_state, stack),
item: get_editor(engine_state, stack)?,
span: Span { start: 0, end: 0 },
};

View file

@ -58,7 +58,7 @@ impl Command for ConfigNu {
nu_config.push("config.nu");
let name = Spanned {
item: get_editor(engine_state, stack),
item: get_editor(engine_state, stack)?,
span: Span { start: 0, end: 0 },
};

View file

@ -1,17 +1,20 @@
use nu_protocol::engine::{EngineState, Stack};
pub(crate) fn get_editor(engine_state: &EngineState, stack: &mut Stack) -> String {
pub(crate) fn get_editor(
engine_state: &EngineState,
stack: &mut Stack,
) -> Result<String, nu_protocol::ShellError> {
let config = engine_state.get_config();
let env_vars = stack.get_env_vars(engine_state);
if !config.buffer_editor.is_empty() {
config.buffer_editor.clone()
Ok(config.buffer_editor.clone())
} else if let Some(value) = env_vars.get("EDITOR") {
value.as_string().expect("Unknown type")
value.as_string()
} else if let Some(value) = env_vars.get("VISUAL") {
value.as_string().expect("Unknown type")
value.as_string()
} else if cfg!(target_os = "windows") {
"notepad".to_string()
Ok("notepad".to_string())
} else {
"nano".to_string()
Ok("nano".to_string())
}
}