Skip to content

Commit e271cc4

Browse files
committed
fix: stop - only stop running containers; add: list - improve display; fix: always store absolute path for local configs; chore: deps
1 parent 38be074 commit e271cc4

File tree

6 files changed

+111
-16
lines changed

6 files changed

+111
-16
lines changed

Cargo.lock

+60-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rooz"
3-
version = "0.98.0"
3+
version = "0.99.0"
44
edition = "2021"
55

66
[dependencies]
@@ -24,6 +24,7 @@ regex = "1.10.6"
2424
serde = "1.0.209"
2525
serde_yaml = "0.9.34"
2626
shellexpand = "3.1.0"
27+
tabled = "0.16.0"
2728
termion = "4.0.2"
2829
tokio = { version = "1.39.3", features = ["rt-multi-thread", "macros"] }
2930
toml = "0.8.19"

src/api/container.rs

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ impl<'a> ContainerApi<'a> {
5252
Ok(self.client.list_containers(Some(list_options)).await?)
5353
}
5454

55+
pub async fn get_running(&self, labels: &Labels) -> Result<Vec<ContainerSummary>, AnyError> {
56+
let list_options = ListContainersOptions {
57+
filters: labels.into(),
58+
all: false,
59+
..Default::default()
60+
};
61+
62+
Ok(self.client.list_containers(Some(list_options)).await?)
63+
}
64+
5565
pub async fn get_single(&self, labels: &Labels) -> Result<Option<ContainerSummary>, AnyError> {
5666
match self.get_all(&labels).await?.as_slice() {
5767
[] => Ok(None),

src/api/workspace/stop.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{api::WorkspaceApi, model::types::AnyError, util::labels::Labels};
55
impl<'a> WorkspaceApi<'a> {
66
pub async fn stop(&self, workspace_key: &str) -> Result<(), AnyError> {
77
let labels = Labels::new(Some(workspace_key), None);
8-
for c in self.api.container.get_all(&labels).await? {
8+
for c in self.api.container.get_running(&labels).await? {
99
print!("Stopping container: {} ... ", c.names.unwrap().join(", "));
1010
self.api.container.stop(&c.id.unwrap()).await?;
1111
println!("{}", format!("OK").green())
@@ -15,8 +15,10 @@ impl<'a> WorkspaceApi<'a> {
1515

1616
pub async fn stop_all(&self) -> Result<(), AnyError> {
1717
let labels = Labels::default();
18-
for c in self.api.container.get_all(&labels).await? {
18+
for c in self.api.container.get_running(&labels).await? {
19+
print!("Stopping container: {} ... ", c.names.unwrap().join(", "));
1920
self.api.container.stop(&c.id.unwrap()).await?;
21+
println!("{}", format!("OK").green())
2022
}
2123
Ok(())
2224
}

src/cmd/list.rs

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
use crate::{
22
model::types::AnyError,
3-
util::labels::{self, Labels},
3+
util::labels::{self, Labels, CONFIG_ORIGIN},
44
};
55

66
use bollard::{container::ListContainersOptions, service::ContainerSummary, Docker};
77

8+
use tabled::{settings::Style, Table, Tabled};
9+
10+
#[derive(Debug, Tabled)]
11+
struct WorkspaceView {
12+
#[tabled(rename = "WORKSPACE")]
13+
name: String,
14+
#[tabled(rename = "RUNNING", format("{}", if self.running {"true"} else {""}))]
15+
running: bool,
16+
#[tabled(rename = "CONFIG")]
17+
origin: String,
18+
}
19+
820
pub async fn list(docker: &Docker) -> Result<(), AnyError> {
921
let labels = Labels::new(None, Some(labels::ROLE_WORK));
1022
let list_options = ListContainersOptions {
@@ -14,7 +26,8 @@ pub async fn list(docker: &Docker) -> Result<(), AnyError> {
1426
};
1527

1628
let container_summary = docker.list_containers(Some(list_options)).await?;
17-
println!("WORKSPACE");
29+
30+
let mut views = Vec::<WorkspaceView>::new();
1831

1932
for c in container_summary {
2033
if let ContainerSummary {
@@ -23,12 +36,25 @@ pub async fn list(docker: &Docker) -> Result<(), AnyError> {
2336
..
2437
} = c
2538
{
26-
let state_icon = match state.as_str() {
27-
"running" => "✱",
28-
_ => "",
39+
let is_running = match state.as_str() {
40+
"running" => true,
41+
_ => false,
2942
};
30-
println!("{} {}", labels[labels::WORKSPACE_KEY], state_icon);
43+
views.push(WorkspaceView {
44+
name: c.names.unwrap().join(", ")[1..].to_string(),
45+
running: is_running,
46+
origin: labels
47+
.get(CONFIG_ORIGIN)
48+
.unwrap_or(&"cli".to_string())
49+
.to_string(),
50+
});
3151
}
3252
}
53+
54+
views.sort_by(|a, b| a.name.cmp(&b.name));
55+
56+
let table = Table::new(views).with(Style::blank()).to_string();
57+
58+
println!("{}", table);
3359
Ok(())
3460
}

src/cmd/new.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ impl<'a> WorkspaceApi<'a> {
154154
ConfigSource::Path { value: path } => match path {
155155
ConfigPath::File { path } => {
156156
let body = fs::read_to_string(&path)?;
157+
let absolute_path =
158+
std::path::absolute(path)?.to_string_lossy().into_owned();
157159
labels = Labels {
158-
config_source: Labels::config_origin(&path),
160+
config_source: Labels::config_origin(&absolute_path),
159161
config_body: Labels::config_body(&body),
160162
..labels
161163
};

0 commit comments

Comments
 (0)