Skip to content

Commit e4156e3

Browse files
committed
CLI-1264: add ok and err
1 parent 98a6774 commit e4156e3

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

Cargo.lock

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

fusecli/cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ path = "src/bin/datafuse-cli.rs"
1919
# Crates.io dependencies
2020
ansi_term = "0.12.1"
2121
clap = "2.33.3"
22+
colored = "2.0.0"
2223
dirs = "3.0.2"
2324
dyn-clone = "1.0.4"
2425
flate2 = "1.0.20"

fusecli/cli/src/cmds/gets/get.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ impl Command for GetCommand {
9292
{
9393
Ok(_matches) => {
9494
let arch = self.get_architecture()?;
95-
writer.writeln("Arch", arch.as_str());
95+
writer.write_ok(format!("Arch {}", arch).as_str());
9696

9797
let latest_tag = self.get_latest_tag()?;
98-
writer.writeln("Tag", latest_tag.as_str());
98+
writer.write_ok(format!("Tag {}", latest_tag).as_str());
9999

100100
// Create download dir.
101101
let bin_download_dir = format!(
@@ -112,7 +112,6 @@ impl Command for GetCommand {
112112

113113
let bin_name = format!("datafuse--{}.tar.gz", arch);
114114
let bin_file = format!("{}/{}", bin_download_dir, bin_name);
115-
writer.writeln("Binary", bin_file.as_str());
116115
let exists = Path::new(bin_file.as_str()).exists();
117116

118117
// Download.
@@ -123,7 +122,6 @@ impl Command for GetCommand {
123122
latest_tag,
124123
bin_name,
125124
);
126-
writer.writeln("Download", binary_url.as_str());
127125
let res = ureq::get(binary_url.as_str()).call()?;
128126
let total_size: u64 = res.header("content-length").unwrap().parse().unwrap();
129127
let pb = ProgressBar::new(total_size);
@@ -133,22 +131,33 @@ impl Command for GetCommand {
133131

134132
let mut out = File::create(bin_file.clone()).unwrap();
135133
io::copy(&mut pb.wrap_read(res.into_reader()), &mut out).unwrap();
134+
writer.write_ok(format!("Download {}", binary_url).as_str());
136135
}
136+
writer.write_ok(format!("Binary {}", bin_file).as_str());
137137

138138
// Unpack.
139139
let tar_gz = File::open(bin_file)?;
140140
let tar = GzDecoder::new(tar_gz);
141141
let mut archive = Archive::new(tar);
142-
writer.writeln("Unpack to", bin_unpack_dir.as_str());
143-
archive.unpack(bin_unpack_dir).unwrap();
142+
let res = archive.unpack(bin_unpack_dir.clone());
143+
match res {
144+
Ok(_) => {
145+
writer.write_ok(format!("Unpack {}", bin_unpack_dir).as_str());
146+
}
147+
Err(e) => {
148+
writer.write_err(format!("{}", e).as_str());
149+
return Ok(());
150+
}
151+
};
144152

145153
// Write status.
146154
let mut status = Status::read(self.conf.clone())?;
147155
status.latest = latest_tag;
148156
status.write()?;
157+
writer.write_ok("Write status");
149158
}
150159
Err(err) => {
151-
writer.writeln_width("", format!("{}", err).as_str(), 0);
160+
writer.write_err(format!("{}", err).as_str());
152161
}
153162
}
154163

fusecli/cli/src/cmds/writer.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use std::io::Stdout;
66
use std::io::Write;
77

8+
use colored::Colorize;
9+
810
enum WriterOutput {
911
Stdout(Stdout),
1012
}
@@ -25,9 +27,12 @@ impl Writer {
2527
writeln!(self, "{:width$} {}", name, value, width = width).unwrap();
2628
}
2729

28-
pub fn writeln_width(&mut self, name: &str, value: &str, width: usize) {
29-
let width = width;
30-
writeln!(self, "{:width$} {}", name, value, width = width).unwrap();
30+
pub fn write_ok(&mut self, msg: &str) {
31+
writeln!(self, "✅ {} {}", "[ok]".bold().green(), msg).unwrap();
32+
}
33+
34+
pub fn write_err(&mut self, msg: &str) {
35+
writeln!(self, "{} {}", "[failed]".bold().red(), msg.red()).unwrap();
3136
}
3237
}
3338

0 commit comments

Comments
 (0)