Skip to content

Commit c7e02cf

Browse files
committed
feat: prettify stdout
1 parent 46a2a3f commit c7e02cf

3 files changed

Lines changed: 146 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vectorctl-migration/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ once_cell = "1.21"
2525
rustc-hash = "2.0"
2626
futures = "0.3"
2727
tinyvec = "*"
28+
owo-colors = "4.2.3"
29+
atty = "0.2.14"
2830

2931
[features]
3032
default = ["qdrant-backend"]

vectorctl-migration/src/migrator.rs

Lines changed: 94 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use crate::{
22
ContextError, MigrationTrait,
33
revision::{Node, RevisionGraph, RevisionGraphError},
44
};
5+
use atty::Stream;
56
use once_cell::sync::OnceCell;
7+
use owo_colors::OwoColorize;
68
use std::collections::HashMap;
79
use thiserror::Error;
810
use uuid::Uuid;
@@ -95,15 +97,52 @@ pub trait MigratorTrait: Send {
9597
ledger.ensure().await?;
9698

9799
let graph = Self::build_graph(&ledger.retrieve().await?)?;
100+
101+
let use_colors = atty::is(Stream::Stdout) && std::env::var_os("NO_COLOR").is_none();
102+
98103
graph
99104
.forward_path(Some(graph.head()), graph.queue())
100105
.into_iter()
101106
.for_each(|Node { migration, .. }| {
102-
println!(
103-
"Migration `{}`, status: `{}`",
104-
migration.runner.name(),
105-
migration.status,
106-
)
107+
let status_str = match migration.status {
108+
MigrationStatus::Applied => {
109+
let text = "Applied";
110+
if use_colors {
111+
text.green().bold().to_string()
112+
} else {
113+
text.to_string()
114+
}
115+
}
116+
MigrationStatus::Pending => {
117+
let text = "Pending";
118+
if use_colors {
119+
text.yellow().bold().to_string()
120+
} else {
121+
text.to_string()
122+
}
123+
}
124+
};
125+
126+
let message = migration
127+
.runner
128+
.revision()
129+
.message
130+
.map(|message| {
131+
if use_colors {
132+
format!(" — {}", message.dimmed().italic())
133+
} else {
134+
format!(" — {}", message)
135+
}
136+
})
137+
.unwrap_or_default();
138+
139+
let name = if use_colors {
140+
migration.runner.name().blue().bold().to_string()
141+
} else {
142+
migration.runner.name().to_string()
143+
};
144+
145+
println!("{:<20} | {}{}", name, status_str, message);
107146
});
108147

109148
Ok(())
@@ -187,14 +226,32 @@ where
187226
let ledger = ctx.backend.ledger();
188227
ledger.ensure().await?;
189228

190-
let ids = futures::future::try_join_all(iterator.map(|(id_opt, migration)| async move {
191-
let id = id_opt.ok_or_else(|| {
192-
MigrationError::Graph(RevisionGraphError::NotFound(format!(
193-
"{:?}",
194-
migration.name()
195-
)))
196-
})?;
197-
migration.down(ctx).await.map(|_| id)
229+
let use_colors = atty::is(Stream::Stdout) && std::env::var_os("NO_COLOR").is_none();
230+
231+
let ids = futures::future::try_join_all(iterator.map(|(id_opt, migration)| {
232+
let name = migration.name();
233+
async move {
234+
let message = format!("Running down: {}", name);
235+
if use_colors {
236+
println!("{}", message.yellow().bold());
237+
} else {
238+
println!("{message}");
239+
}
240+
241+
let id = id_opt.ok_or_else(|| {
242+
MigrationError::Graph(RevisionGraphError::NotFound(format!("{:?}", name)))
243+
})?;
244+
245+
migration.down(ctx).await.map(|_| {
246+
let message = format!("Rolled back: {}", name);
247+
if use_colors {
248+
println!("{}", message.green().bold());
249+
} else {
250+
println!("{message}");
251+
}
252+
id
253+
})
254+
}
198255
}))
199256
.await?;
200257

@@ -212,11 +269,30 @@ where
212269
let ledger = ctx.backend.ledger();
213270
ledger.ensure().await?;
214271

215-
let ids =
216-
futures::future::try_join_all(iterator.map(|(_, migration)| async move {
217-
migration.up(ctx).await.map(|_| migration.name())
218-
}))
219-
.await?;
272+
let use_colors = atty::is(Stream::Stdout) && std::env::var_os("NO_COLOR").is_none();
273+
274+
let ids = futures::future::try_join_all(iterator.map(|(_, migration)| {
275+
let name = migration.name();
276+
async move {
277+
let message = format!("Applying: {}", name);
278+
if use_colors {
279+
println!("{}", message.yellow().bold());
280+
} else {
281+
println!("{message}");
282+
}
283+
284+
migration.up(ctx).await.map(|_| {
285+
let message = format!("Applied: {}", name);
286+
if use_colors {
287+
println!("{}", message.green().bold());
288+
} else {
289+
println!("{message}");
290+
}
291+
name
292+
})
293+
}
294+
}))
295+
.await?;
220296

221297
if !ids.is_empty() {
222298
ledger.insert_many(ids).await?;

0 commit comments

Comments
 (0)