Skip to content

Commit

Permalink
CLI-938: datafuse-cli package switch
Browse files Browse the repository at this point in the history
  • Loading branch information
BohuTANG committed Aug 6, 2021
1 parent ae34d68 commit ac12dd8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
30 changes: 26 additions & 4 deletions fusecli/cli/src/cmds/packages/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use std::fs;

use colored::Colorize;
use prettytable::Cell;
use prettytable::Row;
use prettytable::Table;

use crate::cmds::Config;
use crate::cmds::Status;
use crate::cmds::Writer;
use crate::error::Result;

Expand All @@ -26,16 +28,36 @@ impl ListCommand {
let bin_dir = format!("{}/bin", self.conf.datafuse_dir.clone());
let paths = fs::read_dir(bin_dir)?;

// Status.
let mut current = "".to_string();
if let Ok(status) = Status::read(self.conf.clone()) {
current = status.version;
}

let mut table = Table::new();
// Title.
table.add_row(Row::new(vec![Cell::new("Version"), Cell::new("Path")]));
table.add_row(Row::new(vec![
Cell::new("Version"),
Cell::new("Path"),
Cell::new("Current"),
]));
for path in paths {
let path = path.unwrap();
let version = path
.path()
.file_name()
.unwrap()
.to_string_lossy()
.into_owned();
let mut row = vec![];
row.push(Cell::new(
path.path().file_name().unwrap().to_str().unwrap(),
));
row.push(Cell::new(version.as_str()));
row.push(Cell::new(format!("{}", path.path().display(),).as_str()));

let mut current_marker = "".to_string();
if current == version {
current_marker = format!("{}", "✅ ".blue());
}
row.push(Cell::new(current_marker.as_str()));
table.add_row(Row::new(row));
}
table.print(writer).unwrap();
Expand Down
9 changes: 7 additions & 2 deletions fusecli/cli/src/cmds/packages/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::cell::RefCell;

use clap::App;
use clap::AppSettings;
use clap::Arg;

use crate::cmds::command::Command;
use crate::cmds::Config;
Expand Down Expand Up @@ -44,7 +45,10 @@ impl PackageCommand {
App::new("switch")
.setting(AppSettings::DisableVersion)
.setting(AppSettings::ColoredHelp)
.about("Switch the active datafuse to a specified version"),
.about("Switch the active datafuse to a specified version")
.arg(Arg::with_name("version").required(true).help(
"Version of datafuse package, e.g. v0.4.69-nightly. Check the versions: package list"
))
),
);
PackageCommand { conf, clap }
Expand Down Expand Up @@ -81,8 +85,9 @@ impl Command for PackageCommand {
list.exec(writer, args)?;
}
Some("switch") => {
let val = matches.subcommand().1.unwrap().value_of("version").unwrap();
let switch = SwitchCommand::create(self.conf.clone());
switch.exec(writer, args)?;
switch.exec(writer, val.to_string())?;
}
_ => writer.write_err("unknown command, usage: package -h"),
},
Expand Down
40 changes: 24 additions & 16 deletions fusecli/cli/src/cmds/packages/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

use std::fs;

use prettytable::Cell;
use prettytable::Row;
use prettytable::Table;

use crate::cmds::Config;
use crate::cmds::ListCommand;
use crate::cmds::Status;
use crate::cmds::Writer;
use crate::error::Result;

Expand All @@ -22,23 +20,33 @@ impl SwitchCommand {
SwitchCommand { conf }
}

pub fn exec(&self, writer: &mut Writer, _args: String) -> Result<()> {
pub fn exec(&self, writer: &mut Writer, args: String) -> Result<()> {
let bin_dir = format!("{}/bin", self.conf.datafuse_dir.clone());
let paths = fs::read_dir(bin_dir)?;

let mut table = Table::new();
// Title.
table.add_row(Row::new(vec![Cell::new("Version"), Cell::new("Path")]));
let mut exists = false;
for path in paths {
let path = path.unwrap();
let mut row = vec![];
row.push(Cell::new(
path.path().file_name().unwrap().to_str().unwrap(),
));
row.push(Cell::new(format!("{}", path.path().display(),).as_str()));
table.add_row(Row::new(row));
let path = path.unwrap().path();
let version = path.file_name().unwrap().to_string_lossy().into_owned();
if version == args {
exists = true;
break;
}
}

if !exists {
writer.write_err(format!("Can't found version: {}, package list:", args).as_str());
let list = ListCommand::create(self.conf.clone());
list.exec(writer, args)?;
return Ok(());
}
table.print(writer).unwrap();

// Write to status.
let mut status = Status::read(self.conf.clone())?;
status.version = args.clone();
status.write()?;

writer.write_ok(format!("Package switch to {}", args).as_str());

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions fusecli/cli/src/cmds/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::error::Result;
#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct Status {
path: String,
pub latest: String,
pub version: String,
}

impl Status {
Expand All @@ -25,7 +25,7 @@ impl Status {
let file = File::create(status_path.as_str())?;
let status = Status {
path: status_path.clone(),
latest: "".to_string(),
version: "".to_string(),
};
serde_json::to_writer(&file, &status)?;
}
Expand Down
2 changes: 1 addition & 1 deletion fusecli/cli/src/cmds/status_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn test_status() -> Result<()> {
conf.datafuse_dir = "/tmp/".to_string();

let mut status = Status::read(conf)?;
status.latest = "xx".to_string();
status.version = "xx".to_string();
status.write()?;

Ok(())
Expand Down

0 comments on commit ac12dd8

Please sign in to comment.