From 1e385824510ca39ef243c4e697278a14659b8246 Mon Sep 17 00:00:00 2001 From: i1i1 Date: Thu, 10 Aug 2023 01:06:00 +0300 Subject: [PATCH] Parse value for `NodeFilter` with clap --- src/command/apply.rs | 8 ++------ src/nix/node_filter.rs | 11 ++++++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/command/apply.rs b/src/command/apply.rs index ba58e2b..e102555 100644 --- a/src/command/apply.rs +++ b/src/command/apply.rs @@ -172,11 +172,7 @@ pub async fn run(_global_args: &ArgMatches, local_args: &ArgMatches) -> Result<( let goal_arg = local_args.get_one::("goal").unwrap(); let goal = Goal::from_str(goal_arg).unwrap(); - // FIXME: Just get_one:: - let filter = local_args - .get_one::("on") - .map(NodeFilter::new) - .transpose()?; + let filter = local_args.get_one::("on"); if filter.is_none() && goal != Goal::Build { // User did not specify node, we should check meta and see rules @@ -189,7 +185,7 @@ pub async fn run(_global_args: &ArgMatches, local_args: &ArgMatches) -> Result<( } let targets = hive - .select_nodes(filter, ssh_config, goal.requires_target_host()) + .select_nodes(filter.cloned(), ssh_config, goal.requires_target_host()) .await?; let n_targets = targets.len(); diff --git a/src/nix/node_filter.rs b/src/nix/node_filter.rs index 6f645ca..a796d1e 100644 --- a/src/nix/node_filter.rs +++ b/src/nix/node_filter.rs @@ -3,12 +3,14 @@ use std::collections::HashSet; use std::convert::AsRef; use std::iter::{FromIterator, Iterator}; +use std::str::FromStr; use glob::Pattern as GlobPattern; use super::{ColmenaError, ColmenaResult, NodeConfig, NodeName}; /// A node filter containing a list of rules. +#[derive(Clone)] pub struct NodeFilter { rules: Vec, } @@ -16,7 +18,7 @@ pub struct NodeFilter { /// A filter rule. /// /// The filter rules are OR'd together. -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq)] enum Rule { /// Matches a node's attribute name. MatchName(GlobPattern), @@ -25,6 +27,13 @@ enum Rule { MatchTag(GlobPattern), } +impl FromStr for NodeFilter { + type Err = ColmenaError; + fn from_str(s: &str) -> Result { + Self::new(s) + } +} + impl NodeFilter { /// Creates a new filter using an expression passed using `--on`. pub fn new>(filter: S) -> ColmenaResult {