use std::path::Path;
use anyhow::{Context, Result};
use toml::Value;
use crate::commands::status::Envelope;
use crate::config::Config;
use crate::render::{bold_cyan, dim, green, magenta, yellow};
const HEADERS: [&str; 5] = ["NAME", "SOURCE", "FOLDER", "SERVICES", "EXTENDS"];
pub fn ls(dir: &Path, json: bool) -> Result<()> {
let config = Config::load(dir)?;
if json {
let acked = crate::commands::services::project_id(dir)
.ok()
.and_then(|p| crate::state::State::load().ok()?.identity_acks.remove(&p))
.unwrap_or_default();
let (rows, _errors) = crate::snapshot::sandbox_rows(dir, &config, &acked);
println!("{}", render_json(&rows)?);
return Ok(());
}
let sandboxes = config.resolve_all()?;
if sandboxes.is_empty() {
println!("{}", none_defined(&crate::config::display_path(dir)));
return Ok(());
}
let mut lines = Vec::with_capacity(sandboxes.len());
for sandbox in &sandboxes {
for issue in crate::features::feature_issues(dir, sandbox.properties.features.as_ref()) {
eprintln!("warning: sandbox `{}`: {issue}", sandbox.name);
}
lines.push(Line {
name: sandbox.name.clone(),
source: sandbox.source(),
folder: sandbox.folder().map(str::to_string),
services: sandbox
.properties
.services
.as_ref()
.filter(|s| !s.is_empty())
.map(|s| s.join(", ")),
extends: config
.sandboxes
.get(&sandbox.name)
.and_then(|table| table.get("extends"))
.and_then(extends_names),
});
}
print!("{}", table(&lines));
Ok(())
}
pub(crate) fn render_json(rows: &[crate::snapshot::SandboxRow]) -> Result<String> {
serde_json::to_string_pretty(&Envelope::new(rows)).context("serialize sandboxes")
}
pub(crate) fn none_defined(dir: &Path) -> String {
format!("no sandboxes defined in {}", dir.display())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Line {
pub name: String,
pub source: String,
pub folder: Option<String>,
pub services: Option<String>,
pub extends: Option<String>,
}
impl Line {
pub(crate) fn of_row(row: &crate::snapshot::SandboxRow) -> Line {
let joined = |v: &[String]| (!v.is_empty()).then(|| v.join(", "));
Line {
name: row.name.clone(),
source: row.source.clone(),
folder: row.folder.clone(),
services: joined(&row.services),
extends: joined(&row.extends),
}
}
}
pub(crate) fn table(lines: &[Line]) -> String {
let dash = |v: &Option<String>| v.clone().unwrap_or_else(|| "-".to_string());
let plain: Vec<[String; 5]> = lines
.iter()
.map(|l| [l.name.clone(), l.source.clone(), dash(&l.folder), dash(&l.services), dash(&l.extends)])
.collect();
let colored: Vec<[String; 5]> = lines
.iter()
.map(|l| {
[
bold_cyan(&l.name),
color_source(&l.source),
l.folder.clone().unwrap_or_else(|| dim("-")),
l.services.as_deref().map(magenta).unwrap_or_else(|| dim("-")),
l.extends.clone().unwrap_or_else(|| dim("-")),
]
})
.collect();
let widths = column_widths(&HEADERS, &plain);
let mut out = format_row(&HEADERS.map(dim), &plain_headers(), &widths);
for (colored_row, plain_row) in colored.iter().zip(&plain) {
out.push_str(&format_row(colored_row, plain_row, &widths));
}
out
}
fn plain_headers() -> [String; 5] {
HEADERS.map(|h| h.to_string())
}
fn color_source(source: &str) -> String {
if source.starts_with("image ") {
green(source)
} else if source.starts_with("dockerfile ") {
yellow(source)
} else {
source.to_string()
}
}
fn extends_names(value: &Value) -> Option<String> {
match value {
Value::String(s) => Some(s.clone()),
Value::Array(items) => {
let names: Vec<&str> = items.iter().filter_map(Value::as_str).collect();
if names.is_empty() {
None
} else {
Some(names.join(", "))
}
}
_ => None,
}
}
fn column_widths(headers: &[&str; 5], rows: &[[String; 5]]) -> [usize; 5] {
let mut widths = headers.map(str::len);
for row in rows {
for (i, cell) in row.iter().enumerate() {
widths[i] = widths[i].max(cell.len());
}
}
widths
}
fn format_row(colored: &[String; 5], plain: &[String; 5], widths: &[usize; 5]) -> String {
let mut out = String::new();
for i in 0..5 {
if i > 0 {
out.push_str(" ");
}
out.push_str(&colored[i]);
if i < 4 {
let pad = widths[i] - plain[i].len();
out.extend(std::iter::repeat(' ').take(pad));
}
}
out.push('\n');
out
}
#[cfg(test)]
mod tests {
use super::*;
fn row(cells: [&str; 5]) -> [String; 5] {
cells.map(str::to_string)
}
#[test]
fn widths_take_widest_cell_including_header() {
let rows = [
row(["alpha", "image x", "-", "-", "-"]),
row(["a", "dockerfile very/long/path", "web", "db", "base"]),
];
let widths = column_widths(&HEADERS, &rows);
assert_eq!(widths[0], "alpha".len()); assert_eq!(widths[1], "dockerfile very/long/path".len());
assert_eq!(widths[2], "FOLDER".len()); assert_eq!(widths[3], "SERVICES".len());
assert_eq!(widths[4], "EXTENDS".len());
}
#[test]
fn row_padding_uses_two_space_gutter_and_no_trailing_pad() {
let widths = [5usize, 7, 6, 8, 7];
let cells = row(["a", "image x", "-", "-", "base"]);
let line = format_row(&cells, &cells, &widths);
assert_eq!(line, "a image x - - base\n");
assert!(!line.trim_end_matches('\n').ends_with(' '));
}
#[test]
fn padding_uses_plain_length_not_colored_length() {
let widths = column_widths(&HEADERS, &[row(["name", "image x", "f", "svc", "ext"])]);
let plain = row(["name", "image x", "f", "svc", "ext"]);
let colored = [
"\x1b[1;36mname\x1b[0m".to_string(),
"\x1b[32mimage x\x1b[0m".to_string(),
"f".to_string(),
"\x1b[35msvc\x1b[0m".to_string(),
"ext".to_string(),
];
let colored_line = format_row(&colored, &plain, &widths);
let plain_line = format_row(&plain, &plain, &widths);
let stripped = strip_ansi(&colored_line);
assert_eq!(stripped, plain_line);
}
#[test]
fn extends_string_and_array() {
assert_eq!(
extends_names(&Value::String("base".into())),
Some("base".to_string())
);
let arr = Value::Array(vec![Value::String("a".into()), Value::String("b".into())]);
assert_eq!(extends_names(&arr), Some("a, b".to_string()));
assert_eq!(extends_names(&Value::Array(vec![])), None);
assert_eq!(extends_names(&Value::Integer(1)), None);
}
fn strip_ansi(s: &str) -> String {
let mut out = String::new();
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '\x1b' {
for c in chars.by_ref() {
if c == 'm' {
break;
}
}
} else {
out.push(c);
}
}
out
}
}