use std::path::PathBuf;
use clap::{Parser, Subcommand};
use color_eyre::{
eyre::{self, Context},
Result,
};
use liboswo::{Cfgs, Outputs};
use log::info;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[command(subcommand)]
cmds: Cmds,
#[arg(short, long, action = clap::ArgAction::Count, global = true)]
verbose: u8,
}
#[derive(Subcommand, Debug)]
enum Cmds {
#[command(alias = "d")]
Display,
#[command(alias = "s")]
Set {
setup: Vec<String>,
},
#[command(alias = "u")]
Use {
config: String,
#[arg(short, long)]
cfg_file: Option<PathBuf>,
},
#[command(alias = "p")]
Print {
#[arg(short, long)]
cfg_file: Option<PathBuf>,
},
#[command(alias = "a")]
Add {
name: String,
#[arg(short, long)]
cfg_file: Option<PathBuf>,
},
}
fn main() -> Result<()> {
color_eyre::install()?;
let args = Args::parse();
let default_cfg = dirs::config_dir()
.unwrap_or("/etc/xdg/".into())
.join("oswo.toml");
let outputs = Outputs::list()?;
match args.cmds {
Cmds::Display if args.verbose == 0 => println!("{}", outputs),
Cmds::Display => println!("{:#}", outputs),
Cmds::Set { setup } => outputs.set_by_name(&setup)?,
Cmds::Use { config, cfg_file } => {
let cfg = cfg_file.unwrap_or(default_cfg);
let cfgs = Cfgs::from_file(cfg).wrap_err("Failed to load configuration")?;
let cfg_entry = cfgs
.find(&config)
.ok_or_else(|| eyre::eyre!("Found no setup for '{}'", config))?;
outputs.set_models(&cfg_entry.outputs)?;
}
Cmds::Print { cfg_file } => {
let cfg = cfg_file.unwrap_or(default_cfg);
let cfgs = Cfgs::from_file(cfg).wrap_err("Failed to load configuration")?;
println!("{}", cfgs);
}
Cmds::Add { name, cfg_file } => {
let cfg = cfg_file.unwrap_or(default_cfg);
let mut cfgs = Cfgs::from_file(&cfg).wrap_err("Failed to load configuration")?;
cfgs.add(&name, &outputs)?;
cfgs.save(&cfg).wrap_err("Failed to write configuration")?;
info!("Wrote configuration to {}", cfg.display());
}
}
Ok(())
}