genome-sh 0.1.0

The jq of genomics. Fast, local, human-readable variant analysis.
use anyhow::Result;
use clap::{Args, Subcommand};

use crate::db::config::Config;

#[derive(Args)]
pub struct ConfigArgs {
    #[command(subcommand)]
    command: ConfigCommand,
}

#[derive(Subcommand)]
enum ConfigCommand {
    /// Set a configuration value.
    Set {
        /// Configuration key.
        key: String,
        /// Configuration value.
        value: String,
    },

    /// Get a configuration value.
    Get {
        /// Configuration key.
        key: String,
    },

    /// List all configuration values.
    List,
}

impl ConfigArgs {
    pub fn run(self) -> Result<()> {
        let mut config = Config::load()?;

        match self.command {
            ConfigCommand::Set { key, value } => {
                config.set(&key, &value)?;
                config.save()?;
                println!("Set {key} = {value}");
            }
            ConfigCommand::Get { key } => match config.get(&key) {
                Some(value) => println!("{value}"),
                None => eprintln!("No value set for: {key}"),
            },
            ConfigCommand::List => {
                for (key, value) in config.entries() {
                    println!("{key} = {value}");
                }
            }
        }

        Ok(())
    }
}