Skip to content

Commit

Permalink
cli: Allow multiple --tmpdir values
Browse files Browse the repository at this point in the history
Each folder will be tried in turn.
  • Loading branch information
vincentdephily committed Sep 22, 2023
1 parent dde98f3 commit dc3de04
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Support searching by multiple terms
- eg `emlop s -e gcc clang llvm rust`
* Support multiple `--tmpdir`s
* Upgraded clap dependency
- Inline help styling/content changed a bit

Expand Down
48 changes: 34 additions & 14 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn build_cli_nocomplete() -> Command {
// Workaround bad alignment, might be fixed in clap 4
.help(" Show only packages/repos matching <search>")
.long_help("Show only packages/repos matching <search>\n\
Multiple terms can be provided\n\
Matches using a regex unless `--exact` is specified\n\
See https://docs.rs/regex/*/regex/#syntax\n \
rust: Matches `dev-lang/rust`, `dev-util/rustup`, `dev-python/trustme`, etc\n \
Expand Down Expand Up @@ -264,9 +265,12 @@ pub fn build_cli_nocomplete() -> Command {
let tmpdir = Arg::new("tmpdir").value_name("dir")
.long("tmpdir")
.num_args(1)
.action(Append)
.default_value("/var/tmp")
.display_order(2)
.help("Location of portage tmpdir");
.help("Location of portage tmpdir")
.long_help("Location of portage tmpdir\n\
Multiple folders can be provided");
let resume = Arg::new("resume").long("resume")
.value_name("source")
.value_parser(value_parser!(crate::ResumeKind))
Expand Down Expand Up @@ -410,22 +414,38 @@ mod test {
.clone()
}

macro_rules! one {
($t: ty, $k: expr, $a: expr) => {
matches($a).get_one::<$t>($k)
};
}
macro_rules! many {
($t: ty, $k: expr, $a: expr) => {
matches($a).get_many::<$t>($k).map(|v| v.map(AsRef::as_ref).collect::<Vec<_>>())
};
}

#[test]
fn args() {
assert_eq!(matches("l").get_one::<Option<&usize>>("first"), None);
assert_eq!(matches("l --first").get_one("first"), Some(&1usize));
assert_eq!(matches("l --first 2").get_one("first"), Some(&2usize));
assert_eq!(matches("l -N 2").get_one("first"), Some(&2usize));
assert_eq!(matches("l -N4").get_one("first"), Some(&4usize));
assert_eq!(one!(usize, "first", "l"), None);
assert_eq!(one!(usize, "first", "l --first"), Some(&1usize));
assert_eq!(one!(usize, "first", "l --first"), Some(&1usize));
assert_eq!(one!(usize, "first", "l --first 2"), Some(&2usize));
assert_eq!(one!(usize, "first", "l -N 2"), Some(&2usize));
assert_eq!(one!(usize, "first", "l -N4"), Some(&4usize));

assert_eq!(one!(usize, "last", "l --last"), Some(&1usize));
assert_eq!(one!(usize, "last", "l --last 2"), Some(&2usize));
assert_eq!(one!(usize, "last", "l -n 2"), Some(&2usize));

assert_eq!(matches("l --last").get_one("last"), Some(&1usize));
assert_eq!(matches("l --last 2").get_one("last"), Some(&2usize));
assert_eq!(matches("l -n 2").get_one("last"), Some(&2usize));
assert_eq!(one!(ColorStyle, "color", "l"), Some(&ColorStyle::Auto));
assert_eq!(one!(ColorStyle, "color", "l --color"), Some(&ColorStyle::Always));
assert_eq!(one!(ColorStyle, "color", "l --color=y"), Some(&ColorStyle::Always));
assert_eq!(one!(ColorStyle, "color", "l --color n"), Some(&ColorStyle::Never));
assert_eq!(one!(ColorStyle, "color", "l --color never"), Some(&ColorStyle::Never));

assert_eq!(matches("l").get_one("color"), Some(&ColorStyle::Auto));
assert_eq!(matches("l --color").get_one("color"), Some(&ColorStyle::Always));
assert_eq!(matches("l --color=y").get_one("color"), Some(&ColorStyle::Always));
assert_eq!(matches("l --color n").get_one("color"), Some(&ColorStyle::Never));
assert_eq!(matches("l --color never").get_one("color"), Some(&ColorStyle::Never));
assert_eq!(many!(String, "tmpdir", "p"), Some(vec!["/var/tmp"]));
assert_eq!(many!(String, "tmpdir", "p --tmpdir a"), Some(vec!["a"]));
assert_eq!(many!(String, "tmpdir", "p --tmpdir a --tmpdir b"), Some(vec!["a", "b"]));
}
}
4 changes: 2 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ pub fn cmd_predict(args: &ArgMatches) -> Result<bool, Error> {
let avg = *args.get_one("avg").unwrap();
let resume = *args.get_one("resume").unwrap();
let mut tbl = Table::new(st).align_left(0).align_left(2).margin(2, " ").last(last);
let tmpdir = args.get_one::<String>("tmpdir").unwrap();
let tmpdirs = args.get_many::<String>("tmpdir").unwrap().cloned().collect();

// Gather and print info about current merge process.
let mut cms = std::i64::MAX;
Expand Down Expand Up @@ -416,7 +416,7 @@ pub fn cmd_predict(args: &ArgMatches) -> Result<bool, Error> {
// Done
if show.merge && totcount <= first {
if elapsed > 0 {
let stage = get_buildlog(&p, tmpdir).unwrap_or_default();
let stage = get_buildlog(&p, &tmpdirs).unwrap_or_default();
tbl.row([&[&st.pkg, &p.ebuild_version()],
&[&FmtDur(pred)],
&[&st.clr, &"- ", &FmtDur(elapsed), &st.clr, &stage]]);
Expand Down
14 changes: 9 additions & 5 deletions src/parse/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ fn get_resume_priv(kind: ResumeKind, file: &str) -> Option<Vec<Pkg>> {


/// Retrieve summary info from the build log
pub fn get_buildlog(pkg: &Pkg, portdir: &str) -> Option<String> {
let name = format!("{}/portage/{}/temp/build.log", portdir, pkg.ebuild_version());
info!("Build log: {name}");
let file = File::open(&name).map_err(|e| warn!("Cannot open {name:?}: {e}")).ok()?;
Some(read_buildlog(file, 50))
pub fn get_buildlog(pkg: &Pkg, portdirs: &Vec<String>) -> Option<String> {
for portdir in portdirs {
let name = format!("{}/portage/{}/temp/build.log", portdir, pkg.ebuild_version());
if let Ok(file) = File::open(&name).map_err(|e| warn!("Cannot open {name:?}: {e}")) {
info!("Build log: {name}");
return Some(read_buildlog(file, 50));
}
}
None
}
fn read_buildlog(file: File, max: usize) -> String {
let mut last = String::new();
Expand Down

0 comments on commit dc3de04

Please sign in to comment.