use from method and change path->output

This commit is contained in:
Daylin Morgan 2024-03-27 12:38:44 -05:00
parent 5973c4d8e7
commit 1df7c557a3
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -51,7 +51,7 @@ enum Commands {
Build {}, Build {},
/// print nix flake output /// print nix flake output
Path {}, Output {},
} }
fn print_completions<G: Generator>(gen: G, cmd: &mut clap::Command) { fn print_completions<G: Generator>(gen: G, cmd: &mut clap::Command) {
@ -67,19 +67,24 @@ struct Oizys {
} }
impl Oizys { impl Oizys {
fn new(host: Option<String>, flake: Option<PathBuf>, no_pinix: bool, verbose: u8) -> Oizys { fn from(cli: &Cli) -> Oizys {
let hostname = hostname::get().unwrap().to_string_lossy().to_string(); let host = cli
let flake_path = env::var("OIZYS_DIR").map_or( .host
.clone()
.unwrap_or(hostname::get().unwrap().to_string_lossy().to_string());
let flake = cli.flake.clone().unwrap_or(env::var("OIZYS_DIR").map_or(
homedir::get_my_home().unwrap().unwrap().join("oizys"), homedir::get_my_home().unwrap().unwrap().join("oizys"),
PathBuf::from, PathBuf::from,
); ));
Oizys { Oizys {
host: host.unwrap_or(hostname), host,
flake: flake.unwrap_or(flake_path), flake,
no_pinix, no_pinix: cli.no_pinix,
verbose, verbose: cli.verbose,
} }
} }
fn output(self: &Oizys) -> String { fn output(self: &Oizys) -> String {
format!( format!(
"{}#nixosConfigurations.{}.config.system.build.toplevel", "{}#nixosConfigurations.{}.config.system.build.toplevel",
@ -155,7 +160,7 @@ impl Oizys {
fn main() { fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let oizys = Oizys::new(cli.host, cli.flake, cli.no_pinix, cli.verbose); let oizys = Oizys::from(&cli);
if let Some(completions) = cli.completions { if let Some(completions) = cli.completions {
let mut cmd = Cli::command(); let mut cmd = Cli::command();
@ -175,7 +180,7 @@ fn main() {
match command { match command {
Commands::Dry {} => oizys.build(true), Commands::Dry {} => oizys.build(true),
Commands::Build {} => oizys.build(false), Commands::Build {} => oizys.build(false),
Commands::Path {} => println!("{}", oizys.output()), Commands::Output {} => println!("{}", oizys.output()),
Commands::Boot {} => oizys.nixos_rebuild("boot"), Commands::Boot {} => oizys.nixos_rebuild("boot"),
Commands::Switch {} => oizys.nixos_rebuild("switch"), Commands::Switch {} => oizys.nixos_rebuild("switch"),
Commands::Cache { name } => oizys.cache(name), Commands::Cache { name } => oizys.cache(name),