From 6517ac35143c4ff134561fa3c9b5eade1fa32478 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 13 May 2024 18:23:46 -0500 Subject: [PATCH] parse dry output better --- lib/default.nix | 2 +- pkgs/oizys/oizys-rs/src/main.rs | 44 +++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index e262454..e456fc8 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -57,7 +57,7 @@ rec { oizys-nim = pkgs.callPackage ../pkgs/oizys/oizys-nim { }; oizys-rs = pkgs.callPackage ../pkgs/oizys/oizys-rs { }; oizys-go = pkgs.callPackage ../pkgs/oizys/oizys-go { }; - default = oizys-zig; + default = oizys-rs; }); devShells = forAllSystems (pkgs: { default = pkgs.mkShell { diff --git a/pkgs/oizys/oizys-rs/src/main.rs b/pkgs/oizys/oizys-rs/src/main.rs index 38e6fcf..9923c1b 100644 --- a/pkgs/oizys/oizys-rs/src/main.rs +++ b/pkgs/oizys/oizys-rs/src/main.rs @@ -1,7 +1,7 @@ use clap::{CommandFactory, Parser, Subcommand}; use clap_complete::{generate, Generator, Shell}; -use std::{env, io, path::PathBuf, process::Command}; use spinoff::{spinners, Color, Spinner}; +use std::{env, io, path::PathBuf, process::Command}; #[derive(Parser)] #[command(version, about, long_about = None)] @@ -62,6 +62,25 @@ struct Oizys { verbose: u8, } +fn trim_hashes(buf: &str, trim_drv: bool) -> Vec { + buf.lines() + .skip(1) + .map(|line| { + line.split_once('-') + .map(|x| { + format!(" {}", { + if trim_drv { + x.1.replace(".drv", "") + } else { + x.1.to_string() + } + }) + }) + .expect("failed to trim derivation") + }) + .collect() +} + impl Oizys { fn from(cli: &Cli) -> Oizys { let host = cli @@ -94,17 +113,21 @@ impl Oizys { fn parse_dry_output(self: &Oizys, output: &str) { let parts: Vec<&str> = output.split("\nthese").collect(); + // TODO: handle more cases of output + // currently fails if nothing to fetch if parts.len() != 3 { eprintln!("couldn't parse dry output into three parts"); + eprintln!("{}", output); std::process::exit(1); } - let to_build: Vec<&str> = parts[1].lines().skip(1).collect(); - let to_fetch: Vec<&str> = parts[2].lines().skip(1).collect(); - println!("To build: {}", to_build.len()); - println!("To fetch: {}",to_fetch.len()); + let to_build: Vec = trim_hashes(parts[1], true); + let to_fetch: Vec = trim_hashes(parts[2], false); if self.verbose > 0 { - println!("TO BUILD: \n{}\n", to_build.join("\n")); - println!("TO FETCH: \n{}\n", to_fetch.join("\n")); + println!("TO BUILD: {}\n{}\n", to_build.len(), to_build.join("\n")); + println!("TO FETCH: {}\n{}\n", to_fetch.len(), to_fetch.join("\n")); + } else { + println!("To build: {}", to_build.len()); + println!("To fetch: {}", to_fetch.len()); } } @@ -119,7 +142,12 @@ impl Oizys { let output = String::from_utf8(cmd.output().expect("failed to run nix build").stderr) .expect("faild to parse nix build --dry-run output"); spinner.stop_with_message("evaluating finished"); - self.parse_dry_output(&output); + + if output.contains("derivations will be built") { + self.parse_dry_output(&output); + } else { + println!("{} up to date", self.host) + } } fn build(self: &Oizys) {