eh: clean up dispatch logic
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I63671355750a3c839f71d9155b8c918d6a6a6964
This commit is contained in:
parent
caa6dc9951
commit
790ea414c3
4 changed files with 51 additions and 39 deletions
|
|
@ -62,17 +62,20 @@ impl NixCommand {
|
|||
self
|
||||
}
|
||||
|
||||
#[must_use] pub const fn impure(mut self, yes: bool) -> Self {
|
||||
#[must_use]
|
||||
pub const fn impure(mut self, yes: bool) -> Self {
|
||||
self.impure = yes;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use] pub const fn interactive(mut self, yes: bool) -> Self {
|
||||
#[must_use]
|
||||
pub const fn interactive(mut self, yes: bool) -> Self {
|
||||
self.interactive = yes;
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use] pub const fn print_build_logs(mut self, yes: bool) -> Self {
|
||||
#[must_use]
|
||||
pub const fn print_build_logs(mut self, yes: bool) -> Self {
|
||||
self.print_build_logs = yes;
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@ pub enum EhError {
|
|||
pub type Result<T> = std::result::Result<T, EhError>;
|
||||
|
||||
impl EhError {
|
||||
#[must_use] pub const fn exit_code(&self) -> i32 {
|
||||
#[must_use]
|
||||
pub const fn exit_code(&self) -> i32 {
|
||||
match self {
|
||||
Self::ProcessExit { code } => *code,
|
||||
Self::NixCommandFailed(_) => 1,
|
||||
|
|
|
|||
|
|
@ -30,6 +30,36 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
// Design partially taken from Stash
|
||||
fn dispatch_multicall(app_name: &str, args: std::env::Args) -> Option<Result<i32>> {
|
||||
let rest: Vec<String> = args.collect();
|
||||
let hash_extractor = util::RegexHashExtractor;
|
||||
let fixer = util::DefaultNixFileFixer;
|
||||
let classifier = util::DefaultNixErrorClassifier;
|
||||
|
||||
match app_name {
|
||||
"nr" => Some(run::handle_nix_run(
|
||||
&rest,
|
||||
&hash_extractor,
|
||||
&fixer,
|
||||
&classifier,
|
||||
)),
|
||||
"ns" => Some(shell::handle_nix_shell(
|
||||
&rest,
|
||||
&hash_extractor,
|
||||
&fixer,
|
||||
&classifier,
|
||||
)),
|
||||
"nb" => Some(build::handle_nix_build(
|
||||
&rest,
|
||||
&hash_extractor,
|
||||
&fixer,
|
||||
&classifier,
|
||||
)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn run_app() -> Result<i32> {
|
||||
let mut args = env::args();
|
||||
let bin = args.next().unwrap_or_else(|| "eh".to_string());
|
||||
|
|
@ -39,31 +69,8 @@ fn run_app() -> Result<i32> {
|
|||
.unwrap_or("eh");
|
||||
|
||||
// If invoked as nr/ns/nb, dispatch directly and exit
|
||||
match app_name {
|
||||
"nr" => {
|
||||
let rest: Vec<String> = args.collect();
|
||||
let hash_extractor = util::RegexHashExtractor;
|
||||
let fixer = util::DefaultNixFileFixer;
|
||||
let classifier = util::DefaultNixErrorClassifier;
|
||||
return run::handle_nix_run(&rest, &hash_extractor, &fixer, &classifier);
|
||||
}
|
||||
|
||||
"ns" => {
|
||||
let rest: Vec<String> = args.collect();
|
||||
let hash_extractor = util::RegexHashExtractor;
|
||||
let fixer = util::DefaultNixFileFixer;
|
||||
let classifier = util::DefaultNixErrorClassifier;
|
||||
return shell::handle_nix_shell(&rest, &hash_extractor, &fixer, &classifier);
|
||||
}
|
||||
|
||||
"nb" => {
|
||||
let rest: Vec<String> = args.collect();
|
||||
let hash_extractor = util::RegexHashExtractor;
|
||||
let fixer = util::DefaultNixFileFixer;
|
||||
let classifier = util::DefaultNixErrorClassifier;
|
||||
return build::handle_nix_build(&rest, &hash_extractor, &fixer, &classifier);
|
||||
}
|
||||
_ => {}
|
||||
if let Some(result) = dispatch_multicall(app_name, args) {
|
||||
return result;
|
||||
}
|
||||
|
||||
let cli = Cli::parse();
|
||||
|
|
|
|||
|
|
@ -23,9 +23,10 @@ impl HashExtractor for RegexHashExtractor {
|
|||
for pattern in &patterns {
|
||||
if let Ok(re) = Regex::new(pattern)
|
||||
&& let Some(captures) = re.captures(stderr)
|
||||
&& let Some(hash) = captures.get(1) {
|
||||
return Some(hash.as_str().to_string());
|
||||
}
|
||||
&& let Some(hash) = captures.get(1)
|
||||
{
|
||||
return Some(hash.as_str().to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
|
@ -62,9 +63,10 @@ impl NixFileFixer for DefaultNixFileFixer {
|
|||
if path.is_dir() {
|
||||
stack.push(path);
|
||||
} else if let Some(ext) = path.extension()
|
||||
&& ext.eq_ignore_ascii_case("nix") {
|
||||
files.push(path);
|
||||
}
|
||||
&& ext.eq_ignore_ascii_case("nix")
|
||||
{
|
||||
files.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
if files.is_empty() {
|
||||
|
|
@ -99,10 +101,9 @@ impl NixFileFixer for DefaultNixFileFixer {
|
|||
}
|
||||
}
|
||||
if replaced {
|
||||
fs::write(file_path, new_content)
|
||||
.map_err(|_| EhError::HashFixFailed {
|
||||
path: file_path.to_string_lossy().to_string()
|
||||
})?;
|
||||
fs::write(file_path, new_content).map_err(|_| EhError::HashFixFailed {
|
||||
path: file_path.to_string_lossy().to_string(),
|
||||
})?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue