Traverse up to find hive.nix by default, and other CLI ergonomics fixes
This commit is contained in:
parent
9c8e3034f7
commit
60d6475897
11 changed files with 212 additions and 95 deletions
103
src/util.rs
103
src/util.rs
|
@ -1,15 +1,89 @@
|
|||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::fs;
|
||||
|
||||
use clap::{Arg, App};
|
||||
use clap::{App, Arg, ArgMatches};
|
||||
use glob::Pattern as GlobPattern;
|
||||
|
||||
use super::nix::DeploymentConfig;
|
||||
use super::nix::{DeploymentConfig, Hive, NixResult};
|
||||
|
||||
enum NodeFilter {
|
||||
NameFilter(GlobPattern),
|
||||
TagFilter(GlobPattern),
|
||||
}
|
||||
|
||||
pub fn hive_from_args(args: &ArgMatches<'_>) -> NixResult<Hive> {
|
||||
let path = match args.occurrences_of("config") {
|
||||
0 => {
|
||||
// traverse upwards until we find hive.nix
|
||||
let mut cur = std::env::current_dir()?;
|
||||
let mut hive_path = None;
|
||||
|
||||
loop {
|
||||
let mut listing = match fs::read_dir(&cur) {
|
||||
Ok(listing) => listing,
|
||||
Err(e) => {
|
||||
// This can very likely fail in shared environments
|
||||
// where users aren't able to list /home. It's not
|
||||
// unexpected.
|
||||
//
|
||||
// It may not be immediately obvious to the user that
|
||||
// we are traversing upwards to find hive.nix.
|
||||
log::warn!("Could not traverse up ({:?}) to find hive.nix: {}", cur, e);
|
||||
break;
|
||||
},
|
||||
};
|
||||
|
||||
let found = listing.find_map(|rdirent| {
|
||||
match rdirent {
|
||||
Err(e) => Some(Err(e)),
|
||||
Ok(f) => {
|
||||
if f.file_name() == "hive.nix" {
|
||||
Some(Ok(f))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(rdirent) = found {
|
||||
let dirent = rdirent?;
|
||||
hive_path = Some(dirent.path());
|
||||
break;
|
||||
}
|
||||
|
||||
match cur.parent() {
|
||||
Some(parent) => {
|
||||
cur = parent.to_owned();
|
||||
}
|
||||
None => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if hive_path.is_none() {
|
||||
log::error!("Could not find `hive.nix` in {:?} or any parent directory", std::env::current_dir()?);
|
||||
}
|
||||
|
||||
hive_path.unwrap()
|
||||
}
|
||||
_ => {
|
||||
let path = args.value_of("config").expect("The config arg should exist").to_owned();
|
||||
canonicalize_cli_path(path)
|
||||
}
|
||||
};
|
||||
|
||||
let mut hive = Hive::new(path)?;
|
||||
|
||||
if args.is_present("show-trace") {
|
||||
hive.show_trace(true);
|
||||
}
|
||||
|
||||
Ok(hive)
|
||||
}
|
||||
|
||||
pub fn filter_nodes(nodes: &HashMap<String, DeploymentConfig>, filter: &str) -> Vec<String> {
|
||||
let filters: Vec<NodeFilter> = filter.split(",").map(|pattern| {
|
||||
use NodeFilter::*;
|
||||
|
@ -48,24 +122,7 @@ pub fn filter_nodes(nodes: &HashMap<String, DeploymentConfig>, filter: &str) ->
|
|||
}
|
||||
}
|
||||
|
||||
pub fn register_common_args<'a, 'b>(command: App<'a, 'b>) -> App<'a, 'b> {
|
||||
command
|
||||
.arg(Arg::with_name("config")
|
||||
.short("f")
|
||||
.long("config")
|
||||
.help("Path to a Hive expression")
|
||||
.default_value("hive.nix")
|
||||
.required(true))
|
||||
.arg(Arg::with_name("show-trace")
|
||||
.long("show-trace")
|
||||
.help("Show debug information for Nix commands")
|
||||
.long_help("Passes --show-trace to Nix commands")
|
||||
.takes_value(false))
|
||||
}
|
||||
|
||||
pub fn register_selector_args<'a, 'b>(command: App<'a, 'b>) -> App<'a, 'b> {
|
||||
let command = register_common_args(command);
|
||||
|
||||
command
|
||||
.arg(Arg::with_name("on")
|
||||
.long("on")
|
||||
|
@ -79,3 +136,11 @@ Valid examples:
|
|||
- @a-tag,@tags-can-have-*"#)
|
||||
.takes_value(true))
|
||||
}
|
||||
|
||||
fn canonicalize_cli_path(path: String) -> PathBuf {
|
||||
if !path.starts_with("/") {
|
||||
format!("./{}", path).into()
|
||||
} else {
|
||||
path.into()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue