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

difftreelog

fix(fleet-cmd) include the required package for tab completions (#5)

Petr Portnov | PROgrm_JARvis2024-06-02parent: #ad7852d.patch.diff
in: trunk
* fix(fleet-cmd): include the required package for tab completions

* style(fleet-cmd): reformat automatically

9 files changed

modifiedcmds/fleet/src/better_nix_eval.rsdiffbeforeafterboth
--- a/cmds/fleet/src/better_nix_eval.rs
+++ b/cmds/fleet/src/better_nix_eval.rs
@@ -1,25 +1,24 @@
 //! Wrapper around nix repl, which allows to work on nix code, without relying on
 //! nix libexpr. I mean, nix libexpr is good, but until it has no C bindings, this is the royal PITA.
 
-use std::collections::HashMap;
-use std::ffi::{OsStr, OsString};
-use std::fmt::{self, Display};
-use std::path::PathBuf;
-use std::process::Stdio;
-use std::sync::{Arc, OnceLock};
+use std::{
+	collections::HashMap,
+	ffi::{OsStr, OsString},
+	fmt::{self, Display},
+	path::PathBuf,
+	process::Stdio,
+	sync::{Arc, OnceLock},
+};
 
 use anyhow::{anyhow, bail, ensure, Context, Result};
 use better_command::{ClonableHandler, Handler, NixHandler, NoopHandler};
 use futures::StreamExt;
 use itertools::Itertools;
-use serde::de::DeserializeOwned;
-use serde::{Deserialize, Serialize};
-use tokio::io::AsyncWriteExt;
-use tokio::process::{ChildStderr, ChildStdin, ChildStdout, Command};
-use tokio::select;
-use tokio::sync::{mpsc, oneshot, Mutex};
+use serde::{de::DeserializeOwned, Deserialize, Serialize};
+use tokio::{
+	io::AsyncWriteExt,
+	process::{ChildStderr, ChildStdin, ChildStdout, Command},
+	select,
+	sync::{mpsc, oneshot, Mutex},
+};
 use tracing::{debug, error, warn, Level};
-
-
-
-
modifiedcmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth
--- a/cmds/fleet/src/cmds/build_systems.rs
+++ b/cmds/fleet/src/cmds/build_systems.rs
@@ -1,9 +1,5 @@
-use std::os::unix::fs::symlink;
-use std::path::PathBuf;
-use std::{env::current_dir, time::Duration};
+use std::{env::current_dir, os::unix::fs::symlink, path::PathBuf, time::Duration};
 
-use crate::command::MyCommand;
-use crate::host::{Config, ConfigHost};
 use anyhow::{anyhow, Result};
 use clap::{Parser, ValueEnum};
 use itertools::Itertools as _;
@@ -11,6 +7,11 @@
 use tokio::{task::LocalSet, time::sleep};
 use tracing::{error, field, info, info_span, warn, Instrument};
 
+use crate::{
+	command::MyCommand,
+	host::{Config, ConfigHost},
+};
+
 #[derive(Parser)]
 pub struct Deploy {
 	/// Disable automatic rollback
modifiedcmds/fleet/src/cmds/info.rsdiffbeforeafterboth
before · cmds/fleet/src/cmds/info.rs
1use std::collections::BTreeSet;23use crate::host::Config;4use anyhow::{ensure, Result};5use clap::Parser;6use nix_eval::nix_go_json;78#[derive(Parser)]9pub struct Info {10	#[clap(long)]11	json: bool,12	#[clap(subcommand)]13	cmd: InfoCmd,14}1516#[derive(Parser)]17pub enum InfoCmd {18	/// List hosts19	ListHosts {20		#[clap(long)]21		tagged: Vec<String>,22	},23	/// List ips24	HostIps {25		host: String,26		#[clap(long)]27		external: bool,28		#[clap(long)]29		internal: bool,30	},31}3233impl Info {34	pub async fn run(self, config: &Config) -> Result<()> {35		let mut data = Vec::new();36		match self.cmd {37			InfoCmd::ListHosts { ref tagged } => {38				'host: for host in config.list_hosts().await? {39					if !tagged.is_empty() {40						let config = &config.config_unchecked_field;41						let tags: Vec<String> =42							nix_go_json!(config.hosts[{ host.name }].nixosSystem.config.tags);43						for tag in tagged {44							if !tags.contains(tag) {45								continue 'host;46							}47						}48					}49					data.push(host.name);50				}51			}52			InfoCmd::HostIps {53				host,54				external,55				internal,56			} => {57				ensure!(58					external || internal,59					"at leas one of --external or --internal must be set"60				);61				let mut out = <BTreeSet<String>>::new();62				let host = config.system_config(&host).await?;63				if external {64					let data: Vec<String> = nix_go_json!(host.network.externalIps);65					out.extend(data);66				}67				if internal {68					let data: Vec<String> = nix_go_json!(host.network.internalIps);69					out.extend(data);70				}71				for ip in out {72					data.push(ip);73				}74			}75		}7677		if self.json {78			let v = serde_json::to_string_pretty(&data)?;79			print!("{}", v);80		} else {81			for v in data {82				println!("{}", v);83			}84		}85		Ok(())86	}87}
after · cmds/fleet/src/cmds/info.rs
1use std::collections::BTreeSet;23use anyhow::{ensure, Result};4use clap::Parser;5use nix_eval::nix_go_json;67use crate::host::Config;89#[derive(Parser)]10pub struct Info {11	#[clap(long)]12	json: bool,13	#[clap(subcommand)]14	cmd: InfoCmd,15}1617#[derive(Parser)]18pub enum InfoCmd {19	/// List hosts20	ListHosts {21		#[clap(long)]22		tagged: Vec<String>,23	},24	/// List ips25	HostIps {26		host: String,27		#[clap(long)]28		external: bool,29		#[clap(long)]30		internal: bool,31	},32}3334impl Info {35	pub async fn run(self, config: &Config) -> Result<()> {36		let mut data = Vec::new();37		match self.cmd {38			InfoCmd::ListHosts { ref tagged } => {39				'host: for host in config.list_hosts().await? {40					if !tagged.is_empty() {41						let config = &config.config_unchecked_field;42						let tags: Vec<String> =43							nix_go_json!(config.hosts[{ host.name }].nixosSystem.config.tags);44						for tag in tagged {45							if !tags.contains(tag) {46								continue 'host;47							}48						}49					}50					data.push(host.name);51				}52			}53			InfoCmd::HostIps {54				host,55				external,56				internal,57			} => {58				ensure!(59					external || internal,60					"at leas one of --external or --internal must be set"61				);62				let mut out = <BTreeSet<String>>::new();63				let host = config.system_config(&host).await?;64				if external {65					let data: Vec<String> = nix_go_json!(host.network.externalIps);66					out.extend(data);67				}68				if internal {69					let data: Vec<String> = nix_go_json!(host.network.internalIps);70					out.extend(data);71				}72				for ip in out {73					data.push(ip);74				}75			}76		}7778		if self.json {79			let v = serde_json::to_string_pretty(&data)?;80			print!("{}", v);81		} else {82			for v in data {83				println!("{}", v);84			}85		}86		Ok(())87	}88}
modifiedcmds/fleet/src/cmds/mod.rsdiffbeforeafterboth
--- a/cmds/fleet/src/cmds/mod.rs
+++ b/cmds/fleet/src/cmds/mod.rs
@@ -1,4 +1,4 @@
 pub mod build_systems;
+pub mod complete;
 pub mod info;
 pub mod secrets;
-pub mod complete;
modifiedcmds/fleet/src/extra_args.rsdiffbeforeafterboth
--- a/cmds/fleet/src/extra_args.rs
+++ b/cmds/fleet/src/extra_args.rs
@@ -1,7 +1,7 @@
-use anyhow::anyhow;
-use anyhow::Result;
 use std::ffi::{OsStr, OsString};
 
+use anyhow::{anyhow, Result};
+
 pub fn parse_os(os: &OsStr) -> Result<Vec<OsString>> {
 	Ok(shlex::bytes::split(os.as_encoded_bytes())
 		.ok_or_else(|| anyhow!("invalid arguments"))?
modifiedcmds/fleet/src/keys.rsdiffbeforeafterboth
--- a/cmds/fleet/src/keys.rs
+++ b/cmds/fleet/src/keys.rs
@@ -1,12 +1,13 @@
 use std::str::FromStr;
 
-use crate::host::Config;
 use age::Recipient;
 use anyhow::{anyhow, Result};
 use futures::{StreamExt, TryStreamExt};
 use itertools::Itertools;
 use tracing::warn;
 
+use crate::host::Config;
+
 impl Config {
 	pub fn cached_key(&self, host: &str) -> Option<String> {
 		let data = self.data();
modifiedcmds/fleet/src/main.rsdiffbeforeafterboth
--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -175,10 +175,20 @@
 	reg.init();
 }
 
-#[tokio::main]
-async fn main() -> ExitCode {
+fn main() -> ExitCode {
+	let opts = RootOpts::parse();
+	if let Opts::Complete(c) = &opts.command {
+		c.run(RootOpts::command());
+		return ExitCode::SUCCESS;
+	}
+
 	setup_logging();
-	if let Err(e) = main_real().await {
+	async_main(opts)
+}
+
+#[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")]
@@ -189,14 +199,13 @@
 	ExitCode::SUCCESS
 }
 
-async fn main_real() -> Result<()> {
+async fn main_real(opts: RootOpts) -> Result<()> {
 	nix_eval::init_tokio();
 
 	let nix_args = std::env::var_os("NIX_ARGS")
 		.map(|a| extra_args::parse_os(&a))
 		.transpose()?
 		.unwrap_or_default();
-	let opts = RootOpts::parse();
 	let config = opts.fleet_opts.build(nix_args).await?;
 
 	match run_command(&config, opts.command).await {
modifiedflake.lockdiffbeforeafterboth
--- a/flake.lock
+++ b/flake.lock
@@ -7,11 +7,11 @@
         ]
       },
       "locked": {
-        "lastModified": 1717025063,
-        "narHash": "sha256-dIubLa56W9sNNz0e8jGxrX3CAkPXsq7snuFA/Ie6dn8=",
+        "lastModified": 1717290123,
+        "narHash": "sha256-K8O2KQEbA+NIAc8BDsWV6QKqU3i9M+YTUi4zzmLRy1s=",
         "owner": "ipetkov",
         "repo": "crane",
-        "rev": "480dff0be03dac0e51a8dfc26e882b0d123a450e",
+        "rev": "ae1453ffd0f8f684e863685c317a953317db2b79",
         "type": "github"
       },
       "original": {
@@ -40,11 +40,11 @@
     },
     "nixpkgs": {
       "locked": {
-        "lastModified": 1717282945,
-        "narHash": "sha256-Jrn+/CdB/d2hUqduYQdTwGJYDAdaR5cAdlxnq+yEtXI=",
+        "lastModified": 1717336170,
+        "narHash": "sha256-hkD00+n53WNZ4k8hqIbekl5WGDsmb5urhAuDh5XYjyc=",
         "owner": "nixos",
         "repo": "nixpkgs",
-        "rev": "ab5efd0f3c62dd3b75d21d0de1dd63efc76be5d8",
+        "rev": "73bff846b4e8d0c8156c6fc726bf623fe3f3845c",
         "type": "github"
       },
       "original": {
@@ -56,11 +56,11 @@
     },
     "nixpkgs-stable-for-tests": {
       "locked": {
-        "lastModified": 1716991068,
-        "narHash": "sha256-Av0UWCCiIGJxsZ6TFc+OiKCJNqwoxMNVYDBChmhjNpo=",
+        "lastModified": 1717159533,
+        "narHash": "sha256-oamiKNfr2MS6yH64rUn99mIZjc45nGJlj9eGth/3Xuw=",
         "owner": "nixos",
         "repo": "nixpkgs",
-        "rev": "25cf937a30bf0801447f6bf544fc7486c6309234",
+        "rev": "a62e6edd6d5e1fa0329b8653c801147986f8d446",
         "type": "github"
       },
       "original": {
@@ -89,11 +89,11 @@
         ]
       },
       "locked": {
-        "lastModified": 1717208326,
-        "narHash": "sha256-4gVhbC+NjSQ4c6cJvJGNCI1oTcD+8jRRNAnOF9faGCE=",
+        "lastModified": 1717294752,
+        "narHash": "sha256-QhlS52cEQyx+iVcgrEoCnEEpWUA6uLdmeLRxk935inI=",
         "owner": "oxalica",
         "repo": "rust-overlay",
-        "rev": "ab69b67fac9a96709fbef0b899db308ca714a120",
+        "rev": "b46857a406d207a1de74e792ef3b83e800c30e08",
         "type": "github"
       },
       "original": {
modifiedpkgs/fleet.nixdiffbeforeafterboth
--- a/pkgs/fleet.nix
+++ b/pkgs/fleet.nix
@@ -1,4 +1,7 @@
-{craneLib}:
+{
+  craneLib,
+  installShellFiles,
+}:
 craneLib.buildPackage rec {
   pname = "fleet";
 
@@ -7,10 +10,12 @@
 
   cargoExtraArgs = "--locked -p ${pname}";
 
+  nativeBuildInputs = [installShellFiles];
+
   postInstall = ''
     for shell in bash fish zsh; do
       installShellCompletion --cmd fleet \
-        --$shell <($out/bin/fleet complete --shell $shell --print)
+        --$shell <($out/bin/fleet complete --shell $shell)
     done
   '';
 }