1use 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 18 ListHosts {19 #[structopt(long)]20 tagged: Vec<String>,21 },22 23 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}