git.delta.rocks / jrsonnet / refs/commits / 754b45cdacd0

difftreelog

fix make indicatif optional

Yaroslav Bolyukin2024-03-24parent: #9b6d5bf.patch.diff
in: trunk

7 files changed

modifiedcmds/fleet/Cargo.tomldiffbeforeafterboth
--- a/cmds/fleet/Cargo.toml
+++ b/cmds/fleet/Cargo.toml
@@ -22,26 +22,29 @@
 base64 = "0.21"
 chrono = { version = "0.4", features = ["serde"] }
 z85 = "3.0"
-clap = { version = "4.5", features = [
-	"derive",
-	"env",
-	"wrap_help",
-	"unicode",
-] }
+clap = { version = "4.5", features = ["derive", "env", "wrap_help", "unicode"] }
 tracing = "0.1"
 tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] }
 tokio-util = { version = "0.7", features = ["codec"] }
 async-trait = "0.1"
 futures = "0.3"
-tracing-indicatif = "0.3"
-indicatif = "0.17"
 itertools = "0.12"
 shlex = "1.3"
 tabled = { version = "0.15" }
-owo-colors = { version = "4.0", features = ["supports-color", "supports-colors"] }
+owo-colors = { version = "4.0", features = [
+	"supports-color",
+	"supports-colors",
+] }
 r2d2 = "0.8.10"
 abort-on-drop = "0.2"
 unindent = "0.2"
 regex = "1.10"
 openssh = "0.10"
-human-repr = "1.1"
+
+tracing-indicatif = { version = "0.3", optional = true }
+human-repr = { version = "1.1", optional = true }
+indicatif = { version = "0.17", optional = true }
+
+[features]
+# Not quite stable
+indicatif = ["tracing-indicatif", "dep:indicatif", "human-repr", "better-command/indicatif"]
modifiedcmds/fleet/src/main.rsdiffbeforeafterboth
before · cmds/fleet/src/main.rs
1#![recursion_limit = "512"]2#![feature(try_blocks, lint_reasons)]34pub(crate) mod cmds;5pub(crate) mod command;6pub(crate) mod host;7pub(crate) mod keys;89pub(crate) mod better_nix_eval;10pub(crate) mod extra_args;1112mod fleetdata;1314use std::time::Duration;15use std::{ffi::OsString, process::ExitCode};1617use anyhow::{bail, Result};18use clap::Parser;1920use cmds::{21	build_systems::{BuildSystems, Deploy},22	info::Info,23	secrets::Secret,24};25use futures::future::LocalBoxFuture;26use futures::stream::FuturesUnordered;27use futures::TryStreamExt;28use host::{Config, FleetOpts};29use human_repr::HumanCount;30use indicatif::{ProgressState, ProgressStyle};31use tracing::{error, info};32use tracing::{info_span, Instrument};33use tracing_indicatif::IndicatifLayer;34use tracing_subscriber::{prelude::*, EnvFilter};3536use crate::command::MyCommand;3738#[derive(Parser)]39struct Prefetch {}40impl Prefetch {41	async fn run(&self, config: &Config) -> Result<()> {42		let mut prefetch_dir = config.directory.to_path_buf();43		prefetch_dir.push("prefetch");44		if !prefetch_dir.is_dir() {45			info!("nothing to prefetch: no prefetch directory");46			return Ok(());47		}48		let tasks = <FuturesUnordered<LocalBoxFuture<Result<()>>>>::new();49		for entry in std::fs::read_dir(&prefetch_dir)? {50			tasks.push(Box::pin(async {51				let entry = entry?;52				if !entry.metadata()?.is_file() {53					bail!("only files should exist in prefetch directory");54				}55				let span = info_span!(56					"prefetching",57					name = entry.file_name().to_string_lossy().as_ref()58				);59				let mut path = OsString::new();60				path.push("file://");61				path.push(entry.path());6263				let mut status = MyCommand::new("nix");64				status.args(&config.nix_args);65				status.arg("store").arg("prefetch-file").arg(path);66				status.run_nix_string().instrument(span).await?;67				Ok(())68			}));69		}70		tasks.try_collect::<Vec<()>>().await?;71		Ok(())72	}73}7475#[derive(Parser)]76enum Opts {77	/// Prepare systems for deployments78	BuildSystems(BuildSystems),7980	Deploy(Deploy),81	/// Secret management82	#[clap(subcommand)]83	Secret(Secret),84	/// Upload prefetch directory to the nix store85	Prefetch(Prefetch),86	/// Config parsing87	Info(Info),88}8990#[derive(Parser)]91#[clap(version, author)]92struct RootOpts {93	#[clap(flatten)]94	fleet_opts: FleetOpts,95	#[clap(subcommand)]96	command: Opts,97}9899async fn run_command(config: &Config, command: Opts) -> Result<()> {100	match command {101		Opts::BuildSystems(c) => c.run(config).await?,102		Opts::Deploy(d) => d.run(config).await?,103		Opts::Secret(s) => s.run(config).await?,104		Opts::Info(i) => i.run(config).await?,105		Opts::Prefetch(p) => p.run(config).await?,106	};107	Ok(())108}109110fn setup_logging() {111	let indicatif_layer = IndicatifLayer::new().with_progress_style(112		ProgressStyle::with_template(113			"{color_start}{span_child_prefix} {span_name}{{{span_fields}}}{color_end} {wide_msg} {color_start}{download_progress} {elapsed}{color_end}",114		)115		.unwrap()116		.with_key("download_progress", |state: &ProgressState, writer: &mut dyn std::fmt::Write| {117			let Some(len) = state.len() else {118				return;119			};120			let pos = state.pos();121			if pos > len {122				let _ = write!(writer, "{}", pos.human_count_bare());123			} else {124				let _ = write!(writer, "{} / {}", pos.human_count_bare(), len.human_count_bare());125			}126		})127		.with_key(128			"color_start",129			|state: &ProgressState, writer: &mut dyn std::fmt::Write| {130				let elapsed = state.elapsed();131132				if elapsed > Duration::from_secs(60) {133					// Red134					let _ = write!(writer, "\x1b[{}m", 1 + 30);135				} else if elapsed > Duration::from_secs(30) {136					// Yellow137					let _ = write!(writer, "\x1b[{}m", 3 + 30);138				}139			},140		)141		.with_key(142			"color_end",143			|state: &ProgressState, writer: &mut dyn std::fmt::Write| {144				if state.elapsed() > Duration::from_secs(30) {145					let _ = write!(writer, "\x1b[0m");146				}147			},148		),149	);150151	let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));152153	tracing_subscriber::registry()154		.with(155			tracing_subscriber::fmt::layer()156				.without_time()157				.with_target(true)158				.with_writer(indicatif_layer.get_stdout_writer())159				.with_filter(filter), // .withou,160		)161		.with(indicatif_layer)162		.init();163}164165#[tokio::main]166async fn main() -> ExitCode {167	setup_logging();168	if let Err(e) = main_real().await {169		// If I remove this line, the next error!() line gets eaten.170		info!("fixme: this line gets eaten by tracing-indicatif on levels info+");171		error!("{e:#}");172		return ExitCode::FAILURE;173	}174	ExitCode::SUCCESS175}176177async fn main_real() -> Result<()> {178	let _ = better_nix_eval::TOKIO_RUNTIME.set(tokio::runtime::Handle::current());179180	let nix_args = std::env::var_os("NIX_ARGS")181		.map(|a| extra_args::parse_os(&a))182		.transpose()?183		.unwrap_or_default();184	let opts = RootOpts::parse();185	let config = opts.fleet_opts.build(nix_args).await?;186187	match run_command(&config, opts.command).await {188		Ok(()) => {189			config.save()?;190			Ok(())191		}192		Err(e) => {193			let _ = config.save();194			Err(e)195		}196	}197}
after · cmds/fleet/src/main.rs
1#![recursion_limit = "512"]2#![feature(try_blocks, lint_reasons)]34pub(crate) mod cmds;5pub(crate) mod command;6pub(crate) mod host;7pub(crate) mod keys;89pub(crate) mod better_nix_eval;10pub(crate) mod extra_args;1112mod fleetdata;1314use std::time::Duration;15use std::{ffi::OsString, process::ExitCode};1617use anyhow::{bail, Result};18use clap::Parser;1920use cmds::{21	build_systems::{BuildSystems, Deploy},22	info::Info,23	secrets::Secret,24};25use futures::future::LocalBoxFuture;26use futures::stream::FuturesUnordered;27use futures::TryStreamExt;28use host::{Config, FleetOpts};29#[cfg(feature = "indicatif")]30use human_repr::HumanCount;31#[cfg(feature = "indicatif")]32use indicatif::{ProgressState, ProgressStyle};33use tracing::{error, info};34use tracing::{info_span, Instrument};35#[cfg(feature = "indicatif")]36use tracing_indicatif::IndicatifLayer;37use tracing_subscriber::{prelude::*, EnvFilter};3839use crate::command::MyCommand;4041#[derive(Parser)]42struct Prefetch {}43impl Prefetch {44	async fn run(&self, config: &Config) -> Result<()> {45		let mut prefetch_dir = config.directory.to_path_buf();46		prefetch_dir.push("prefetch");47		if !prefetch_dir.is_dir() {48			info!("nothing to prefetch: no prefetch directory");49			return Ok(());50		}51		let tasks = <FuturesUnordered<LocalBoxFuture<Result<()>>>>::new();52		for entry in std::fs::read_dir(&prefetch_dir)? {53			tasks.push(Box::pin(async {54				let entry = entry?;55				if !entry.metadata()?.is_file() {56					bail!("only files should exist in prefetch directory");57				}58				let span = info_span!(59					"prefetching",60					name = entry.file_name().to_string_lossy().as_ref()61				);62				let mut path = OsString::new();63				path.push("file://");64				path.push(entry.path());6566				let mut status = MyCommand::new("nix");67				status.args(&config.nix_args);68				status.arg("store").arg("prefetch-file").arg(path);69				status.run_nix_string().instrument(span).await?;70				Ok(())71			}));72		}73		tasks.try_collect::<Vec<()>>().await?;74		Ok(())75	}76}7778#[derive(Parser)]79enum Opts {80	/// Prepare systems for deployments81	BuildSystems(BuildSystems),8283	Deploy(Deploy),84	/// Secret management85	#[clap(subcommand)]86	Secret(Secret),87	/// Upload prefetch directory to the nix store88	Prefetch(Prefetch),89	/// Config parsing90	Info(Info),91}9293#[derive(Parser)]94#[clap(version, author)]95struct RootOpts {96	#[clap(flatten)]97	fleet_opts: FleetOpts,98	#[clap(subcommand)]99	command: Opts,100}101102async fn run_command(config: &Config, command: Opts) -> Result<()> {103	match command {104		Opts::BuildSystems(c) => c.run(config).await?,105		Opts::Deploy(d) => d.run(config).await?,106		Opts::Secret(s) => s.run(config).await?,107		Opts::Info(i) => i.run(config).await?,108		Opts::Prefetch(p) => p.run(config).await?,109	};110	Ok(())111}112113fn setup_logging() {114	#[cfg(feature = "indicatif")]115	let indicatif_layer = IndicatifLayer::new().with_progress_style(116		ProgressStyle::with_template(117			"{color_start}{span_child_prefix} {span_name}{{{span_fields}}}{color_end} {wide_msg} {color_start}{download_progress} {elapsed}{color_end}",118		)119		.unwrap()120		.with_key("download_progress", |state: &ProgressState, writer: &mut dyn std::fmt::Write| {121			let Some(len) = state.len() else {122				return;123			};124			let pos = state.pos();125			if pos > len {126				let _ = write!(writer, "{}", pos.human_count_bare());127			} else {128				let _ = write!(writer, "{} / {}", pos.human_count_bare(), len.human_count_bare());129			}130		})131		.with_key(132			"color_start",133			|state: &ProgressState, writer: &mut dyn std::fmt::Write| {134				use std::time::Duration;135				let elapsed = state.elapsed();136137				if elapsed > Duration::from_secs(60) {138					// Red139					let _ = write!(writer, "\x1b[{}m", 1 + 30);140				} else if elapsed > Duration::from_secs(30) {141					// Yellow142					let _ = write!(writer, "\x1b[{}m", 3 + 30);143				}144			},145		)146		.with_key(147			"color_end",148			|state: &ProgressState, writer: &mut dyn std::fmt::Write| {149				if state.elapsed() > Duration::from_secs(30) {150					let _ = write!(writer, "\x1b[0m");151				}152			},153		),154	);155156	let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));157158	let reg = tracing_subscriber::registry().with({159		let sub = tracing_subscriber::fmt::layer()160			.without_time()161			.with_target(true);162		#[cfg(feature = "indicatif")]163		let sub = sub.with_writer(indicatif_layer.get_stdout_writer());164		sub.with_filter(filter) // .withou,165	});166	// #[cfg(feature = "indicatif")]167	#[cfg(feature = "indicatif")]168	let reg = reg.with(indicatif_layer);169	reg.init();170}171172#[tokio::main]173async fn main() -> ExitCode {174	setup_logging();175	if let Err(e) = main_real().await {176		// If I remove this line, the next error!() line gets eaten.177		info!("fixme: this line gets eaten by tracing-indicatif on levels info+");178		error!("{e:#}");179		return ExitCode::FAILURE;180	}181	ExitCode::SUCCESS182}183184async fn main_real() -> Result<()> {185	let _ = better_nix_eval::TOKIO_RUNTIME.set(tokio::runtime::Handle::current());186187	let nix_args = std::env::var_os("NIX_ARGS")188		.map(|a| extra_args::parse_os(&a))189		.transpose()?190		.unwrap_or_default();191	let opts = RootOpts::parse();192	let config = opts.fleet_opts.build(nix_args).await?;193194	match run_command(&config, opts.command).await {195		Ok(()) => {196			config.save()?;197			Ok(())198		}199		Err(e) => {200			let _ = config.save();201			Err(e)202		}203	}204}
modifiedcmds/install-secrets/Cargo.tomldiffbeforeafterboth
--- a/cmds/install-secrets/Cargo.toml
+++ b/cmds/install-secrets/Cargo.toml
@@ -6,7 +6,7 @@
 [dependencies]
 age = { version = "0.10.0", features = ["ssh"] }
 anyhow = "1.0.79"
-tracing-subscriber = "0.3"
+tracing-subscriber = { version = "0.3", features = ["env-filter"] }
 tracing = "0.1"
 nix = {version = "0.27.1", features = ["user", "fs"]}
 serde = { version = "1.0.196", features = ["derive"] }
modifiedcrates/better-command/Cargo.tomldiffbeforeafterboth
--- a/crates/better-command/Cargo.toml
+++ b/crates/better-command/Cargo.toml
@@ -9,4 +9,8 @@
 serde = { version = "1.0", features = ["derive"] }
 serde_json = "1.0"
 tracing = "0.1"
-tracing-indicatif = "0.3"
+
+tracing-indicatif = { version = "0.3", optional = true }
+
+[features]
+indicatif = ["tracing-indicatif"]
modifiedcrates/better-command/src/handler.rsdiffbeforeafterboth
--- a/crates/better-command/src/handler.rs
+++ b/crates/better-command/src/handler.rs
@@ -6,7 +6,8 @@
 use once_cell::sync::Lazy;
 use regex::Regex;
 use serde::Deserialize;
-use tracing::{Span, info, warn, info_span};
+use tracing::{info, info_span, warn, Span};
+#[cfg(feature = "indicatif")]
 use tracing_indicatif::span_ext::IndicatifSpanExt as _;
 
 pub trait Handler: Send {
@@ -141,6 +142,7 @@
 						}
 						info!(target: "nix","building {}", drv);
 						let span = info_span!("build", drv);
+						#[cfg(feature = "indicatif")]
 						span.pb_start();
 						self.spans.insert(id, span);
 					} else {
@@ -167,6 +169,7 @@
 						}
 						info!(target: "nix","copying {} {} -> {}", drv, from, to);
 						let span = info_span!("copy", from, to, drv);
+						#[cfg(feature = "indicatif")]
 						span.pb_start();
 						self.spans.insert(id, span);
 					} else {
@@ -183,8 +186,11 @@
 						&& !(text.starts_with("copying '") && text.ends_with("' to the store"))
 					{
 						let span = info_span!("job");
-						span.pb_start();
-						span.pb_set_message(&process_message(text.trim()));
+						#[cfg(feature = "indicatif")]
+						{
+							span.pb_start();
+							span.pb_set_message(&process_message(text.trim()));
+						}
 						self.spans.insert(id, span);
 						info!(target: "nix", "{}", text);
 					}
@@ -254,6 +260,7 @@
 						}
 					}
 					let span = info_span!("waiting on drv", drv);
+					#[cfg(feature = "indicatif")]
 					span.pb_start();
 					self.spans.insert(id, span);
 					// Concurrent build of the same message
@@ -264,7 +271,10 @@
 				NixLog::Result { fields, id, typ } if typ == 101 && !fields.is_empty() => {
 					if let Some(span) = self.spans.get(&id) {
 						if let LogField::String(s) = &fields[0] {
+							#[cfg(feature = "indicatif")]
 							span.pb_set_message(&process_message(s.trim()));
+							#[cfg(not(feature = "indicatif"))]
+							info!("{}", process_message(s));
 						} else {
 							warn!("bad fields: {fields:?}");
 						}
@@ -278,8 +288,12 @@
 						if let [LogField::Num(done), LogField::Num(expected), LogField::Num(_running), LogField::Num(_failed)] =
 							&fields[..4]
 						{
-							span.pb_set_length(*expected);
-							span.pb_set_position(*done);
+							#[cfg(feature = "indicatif")]
+							{
+								span.pb_set_length(*expected);
+								span.pb_set_position(*done);
+							}
+							let _ = (span, done, expected);
 						} else {
 							warn!("bad fields: {fields:?}");
 						}
modifiedlib/fleetLib.nixdiffbeforeafterboth
--- a/lib/fleetLib.nix
+++ b/lib/fleetLib.nix
@@ -34,6 +34,9 @@
     then "${this}-${other}"
     else "${other}-${this}";
 
-  # For places, where fleet knows better than nixpkgs defaults
+  # mkDefault = mkOverride 1000
+  # For places, where fleet knows better than nixpkgs defaults.
   mkFleetDefault = mkOverride 999;
+  # Some generators use mkDefault, but optionDefault is set by nixpkgs.
+  mkFleetGeneratorDefault = mkOverride 1001;
 }
modifiedmodules/fleet/meta.nixdiffbeforeafterboth
--- a/modules/fleet/meta.nix
+++ b/modules/fleet/meta.nix
@@ -34,9 +34,14 @@
           type = unspecified;
           description = "Nixos configuration";
         };
+        nixpkgs = mkOption {
+          type = unspecified;
+          description = "Nixpkgs override";
+          default = nixpkgs;
+        };
       };
       config = {
-        nixosSystem = nixpkgs.lib.nixosSystem {
+        nixosSystem = hostConfig.config.nixpkgs.lib.nixosSystem {
           inherit (hostConfig.config) system modules;
           specialArgs = {
             inherit fleetLib;
@@ -45,7 +50,7 @@
         };
         modules = [
           ({...}: {
-            networking.hostName = mkFleetDefault hostName;
+            networking.hostName = mkFleetGeneratorDefault hostName;
           })
         ];
       };