mirror of
https://github.com/nushell/nushell.git
synced 2025-03-29 17:16:49 +01:00
* First commit updating `config` to use subcommands (#2119) - Implemented `get` subcommand * Implmented `config set` as a subcommand. * Implemented `config set_into` as subcommand * Fixed base `config` command - Instead of outputting help, it now outputs the list of all configuration parameters. * Added `config clear` subcommand * Added `config load` and `config remove` subcommands * Added `config path` subcommand * fixed clippy
76 lines
1.9 KiB
Rust
76 lines
1.9 KiB
Rust
use crate::commands::WholeStreamCommand;
|
|
use crate::context::CommandRegistry;
|
|
use crate::prelude::*;
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
|
use nu_source::Tagged;
|
|
|
|
pub struct SubCommand;
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct RemoveArgs {
|
|
remove: Tagged<String>,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl WholeStreamCommand for SubCommand {
|
|
fn name(&self) -> &str {
|
|
"config remove"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("config remove").required(
|
|
"remove",
|
|
SyntaxShape::Any,
|
|
"remove a value from the config",
|
|
)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Removes a value from the config"
|
|
}
|
|
|
|
async fn run(
|
|
&self,
|
|
args: CommandArgs,
|
|
registry: &CommandRegistry,
|
|
) -> Result<OutputStream, ShellError> {
|
|
remove(args, registry).await
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Remove the startup commands",
|
|
example: "config --remove startup",
|
|
result: None,
|
|
}]
|
|
}
|
|
}
|
|
|
|
pub async fn remove(
|
|
args: CommandArgs,
|
|
registry: &CommandRegistry,
|
|
) -> Result<OutputStream, ShellError> {
|
|
let name_span = args.call_info.name_tag.clone();
|
|
let (RemoveArgs { remove }, _) = args.process(®istry).await?;
|
|
|
|
let mut result = crate::data::config::read(name_span, &None)?;
|
|
|
|
let key = remove.to_string();
|
|
|
|
if result.contains_key(&key) {
|
|
result.swap_remove(&key);
|
|
config::write(&result, &None)?;
|
|
Ok(futures::stream::iter(vec![ReturnSuccess::value(
|
|
UntaggedValue::Row(result.into()).into_value(remove.tag()),
|
|
)])
|
|
.to_output_stream())
|
|
} else {
|
|
Err(ShellError::labeled_error(
|
|
"Key does not exist in config",
|
|
"key",
|
|
remove.tag(),
|
|
))
|
|
}
|
|
}
|