Skip to content

Commit

Permalink
Merge pull request #152 from DeterminateSystems/lucperkins/fh-431-fh-…
Browse files Browse the repository at this point in the history
…init-offers-to-use-direnv-and-fails-if-it-isnt-installed

Change fh init behavior when executables aren't installed
  • Loading branch information
grahamc authored Nov 6, 2024
2 parents 2d0af28 + 5fe72c7 commit 4d3de3a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fh"
version = "0.1.18"
version = "0.1.19"
authors = ["Determinate Systems <[email protected]>"]
edition = "2021"
license = "Apache 2.0"
Expand Down
14 changes: 5 additions & 9 deletions src/cli/cmd/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl CommandExecute for InitSubcommand {
write(self.output, flake_string)?;

if project.has_directory(".git")
&& command_exists("git")?
&& command_exists("git")
&& Prompt::bool(&format!(
"Would you like to add your new Nix {} to Git?",
if use_flake_compat { "files" } else { "file" }
Expand All @@ -263,10 +263,10 @@ impl CommandExecute for InitSubcommand {
write(PathBuf::from(".envrc"), String::from("use flake"))?;

if Prompt::bool("You'll need to run `direnv allow` to activate direnv in this project. Would you like to do that now?") {
if command_exists("direnv")? {
if command_exists("direnv") {
Command::new("direnv").arg("allow").output()?;
} else {
println!("It looks like direnv isn't installed.");
println!("It looks like direnv isn't installed. Skipping `direnv allow`.");
}
}
}
Expand All @@ -280,12 +280,8 @@ impl CommandExecute for InitSubcommand {
}
}

pub(super) fn command_exists(cmd: &str) -> Result<bool, FhError> {
if Command::new(cmd).output().is_err() {
return Err(FhError::MissingExecutable(String::from(cmd)));
}

Ok(true)
pub(super) fn command_exists(cmd: &str) -> bool {
Command::new(cmd).output().is_ok()
}

async fn select_nixpkgs(api_addr: &str) -> Result<Url, FhError> {
Expand Down
4 changes: 3 additions & 1 deletion src/cli/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ fn is_root_user() -> bool {
}

async fn nix_command(args: &[String], sudo_if_necessary: bool) -> Result<(), FhError> {
command_exists("nix")?;
if !command_exists("nix") {
return Err(FhError::MissingExecutable("nix".to_string()));
}

let use_sudo = sudo_if_necessary && !is_root_user();

Expand Down

0 comments on commit 4d3de3a

Please sign in to comment.