Add a ta config set subcommand
This uses `toml_edit` to edit the config file in-place. For the moment, it only supports top-level arguments, but can be extended to do other things later.
This commit is contained in:
62
cli/src/invocation/cmd/config.rs
Normal file
62
cli/src/invocation/cmd/config.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use crate::argparse::ConfigOperation;
|
||||
use crate::settings::Settings;
|
||||
use termcolor::{ColorSpec, WriteColor};
|
||||
|
||||
pub(crate) fn execute<W: WriteColor>(
|
||||
w: &mut W,
|
||||
config_operation: ConfigOperation,
|
||||
settings: &Settings,
|
||||
) -> anyhow::Result<()> {
|
||||
match config_operation {
|
||||
ConfigOperation::Set(key, value) => {
|
||||
let filename = settings.set(&key, &value)?;
|
||||
write!(w, "Set configuration value ")?;
|
||||
w.set_color(ColorSpec::new().set_bold(true))?;
|
||||
write!(w, "{}", &key)?;
|
||||
w.set_color(ColorSpec::new().set_bold(false))?;
|
||||
write!(w, " in ")?;
|
||||
w.set_color(ColorSpec::new().set_bold(true))?;
|
||||
writeln!(w, "{:?}.", filename)?;
|
||||
w.set_color(ColorSpec::new().set_bold(false))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::invocation::test::*;
|
||||
use std::fs;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn test_config_set() {
|
||||
let cfg_dir = TempDir::new().unwrap();
|
||||
let cfg_file = cfg_dir.path().join("foo.toml");
|
||||
fs::write(
|
||||
cfg_file.clone(),
|
||||
"# store data everywhere\ndata_dir = \"/nowhere\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let settings = Settings::load_from_file(cfg_file.clone(), true).unwrap();
|
||||
|
||||
let mut w = test_writer();
|
||||
|
||||
execute(
|
||||
&mut w,
|
||||
ConfigOperation::Set("data_dir".to_owned(), "/somewhere".to_owned()),
|
||||
&settings,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(w.into_string().starts_with("Set configuration value "));
|
||||
|
||||
let updated_toml = fs::read_to_string(cfg_file.clone()).unwrap();
|
||||
dbg!(&updated_toml);
|
||||
assert_eq!(
|
||||
updated_toml,
|
||||
"# store data everywhere\ndata_dir = \"/somewhere\"\n"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Responsible for executing commands as parsed by [`crate::argparse`].
|
||||
|
||||
pub(crate) mod add;
|
||||
pub(crate) mod config;
|
||||
pub(crate) mod gc;
|
||||
pub(crate) mod help;
|
||||
pub(crate) mod info;
|
||||
|
||||
@@ -35,6 +35,10 @@ pub(crate) fn invoke(command: Command, settings: Settings) -> anyhow::Result<()>
|
||||
subcommand: Subcommand::Help { summary },
|
||||
command_name,
|
||||
} => return cmd::help::execute(&mut w, command_name, summary),
|
||||
Command {
|
||||
subcommand: Subcommand::Config { config_operation },
|
||||
..
|
||||
} => return cmd::config::execute(&mut w, config_operation, &settings),
|
||||
Command {
|
||||
subcommand: Subcommand::Version,
|
||||
..
|
||||
@@ -90,6 +94,10 @@ pub(crate) fn invoke(command: Command, settings: Settings) -> anyhow::Result<()>
|
||||
subcommand: Subcommand::Help { .. },
|
||||
..
|
||||
} => unreachable!(),
|
||||
Command {
|
||||
subcommand: Subcommand::Config { .. },
|
||||
..
|
||||
} => unreachable!(),
|
||||
Command {
|
||||
subcommand: Subcommand::Version,
|
||||
..
|
||||
|
||||
Reference in New Issue
Block a user