Skip to content

Commit

Permalink
feat: ✨ Allow to run nur --help and nur --version when no project pat…
Browse files Browse the repository at this point in the history
…h exists
  • Loading branch information
ddanier committed Apr 1, 2024
1 parent cce410a commit ee2b612
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
20 changes: 18 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ use std::process::ExitCode;
fn main() -> Result<ExitCode, miette::ErrReport> {
// Get initial directory details
let run_path = get_init_cwd();
let project_path = find_project_path(&run_path)?;
let found_project_path = find_project_path(&run_path);
let has_project_path = found_project_path.is_some();
let project_path = found_project_path.unwrap_or(&run_path);

// Initialize nu engine state and stack
let mut engine_state = init_engine_state(project_path)?;
Expand All @@ -60,7 +62,21 @@ fn main() -> Result<ExitCode, miette::ErrReport> {
}

// Show hints for compatibility issues
show_nurscripts_hint(project_path, use_color);
if has_project_path {
show_nurscripts_hint(project_path, use_color);
}

// Handle execution without project path, only allow to show help, abort otherwise
if !has_project_path {
if parsed_nur_args.show_help {
let mut nur_engine = NurEngine::new(engine_state, stack);
nur_engine.print_help(&Nur);

std::process::exit(0);
} else {
return Err(miette::ErrReport::from(NurError::NurfileNotFound()));
}
}

// Base path for nur config/env
let nur_config_dir = project_path.join(NUR_CONFIG_PATH);
Expand Down
14 changes: 5 additions & 9 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use crate::errors::{NurError, NurResult};
use crate::names::NUR_FILE;
use std::path::Path;

pub(crate) fn find_project_path(cwd: &Path) -> NurResult<&Path> {
pub(crate) fn find_project_path(cwd: &Path) -> Option<&Path> {
let mut path = cwd;

loop {
let taskfile_path = path.join(NUR_FILE);
if taskfile_path.exists() {
return Ok(path);
return Some(path);
}

if let Some(parent) = path.parent() {
path = parent;
} else {
return Err(NurError::NurfileNotFound());
return None;
}
}
}
Expand Down Expand Up @@ -72,11 +71,8 @@ mod tests {

// Test the function with the temporary directory as the current working directory
match find_project_path(&temp_dir_path) {
Ok(_) => panic!("Expected an error, but got Ok"),
Err(e) => match e {
NurError::NurfileNotFound() => (), // Test passes
_ => panic!("Expected NurTaskfileNotFound, but got a different error"),
},
Some(_) => panic!("Expected an error, but got Ok"),
None => (),
}
}
}

0 comments on commit ee2b612

Please sign in to comment.