Skip to content

Commit

Permalink
Merge pull request #1314 from BohuTANG/cli-package-switch-938
Browse files Browse the repository at this point in the history
CLI-938: add datafuse-cli package switch command
  • Loading branch information
BohuTANG authored Aug 6, 2021
2 parents 1a2c6e1 + 6125af3 commit b83f0e5
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 8 deletions.
16 changes: 15 additions & 1 deletion fusecli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ OS version thinkpad 20.04 (kernel 5.10.0-1038-oem)
version Datafuse CLI version
comment # your comments
package Package command
[test] > package -h
package
Expand All @@ -37,13 +38,26 @@ FLAGS:
-h, --help Prints help information
SUBCOMMANDS:
fetch Fetch the latest version package
fetch Fetch the latest version package
list List all the packages
switch Switch the active datafuse to a specified version
[test] > package fetch
✅ [ok] Arch x86_64-unknown-linux-gnu
✅ [ok] Tag v0.4.69-nightly
✅ [ok] Binary /home/bohu/.datafuse/test/downloads/v0.4.69-nightly/datafuse--x86_64-unknown-linux-gnu.tar.gz
✅ [ok] Unpack /home/bohu/.datafuse/test/bin/v0.4.69-nightly
[test] > package list
+-----------------+-----------------------------------------------+---------+
| Version | Path | Current |
+-----------------+-----------------------------------------------+---------+
| v0.4.69-nightly | /home/bohu/.datafuse/test/bin/v0.4.69-nightly | ✅ |
+-----------------+-----------------------------------------------+---------+
| v0.4.68-nightly | /home/bohu/.datafuse/test/bin/v0.4.68-nightly | |
+-----------------+-----------------------------------------------+---------+
[test] > package switch v0.4.68-nightly
✅ [ok] Package switch to v0.4.68-nightly
[test] >
```
1 change: 1 addition & 0 deletions fusecli/cli/src/cmds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub use helps::help::HelpCommand;
pub use packages::fetch::FetchCommand;
pub use packages::list::ListCommand;
pub use packages::package::PackageCommand;
pub use packages::switch::SwitchCommand;
pub use processor::Processor;
pub use status::Status;
pub use versions::version::VersionCommand;
Expand Down
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
1 change: 1 addition & 0 deletions fusecli/cli/src/cmds/packages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
pub mod fetch;
pub mod list;
pub mod package;
pub mod switch;
16 changes: 16 additions & 0 deletions fusecli/cli/src/cmds/packages/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use std::cell::RefCell;

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

use crate::cmds::command::Command;
use crate::cmds::Config;
use crate::cmds::FetchCommand;
use crate::cmds::ListCommand;
use crate::cmds::SwitchCommand;
use crate::cmds::Writer;
use crate::error::Result;

Expand Down Expand Up @@ -38,6 +40,15 @@ impl PackageCommand {
.setting(AppSettings::DisableVersion)
.setting(AppSettings::ColoredHelp)
.about("List all the packages"),
)
.subcommand(
App::new("switch")
.setting(AppSettings::DisableVersion)
.setting(AppSettings::ColoredHelp)
.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 @@ -73,6 +84,11 @@ impl Command for PackageCommand {
let list = ListCommand::create(self.conf.clone());
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, val.to_string())?;
}
_ => writer.write_err("unknown command, usage: package -h"),
},
Err(err) => {
Expand Down
53 changes: 53 additions & 0 deletions fusecli/cli/src/cmds/packages/switch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2020-2021 The Datafuse Authors.
//
// SPDX-License-Identifier: Apache-2.0.

use std::fs;

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

#[derive(Clone)]
pub struct SwitchCommand {
conf: Config,
}

impl SwitchCommand {
pub fn create(conf: Config) -> Self {
SwitchCommand { conf }
}

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 exists = false;
for path in paths {
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(());
}

// 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(())
}
}
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 b83f0e5

Please sign in to comment.