nushell/crates/nu-command/src/env/config/config_nu.rs
Jérémy Audiger 99076af18b
Use imported names in Command::run signatures (#7967)
# Description

_(Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.)_

I opened this PR to unify the run command method. It's mainly to improve
consistency across the tree.

# User-Facing Changes

None.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-05 22:17:46 +01:00

86 lines
2.3 KiB
Rust

use nu_engine::env_to_strings;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, Type,
};
use crate::ExternalCommand;
use super::utils::get_editor;
#[derive(Clone)]
pub struct ConfigNu;
impl Command for ConfigNu {
fn name(&self) -> &str {
"config nu"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.category(Category::Env)
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
// TODO: Signature narrower than what run actually supports theoretically
}
fn usage(&self) -> &str {
"Edit nu configurations"
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "allow user to open and update nu config",
example: "config nu",
result: None,
}]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let env_vars_str = env_to_strings(engine_state, stack)?;
let mut config_path = match nu_path::config_dir() {
Some(path) => path,
None => {
return Err(ShellError::GenericError(
"Could not find nu config path".to_string(),
"Could not find nu config path".to_string(),
None,
None,
Vec::new(),
));
}
};
config_path.push("nushell");
let mut nu_config = config_path.clone();
nu_config.push("config.nu");
let name = Spanned {
item: get_editor(engine_state, stack)?,
span: call.head,
};
let args = vec![Spanned {
item: nu_config.to_string_lossy().to_string(),
span: Span::unknown(),
}];
let command = ExternalCommand {
name,
args,
arg_keep_raw: vec![false],
redirect_stdout: false,
redirect_stderr: false,
env_vars: env_vars_str,
trim_end_newline: false,
};
command.run_with_input(engine_state, stack, input, true)
}
}