Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ attribute set."#
#[cfg(debug_assertions)]
#[command(about = "Run progress spinner tests", hide = true)]
TestProgress,
List(command::list::Opts),
#[command(about = "Generate shell auto-completion files (Internal)", hide = true)]
GenCompletions {
shell: Shell,
Expand Down Expand Up @@ -331,6 +332,7 @@ pub async fn run() {
};
r(command::apply::run(hive, args), opts.config).await
}
Command::List(args) => r(command::list::run(hive, args), opts.config).await,
Command::GenCompletions { .. } => unreachable!(),
}
}
Expand Down
95 changes: 95 additions & 0 deletions src/command/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use itertools::Itertools;

use crate::{
error::ColmenaError,
nix::{node_filter::NodeFilterOpts, Hive},
};
use clap::Args;
use serde::Serialize;

#[derive(Debug, Args)]
#[command(name = "list", about = "List nodes in the Hive")]
pub struct Opts {
#[arg(long, help = "Output node list in JSON format")]
json: bool,

#[command(flatten)]
node_filter: NodeFilterOpts,
}

#[derive(Debug, Serialize)]
struct Row {
name: String,
ssh_host: Option<String>,
tags: Vec<String>,
}

impl Row {
fn table_tags(&self) -> String {
format!("[{}]", self.tags.join(", "))
}

fn table_ssh_host(&self) -> String {
self.ssh_host
.clone()
.unwrap_or_else(|| "[no host]".to_owned())
}

fn table_name(&self) -> String {
self.name.clone()
}
}

pub async fn run(hive: Hive, opts: Opts) -> Result<(), ColmenaError> {
let targets = hive
.select_nodes(opts.node_filter.on.clone(), None, false)
.await?;

let rows = targets
.values()
.sorted_by(|node1, node2| node1.name().cmp(&node2.name()))
.map(|node| Row {
name: node.name().to_owned(),
ssh_host: node
.config()
.to_ssh_host()
.map(|ssh_host| ssh_host.ssh_target()),
tags: node.config().tags().to_vec(),
})
.collect::<Vec<_>>();

if opts.json {
println!(
"{}",
serde_json::to_string_pretty(&rows).expect("Failed to serialize nodes")
);
return Ok(());
} else {
let max_name_len = rows
.iter()
.map(|row| row.table_name().len())
.max()
.unwrap_or(0);
let max_ssh_host_len = rows
.iter()
.map(|row| row.table_ssh_host().len())
.max()
.unwrap_or(0);
let max_tags_len = rows
.iter()
.map(|row| row.table_tags().len())
.max()
.unwrap_or(0);

for row in rows {
println!(
"{:max_name_len$} {:max_ssh_host_len$} {:max_tags_len$}",
row.table_name(),
row.table_ssh_host(),
row.table_tags(),
);
}
}

Ok(())
}
1 change: 1 addition & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod apply;
pub mod eval;
pub mod exec;
pub mod list;
pub mod nix_info;
pub mod repl;

Expand Down
8 changes: 8 additions & 0 deletions src/nix/deployment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ impl TargetNode {
pub fn into_host(self) -> Option<Box<dyn Host>> {
self.host
}

pub fn name(&self) -> &str {
&self.name.0
}

pub fn config(&self) -> &NodeConfig {
&self.config
}
}

impl Deployment {
Expand Down
2 changes: 1 addition & 1 deletion src/nix/host/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Ssh {
execution.run().await
}

fn ssh_target(&self) -> String {
pub(crate) fn ssh_target(&self) -> String {
match &self.user {
Some(n) => format!("{}@{}", n, self.host),
None => self.host.clone(),
Expand Down