Skip to content

Commit

Permalink
feat: Add support for clap deriving
Browse files Browse the repository at this point in the history
This allows creating `NixCmd` directly off user passed CLI arguments
  • Loading branch information
srid committed Jun 5, 2024
1 parent a287ab2 commit f61bd2c
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 13 deletions.
122 changes: 121 additions & 1 deletion Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ shell-words = { version = "1.1.0" }
is_proc_translated = { version = "0.1.1" }
sysinfo.version = "0.29.10"
bytesize = { version = "1.3.0", features = ["serde"] }
clap = { version = "4.4", features = ["derive"], optional = true }

[features]
clap = ["dep:clap"]
26 changes: 14 additions & 12 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@ use tokio::process::Command;

use tracing::instrument;

#[cfg(feature = "clap")]
use clap;

/// The `nix` command's global options.
///
/// See [available global
/// options](https://nixos.org/manual/nix/stable/command-ref/new-cli/nix#options)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[cfg_attr(feature = "clap", derive(clap::Parser))]
pub struct NixCmd {
/// Append to the experimental-features setting of Nix.
#[cfg_attr(feature = "clap", arg(long))]
pub extra_experimental_features: Vec<String>,
pub extra_access_tokens: Vec<String>,
pub refresh: Refresh,
}

/// Whether to refresh the flake, by passing `--refresh` to nix
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct Refresh(bool);
/// Append to the access-tokens setting of Nix.
#[cfg_attr(feature = "clap", arg(long))]
pub extra_access_tokens: Vec<String>,

impl From<bool> for Refresh {
fn from(b: bool) -> Self {
Self(b)
}
/// Consider all previously downloaded files out-of-date.
#[cfg_attr(feature = "clap", arg(long))]
pub refresh: bool,
}

impl Default for NixCmd {
Expand All @@ -46,7 +48,7 @@ impl Default for NixCmd {
Self {
extra_experimental_features: vec!["nix-command".to_string(), "flakes".to_string()],
extra_access_tokens: vec![],
refresh: false.into(),
refresh: false,
}
}
}
Expand Down Expand Up @@ -161,7 +163,7 @@ impl NixCmd {
args.push("--extra-access-tokens".to_string());
args.push(self.extra_access_tokens.join(" "));
}
if self.refresh.0 {
if self.refresh {
args.push("--refresh".to_string());
}
args
Expand Down

0 comments on commit f61bd2c

Please sign in to comment.