Skip to content

Commit

Permalink
Provide enum for apply verb
Browse files Browse the repository at this point in the history
  • Loading branch information
lucperkins committed Jul 22, 2024
1 parent 31591d2 commit f1da923
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/cli/cmd/apply.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{os::unix::prelude::PermissionsExt, path::PathBuf, process::ExitCode};
use std::{fmt::Display, os::unix::prelude::PermissionsExt, path::PathBuf, process::ExitCode};

use clap::Parser;
use clap::{Parser, ValueEnum};
use color_eyre::eyre::Context;

use crate::cli::{
Expand Down Expand Up @@ -28,12 +28,35 @@ pub(crate) struct ApplySubcommand {
/// The command to run with the profile: bin/switch-to-configuration <verb>
/// For NixOS, this would be `switch`: bin/switch-to-configuration switch
#[arg(long, env = "FH_RESOLVE_VERB")]
verb: Option<String>,
verb: Option<Verb>,

#[clap(from_global)]
api_addr: url::Url,
}

#[derive(Clone, Debug, ValueEnum)]
pub enum Verb {
Switch,
Boot,
Test,
DryActivate,
}

impl Display for Verb {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Switch => "switch",
Self::Boot => "boot",
Self::Test => "test",
Self::DryActivate => "dry-activate",
}
)
}
}

#[async_trait::async_trait]
impl CommandExecute for ApplySubcommand {
async fn execute(self) -> color_eyre::Result<ExitCode> {
Expand Down Expand Up @@ -114,11 +137,11 @@ impl CommandExecute for ApplySubcommand {
tracing::info!(
"Switching configuration by running {} {}",
&switch_bin_path.display().to_string(),
verb,
verb.to_string(),
);

let output = tokio::process::Command::new(&switch_bin_path)
.args([&verb])
.args([&verb.to_string()])
.output()
.await
.wrap_err("failed to run switch-to-configuration")?;
Expand All @@ -131,8 +154,7 @@ impl CommandExecute for ApplySubcommand {
profile_path
);

println!("To update your machine, run:\n\n {profile_path}/bin/switch-to-configuration switch\n");
println!("For more information:\n\n {profile_path}/bin/switch-to-configuration --help");
println!("For more information on how to update your machine:\n\n {profile_path}/bin/switch-to-configuration --help");
}
} else {
tracing::debug!(
Expand Down

0 comments on commit f1da923

Please sign in to comment.