git.delta.rocks / jrsonnet / refs/commits / a1a72ceb9060

difftreelog

feat reenable indicatif, since the eaten line was fixed

Lach2025-03-29parent: #4e5d91e.patch.diff
in: trunk

3 files changed

modifiedcmds/fleet/Cargo.tomldiffbeforeafterboth
--- a/cmds/fleet/Cargo.toml
+++ b/cmds/fleet/Cargo.toml
@@ -49,6 +49,7 @@
 fleet-base = { version = "0.1.0", path = "../../crates/fleet-base" }
 
 [features]
+default = ["indicatif"]
 # Not quite stable
 indicatif = [
 	"dep:tracing-indicatif",
modifiedcmds/fleet/src/main.rsdiffbeforeafterboth
--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -185,10 +185,6 @@
 #[tokio::main]
 async fn async_main(opts: RootOpts) -> ExitCode {
 	if let Err(e) = main_real(opts).await {
-		// If I remove this line, the next error!() line gets eaten.
-		// This is a bug in indicatif, it needs to be fixed
-		#[cfg(feature = "indicatif")]
-		info!("fixme: this line gets eaten by tracing-indicatif on levels info+");
 		error!("{e:#}");
 		return ExitCode::FAILURE;
 	}
modifiedcrates/fleet-base/src/opts.rsdiffbeforeafterboth
before · crates/fleet-base/src/opts.rs
1use std::{2	collections::BTreeMap,3	env::current_dir,4	ffi::OsString,5	str::FromStr,6	sync::{Arc, Mutex},7};89use anyhow::Result;10use clap::Parser;11use nix_eval::{nix_go, util::assert_warn, NixSessionPool, Value};12use nom::{13	bytes::complete::take_while1,14	character::complete::char,15	combinator::{map, opt},16	multi::separated_list1,17	sequence::{preceded, separated_pair},18};1920use crate::{21	fleetdata::FleetData,22	host::{Config, ConfigHost, FleetConfigInternals},23};2425#[derive(Clone)]26pub enum HostItem {27	Host {28		name: String,29		attrs: BTreeMap<String, String>,30	},31	Tag {32		name: String,33		attrs: BTreeMap<String, String>,34	},35}36fn host_item_parser(input: &str) -> Result<HostItem, String> {37	fn err_to_string(err: nom::Err<nom::error::Error<&str>>) -> String {38		err.to_string()39	}4041	let (input, is_tag) = map(opt(char('@')), |c| c.is_some())(input).map_err(err_to_string)?;42	let (input, name) = map(43		take_while1(|v| v != ',' && v != '?' && v != '@'),44		str::to_owned,45	)(input)46	.map_err(err_to_string)?;4748	let kw_item = separated_pair(49		map(take_while1(|v| v != '&' && v != '='), str::to_owned),50		char('='),51		map(take_while1(|v| v != '&'), str::to_owned),52	);53	let kw = map(separated_list1(char('&'), kw_item), |vec| {54		vec.into_iter().collect::<BTreeMap<_, _>>()55	});56	let mut opt_kw = map(opt(preceded(char('?'), kw)), Option::unwrap_or_default);5758	let (input, attrs) = opt_kw(input).map_err(err_to_string)?;5960	if !input.is_empty() {61		return Err(format!("unexpected trailing input: {input:?}"));62	}63	Ok(if is_tag {64		HostItem::Tag { name, attrs }65	} else {66		HostItem::Host { name, attrs }67	})68}6970// TODO: Rename to HostSelector71#[derive(Parser, Clone)]72pub struct FleetOpts {73	/// All hosts except those would be skipped74	#[clap(long, number_of_values = 1, value_parser = host_item_parser)]75	pub only: Vec<HostItem>,7677	/// Hosts to skip78	#[clap(long, number_of_values = 1)]79	pub skip: Vec<String>,8081	/// Host, which should be threaten as current machine82	// TODO: Replace with connectivity refactor83	#[clap(long, default_value_t = hostname::get().expect("unknown hostname").to_str().expect("hostname is not utf-8").to_owned())]84	pub localhost: String,8586	/// Override detected system for host, to perform builds via87	/// binfmt-declared qemu instead of trying to crosscompile88	#[clap(long, default_value = env!("NIX_SYSTEM"))]89	pub local_system: String,90}9192impl FleetOpts {93	pub async fn filter_skipped(94		&self,95		hosts: impl IntoIterator<Item = ConfigHost>,96	) -> Result<Vec<ConfigHost>> {97		let mut out = Vec::new();98		for host in hosts {99			if self.should_skip(&host).await? {100				continue;101			}102			out.push(host);103		}104		Ok(out)105	}106	pub async fn should_skip(&self, host: &ConfigHost) -> Result<bool> {107		if self.skip.iter().any(|h| h as &str == host.name) {108			return Ok(true);109		}110		if self.only.is_empty() {111			return Ok(false);112		}113		let mut have_group_matches = false;114		for item in self.only.iter() {115			match item {116				HostItem::Host { name, .. } if *name == host.name => {117					return Ok(false);118				}119				HostItem::Tag { .. } => {120					have_group_matches = true;121				}122				_ => {}123			}124		}125		if have_group_matches {126			let host_tags = host.tags().await?;127			for item in self.only.iter() {128				match item {129					HostItem::Tag { name, .. } if host_tags.contains(name) => {130						return Ok(false);131					}132					_ => {}133				}134			}135		}136		Ok(true)137	}138	pub async fn action_attr<T: FromStr>(&self, host: &ConfigHost, attr: &str) -> Result<Option<T>>139	where140		T::Err: Sync,141		anyhow::Error: From<T::Err>,142	{143		let str = self.action_attr_str(host, attr).await?;144		Ok(str.map(|v| T::from_str(&v)).transpose()?)145	}146	pub async fn action_attr_str(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {147		if self.only.is_empty() {148			return Ok(None);149		}150		let mut have_group_matches = false;151		for item in self.only.iter() {152			match item {153				HostItem::Host { name, attrs }154					if *name == host.name && attrs.contains_key(attr) =>155				{156					return Ok(attrs.get(attr).cloned());157				}158				HostItem::Tag { attrs, .. } if attrs.contains_key(attr) => {159					have_group_matches = true;160				}161				_ => {}162			}163		}164		if have_group_matches {165			let host_tags = host.tags().await?;166			for item in self.only.iter() {167				match item {168					HostItem::Tag { name, attrs }169						if host_tags.contains(name) && attrs.contains_key(attr) =>170					{171						return Ok(attrs.get(attr).cloned());172					}173					_ => {}174				}175			}176		}177		Ok(None)178	}179	pub fn is_local(&self, host: &str) -> bool {180		self.localhost == host181	}182183	// TODO: Config should be detached from opts.184	pub async fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {185		let directory = current_dir()?;186187		let pool = NixSessionPool::new(188			directory.as_os_str().to_owned(),189			nix_args.clone(),190			self.local_system.clone(),191		)192		.await?;193		let nix_session = pool.get().await?;194195		let builtins_field = Value::binding(nix_session.clone(), "builtins").await?;196197		let mut fleet_data_path = directory.clone();198		fleet_data_path.push("fleet.nix");199		let bytes = std::fs::read_to_string(fleet_data_path)?;200		let data: Mutex<FleetData> = nixlike::parse_str(&bytes)?;201202		let fleet_root = Value::binding(nix_session.clone(), "fleetConfigurations").await?;203		let fleet_field = nix_go!(fleet_root.default({ data }));204205		let config_field = nix_go!(fleet_field.config);206207		if assert {208			assert_warn("fleet config evaluation", &config_field).await?;209		}210211		let import = nix_go!(builtins_field.import);212		let overlays = nix_go!(config_field.nixpkgs.overlays);213		let nixpkgs = nix_go!(config_field.nixpkgs.buildUsing);214		let nixpkgs_imported = nix_go!(nixpkgs | import);215216		let default_pkgs = nix_go!(nixpkgs_imported(Obj {217			overlays,218			system: self.local_system.clone(),219		}));220221		Ok(Config(Arc::new(FleetConfigInternals {222			nix_session,223			directory,224			data,225			local_system: self.local_system.clone(),226			nix_args,227			config_field,228			default_pkgs,229			nixpkgs,230			localhost: self.localhost.to_owned(),231		})))232	}233}