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

difftreelog

feat info subcommand

Yaroslav Bolyukin2021-10-01parent: #5fd92ba.patch.diff
in: trunk

4 files changed

addedcmds/fleet/src/cmds/info.rsdiffbeforeafterboth
--- /dev/null
+++ b/cmds/fleet/src/cmds/info.rs
@@ -0,0 +1,81 @@
+use std::collections::BTreeSet;
+
+use crate::host::Config;
+use anyhow::{ensure, Result};
+use structopt::StructOpt;
+
+#[derive(StructOpt)]
+pub struct Info {
+	#[structopt(long)]
+	json: bool,
+	#[structopt(subcommand)]
+	cmd: InfoCmd,
+}
+
+#[derive(StructOpt)]
+pub enum InfoCmd {
+	/// List hosts
+	ListHosts {
+		#[structopt(long)]
+		tagged: Vec<String>,
+	},
+	/// List ips
+	HostIps {
+		host: String,
+		#[structopt(long)]
+		external: bool,
+		#[structopt(long)]
+		internal: bool,
+	},
+}
+
+impl Info {
+	pub fn run(self, config: &Config) -> Result<()> {
+		let mut data = Vec::new();
+		match self.cmd {
+			InfoCmd::ListHosts { ref tagged } => {
+				'host: for host in config.list_hosts()? {
+					if !tagged.is_empty() {
+						let tags: Vec<String> = config.config_attr(&host, "tags")?;
+						for tag in tagged {
+							if !tags.contains(&tag) {
+								continue 'host;
+							}
+						}
+					}
+					data.push(host);
+				}
+			}
+			InfoCmd::HostIps {
+				host,
+				external,
+				internal,
+			} => {
+				ensure!(
+					external || internal,
+					"at leas one of --external or --internal must be set"
+				);
+				let mut out = <BTreeSet<String>>::new();
+				if external {
+					out.extend(config.config_attr::<Vec<String>>(&host, "network.externalIps")?);
+				}
+				if internal {
+					out.extend(config.config_attr::<Vec<String>>(&host, "network.internalIps")?);
+				}
+				for ip in out {
+					data.push(ip);
+				}
+			}
+		}
+
+		if self.json {
+			let v = serde_json::to_string_pretty(&data)?;
+			print!("{}", v);
+		} else {
+			for v in data {
+				println!("{}", v);
+			}
+		}
+		Ok(())
+	}
+}
modifiedcmds/fleet/src/cmds/mod.rsdiffbeforeafterboth
--- a/cmds/fleet/src/cmds/mod.rs
+++ b/cmds/fleet/src/cmds/mod.rs
@@ -1,2 +1,3 @@
 pub mod build_systems;
 pub mod secrets;
+pub mod info;
modifiedcmds/fleet/src/host.rsdiffbeforeafterboth
9};9};
1010
11use anyhow::Result;11use anyhow::Result;
12use serde::de::DeserializeOwned;
12use structopt::clap::ArgGroup;13use structopt::clap::ArgGroup;
13use structopt::StructOpt;14use structopt::StructOpt;
1415
80 .inherit_stdio()81 .inherit_stdio()
81 .run_json()82 .run_json()
82 }83 }
84 pub fn config_attr<T: DeserializeOwned>(&self, host: &str, attr: &str) -> Result<T> {
85 Command::new("nix")
86 .arg("eval")
87 .arg(self.full_attr_name(&format!(
88 "fleetConfigurations.default.configuredSystems.{}.config.{}",
89 host, attr
90 )))
91 .args(&["--json", "--show-trace"])
92 .inherit_stdio()
93 .run_json()
94 }
8395
84 pub fn data(&self) -> Ref<FleetData> {96 pub fn data(&self) -> Ref<FleetData> {
85 self.data.borrow()97 self.data.borrow()
modifiedcmds/fleet/src/main.rsdiffbeforeafterboth
--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -11,7 +11,7 @@
 use structopt::clap::AppSettings::*;
 use structopt::StructOpt;
 
-use cmds::{build_systems::BuildSystems, secrets::Secrets};
+use cmds::{build_systems::BuildSystems, info::Info, secrets::Secrets};
 use host::{Config, FleetOpts};
 
 #[derive(StructOpt)]
@@ -20,6 +20,8 @@
 	BuildSystems(BuildSystems),
 	/// Secret management
 	Secrets(Secrets),
+	/// Config parsing
+	Info(Info),
 }
 
 #[derive(StructOpt)]
@@ -40,6 +42,7 @@
 	match command {
 		Opts::BuildSystems(c) => c.run(config)?,
 		Opts::Secrets(s) => s.run(config)?,
+		Opts::Info(i) => i.run(config)?,
 	};
 	Ok(())
 }