difftreelog
feat info subcommand
in: trunk
4 files changed
cmds/fleet/src/cmds/info.rsdiffbeforeafterboth1use std::collections::BTreeSet;23use crate::host::Config;4use anyhow::{ensure, Result};5use structopt::StructOpt;67#[derive(StructOpt)]8pub struct Info {9 #[structopt(long)]10 json: bool,11 #[structopt(subcommand)]12 cmd: InfoCmd,13}1415#[derive(StructOpt)]16pub enum InfoCmd {17 /// List hosts18 ListHosts {19 #[structopt(long)]20 tagged: Vec<String>,21 },22 /// List ips23 HostIps {24 host: String,25 #[structopt(long)]26 external: bool,27 #[structopt(long)]28 internal: bool,29 },30}3132impl Info {33 pub fn run(self, config: &Config) -> Result<()> {34 let mut data = Vec::new();35 match self.cmd {36 InfoCmd::ListHosts { ref tagged } => {37 'host: for host in config.list_hosts()? {38 if !tagged.is_empty() {39 let tags: Vec<String> = config.config_attr(&host, "tags")?;40 for tag in tagged {41 if !tags.contains(&tag) {42 continue 'host;43 }44 }45 }46 data.push(host);47 }48 }49 InfoCmd::HostIps {50 host,51 external,52 internal,53 } => {54 ensure!(55 external || internal,56 "at leas one of --external or --internal must be set"57 );58 let mut out = <BTreeSet<String>>::new();59 if external {60 out.extend(config.config_attr::<Vec<String>>(&host, "network.externalIps")?);61 }62 if internal {63 out.extend(config.config_attr::<Vec<String>>(&host, "network.internalIps")?);64 }65 for ip in out {66 data.push(ip);67 }68 }69 }7071 if self.json {72 let v = serde_json::to_string_pretty(&data)?;73 print!("{}", v);74 } else {75 for v in data {76 println!("{}", v);77 }78 }79 Ok(())80 }81}cmds/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;
cmds/fleet/src/host.rsdiffbeforeafterboth--- a/cmds/fleet/src/host.rs
+++ b/cmds/fleet/src/host.rs
@@ -9,6 +9,7 @@
};
use anyhow::Result;
+use serde::de::DeserializeOwned;
use structopt::clap::ArgGroup;
use structopt::StructOpt;
@@ -80,6 +81,17 @@
.inherit_stdio()
.run_json()
}
+ pub fn config_attr<T: DeserializeOwned>(&self, host: &str, attr: &str) -> Result<T> {
+ Command::new("nix")
+ .arg("eval")
+ .arg(self.full_attr_name(&format!(
+ "fleetConfigurations.default.configuredSystems.{}.config.{}",
+ host, attr
+ )))
+ .args(&["--json", "--show-trace"])
+ .inherit_stdio()
+ .run_json()
+ }
pub fn data(&self) -> Ref<FleetData> {
self.data.borrow()
cmds/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(())
}