quartz_cli/action/
config.rs1use crate::{cli::ConfigCmd as Cmd, validator, Config, Ctx, QuartzResult};
2
3#[derive(clap::Args, Debug)]
4pub struct GetArgs {
5 key: String,
6}
7
8#[derive(clap::Args, Debug)]
9pub struct SetArgs {
10 key: String,
11 value: String,
12}
13
14pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult {
15 match command {
16 Cmd::Get(args) => get(ctx, args),
17 Cmd::Edit => edit(ctx)?,
18 Cmd::Set(args) => set(ctx, args),
19 Cmd::Ls => ls(ctx),
20 };
21
22 Ok(())
23}
24
25pub fn get(ctx: &Ctx, args: GetArgs) {
26 let value = match args.key.as_str() {
27 "preferences.editor" => ctx.config.preferences.editor(),
28 "preferences.pager" => ctx.config.preferences.pager(),
29 "ui.colors" => ctx.config.ui.colors().to_string(),
30 _ => panic!("invalid key"),
31 };
32
33 println!("{value}");
34}
35
36pub fn edit(ctx: &Ctx) -> QuartzResult {
37 ctx.edit(&Config::filepath(), validator::toml_as::<Config>)?;
38
39 Ok(())
40}
41
42pub fn set(ctx: &mut Ctx, args: SetArgs) {
43 match args.key.as_str() {
44 "preferences.editor" => ctx.config.preferences.set_editor(args.value),
45 "preferences.pager" => ctx.config.preferences.set_pager(args.value),
46 "ui.colors" => ctx
47 .config
48 .ui
49 .set_colors(matches!(args.value.as_str(), "true")),
50 _ => panic!("invalid key"),
51 };
52
53 if ctx.config.write().is_err() {
54 panic!("failed to save config change");
55 }
56}
57
58pub fn ls(ctx: &Ctx) {
59 let content = toml::to_string(&ctx.config)
60 .unwrap_or_else(|_| panic!("could not parse configuration file"));
61
62 println!("{content}");
63}