1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::{cli::ConfigCmd as Cmd, validator, Config, Ctx, QuartzResult};

#[derive(clap::Args, Debug)]
pub struct GetArgs {
    key: String,
}

#[derive(clap::Args, Debug)]
pub struct SetArgs {
    key: String,
    value: String,
}

pub fn cmd(ctx: &mut Ctx, command: Cmd) -> QuartzResult {
    match command {
        Cmd::Get(args) => get(ctx, args),
        Cmd::Edit => edit(ctx)?,
        Cmd::Set(args) => set(ctx, args),
        Cmd::Ls => ls(ctx),
    };

    Ok(())
}

pub fn get(ctx: &Ctx, args: GetArgs) {
    let value = match args.key.as_str() {
        "preferences.editor" => ctx.config.preferences.editor(),
        "preferences.pager" => ctx.config.preferences.pager(),
        "ui.colors" => ctx.config.ui.colors().to_string(),
        _ => panic!("invalid key"),
    };

    println!("{value}");
}

pub fn edit(ctx: &Ctx) -> QuartzResult {
    ctx.edit(&Config::filepath(), validator::toml_as::<Config>)?;

    Ok(())
}

pub fn set(ctx: &mut Ctx, args: SetArgs) {
    match args.key.as_str() {
        "preferences.editor" => ctx.config.preferences.set_editor(args.value),
        "preferences.pager" => ctx.config.preferences.set_pager(args.value),
        "ui.colors" => ctx
            .config
            .ui
            .set_colors(matches!(args.value.as_str(), "true")),
        _ => panic!("invalid key"),
    };

    if ctx.config.write().is_err() {
        panic!("failed to save config change");
    }
}

pub fn ls(ctx: &Ctx) {
    let content = toml::to_string(&ctx.config)
        .unwrap_or_else(|_| panic!("could not parse configuration file"));

    println!("{content}");
}