From 4cce06d5fb4cfb302f4231587fa0a520d849d8d1 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 22 Jul 2024 01:50:25 -0700 Subject: [PATCH] InvalidHive error --- src/cli.rs | 45 +++++++++++++++++++++++++-------------------- src/error.rs | 4 ++++ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 7acc241..6041a0d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -9,8 +9,9 @@ use env_logger::fmt::WriteStyle; use crate::{ command::{self, apply::DeployOpts}, - error::ColmenaResult, + error::{ColmenaError, ColmenaResult}, nix::{Hive, HivePath}, + troubleshooter::run_wrapped, }; /// Base URL of the manual, without the trailing slash. @@ -232,14 +233,14 @@ async fn get_hive(opts: &Opts) -> ColmenaResult { } } - if file_path.is_none() { - log::error!( - "Could not find `hive.nix` or `flake.nix` in {:?} or any parent directory", - std::env::current_dir()? - ); + match file_path { + None => { + return Err(ColmenaError::InvalidHive { + current_dir: std::env::current_dir()?, + }) + } + Some(file_path) => HivePath::from_path(file_path).await?, } - - HivePath::from_path(file_path.unwrap()).await? } }; @@ -283,33 +284,37 @@ pub async fn run() { return; } - let hive = get_hive(&opts).await.expect("Failed to get flake or hive"); + let hive = run_wrapped(get_hive(&opts), None).await; + let config = opts.config; + let command = opts.command; - use crate::troubleshooter::run_wrapped as r; + run_wrapped(handle_command(command, hive), config).await; +} - match opts.command { - Command::Apply(args) => r(command::apply::run(hive, args), opts.config).await, +async fn handle_command(command: Command, hive: Hive) -> ColmenaResult<()> { + match command { + Command::Apply(args) => command::apply::run(hive, args).await, #[cfg(target_os = "linux")] - Command::ApplyLocal(args) => r(command::apply_local::run(hive, args), opts.config).await, - Command::Eval(args) => r(command::eval::run(hive, args), opts.config).await, - Command::Exec(args) => r(command::exec::run(hive, args), opts.config).await, - Command::NixInfo => r(command::nix_info::run(), opts.config).await, - Command::Repl => r(command::repl::run(hive), opts.config).await, + Command::ApplyLocal(args) => command::apply_local::run(hive, args).await, + Command::Eval(args) => command::eval::run(hive, args).await, + Command::Exec(args) => command::exec::run(hive, args).await, + Command::NixInfo => command::nix_info::run().await, + Command::Repl => command::repl::run(hive).await, #[cfg(debug_assertions)] - Command::TestProgress => r(command::test_progress::run(), opts.config).await, + Command::TestProgress => command::test_progress::run().await, Command::Build { deploy } => { let args = command::apply::Opts { deploy, goal: crate::nix::Goal::Build, }; - r(command::apply::run(hive, args), opts.config).await + command::apply::run(hive, args).await } Command::UploadKeys { deploy } => { let args = command::apply::Opts { deploy, goal: crate::nix::Goal::UploadKeys, }; - r(command::apply::run(hive, args), opts.config).await + command::apply::run(hive, args).await } Command::GenCompletions { .. } => unreachable!(), } diff --git a/src/error.rs b/src/error.rs index a9855e6..f9b72e9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ //! Custom error types. use std::os::unix::process::ExitStatusExt; +use std::path::PathBuf; use std::process::ExitStatus; use snafu::{Backtrace, Snafu}; @@ -78,6 +79,9 @@ pub enum ColmenaError { #[snafu(display("Exec failed on {} hosts", n_hosts))] ExecError { n_hosts: usize }, + + #[snafu(display("Could not find `hive.nix` or `flake.nix` in {} or any parent directory", current_dir.display()))] + InvalidHive { current_dir: PathBuf }, } impl From for ColmenaError {