-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the second PR of a two-part feature related to #3578. The first part can be found in #3611. Reuse the code from #3611 at `cdk hub`. CI is expected to fail, see the comment below. Closes #3578
- Loading branch information
1 parent
e2fb300
commit 8d473ac
Showing
17 changed files
with
173 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use clap::Parser; | ||
use fluvio_future::task; | ||
use fluvio_hub_util::cmd::{ConnectorHubListOpts, ConnectorHubDownloadOpts}; | ||
use fluvio_extension_common::PrintTerminal; | ||
|
||
/// Work with Connectors hub | ||
#[derive(Debug, Parser)] | ||
pub enum HubCmd { | ||
#[command(name = "list")] | ||
List(ConnectorHubListOpts), | ||
#[command(name = "download")] | ||
Download(ConnectorHubDownloadOpts), | ||
} | ||
|
||
impl HubCmd { | ||
pub fn process(self) -> Result<()> { | ||
let terminal = Arc::new(PrintTerminal::new()); | ||
match self { | ||
HubCmd::List(opt) => task::run_block_on(opt.process(terminal)), | ||
HubCmd::Download(opt) => task::run_block_on(opt.process(terminal)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
mod list; | ||
use list::ConnectorHubListOpts; | ||
mod download; | ||
use download::ConnectorHubDownloadOpts; | ||
use fluvio_hub_util::cmd::{ConnectorHubDownloadOpts, ConnectorHubListOpts}; | ||
|
||
use std::sync::Arc; | ||
use std::fmt::Debug; | ||
|
@@ -11,16 +8,17 @@ use anyhow::Result; | |
|
||
use fluvio_extension_common::Terminal; | ||
|
||
use super::{get_pkg_list, get_hub_access}; | ||
const CONNECTOR_HUB_DOWNLOAD_REDIRECT_MESSAGE: &str = r#"Connectors running on `InfinyOn Cloud` are automatically downloaded during `fluvio cloud connector create ...`. | ||
Connectors running locally require `cdk` to download and deploy: | ||
1. Install cdk: `fluvio install cdk` | ||
2. Download connector: `cdk hub download ...` | ||
3. Deploy connector: `cdk deploy start ...`"#; | ||
|
||
/// List available Connectors in the hub | ||
#[derive(Debug, Parser)] | ||
pub enum ConnectorHubSubCmd { | ||
/// List all available SmartConnectors | ||
#[command(name = "list")] | ||
List(ConnectorHubListOpts), | ||
|
||
/// Download SmartConnector to the local folder | ||
#[command(name = "download")] | ||
Download(ConnectorHubDownloadOpts), | ||
} | ||
|
@@ -29,7 +27,56 @@ impl ConnectorHubSubCmd { | |
pub async fn process<O: Terminal + Debug + Send + Sync>(self, out: Arc<O>) -> Result<()> { | ||
match self { | ||
ConnectorHubSubCmd::List(opts) => opts.process(out).await, | ||
ConnectorHubSubCmd::Download(opts) => opts.process(out).await, | ||
ConnectorHubSubCmd::Download(_) => { | ||
out.println(CONNECTOR_HUB_DOWNLOAD_REDIRECT_MESSAGE); | ||
Ok(()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::sync::{RwLock, Arc}; | ||
|
||
use clap::Parser; | ||
use fluvio_extension_common::Terminal; | ||
use fluvio_future::task::run_block_on; | ||
|
||
use crate::client::hub::connector::CONNECTOR_HUB_DOWNLOAD_REDIRECT_MESSAGE; | ||
|
||
use super::ConnectorHubSubCmd; | ||
|
||
#[derive(Default, Debug)] | ||
struct MockTerminal(Arc<RwLock<String>>); | ||
impl Terminal for MockTerminal { | ||
fn print(&self, msg: &str) { | ||
self.0 | ||
.write() | ||
.expect("could not print to MockTerminal") | ||
.push_str(msg); | ||
} | ||
|
||
fn println(&self, msg: &str) { | ||
self.0 | ||
.write() | ||
.expect("could not println to MockTerminal") | ||
.push_str(&format!("{msg}\n")); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_calling_fluvio_hub_download_displays_a_help_message() { | ||
let terminal = Arc::new(MockTerminal::default()); | ||
let cmd = | ||
ConnectorHubSubCmd::parse_from(["conn", "download", "infinyon/[email protected]"]); | ||
|
||
assert!(matches!(cmd, ConnectorHubSubCmd::Download(_))); | ||
|
||
run_block_on(cmd.process(terminal.clone())).expect("command failed to process"); | ||
|
||
let x = terminal.0.read().unwrap(); | ||
let x = x.as_str().trim_end(); | ||
assert_eq!(x, CONNECTOR_HUB_DOWNLOAD_REDIRECT_MESSAGE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,17 @@ use std::sync::Arc; | |
use std::fmt::Debug; | ||
|
||
use clap::Parser; | ||
use anyhow::Result; | ||
use anyhow::{Result, anyhow}; | ||
|
||
use fluvio_extension_common::Terminal; | ||
|
||
use crate::error::CliError; | ||
use crate::{cli_pkgname_to_filename, cli_conn_pkgname_to_url, get_package}; | ||
|
||
use super::get_hub_access; | ||
|
||
/// Download SmartConnector to the local folder | ||
#[derive(Debug, Parser)] | ||
#[command(arg_required_else_help = true)] | ||
pub struct ConnectorHubDownloadOpts { | ||
/// SmartConnector name: e.g. infinyon/[email protected] | ||
#[arg(value_name = "name", required = true)] | ||
|
@@ -37,10 +39,8 @@ impl ConnectorHubDownloadOpts { | |
let access = get_hub_access(&self.remote)?; | ||
|
||
let package_name = self.package_name; | ||
let file_name = fluvio_hub_util::cli_pkgname_to_filename(&package_name).map_err(|_| { | ||
CliError::HubError(format!( | ||
"invalid package name format {package_name}, is it the form infinyon/[email protected]" | ||
)) | ||
let file_name = cli_pkgname_to_filename(&package_name).map_err(|_| { | ||
anyhow!("invalid package name format {package_name}, is it the form infinyon/[email protected]") | ||
})?; | ||
|
||
let file_path = if let Some(mut output) = self.output { | ||
|
@@ -51,28 +51,18 @@ impl ConnectorHubDownloadOpts { | |
} else { | ||
PathBuf::from(file_name) | ||
}; | ||
println!( | ||
"downloading {package_name} to {}", | ||
file_path.to_string_lossy() | ||
); | ||
let path = file_path.to_string_lossy(); | ||
println!("downloading {package_name} to {path}"); | ||
|
||
let url = | ||
fluvio_hub_util::cli_conn_pkgname_to_url(&package_name, &access.remote, &self.target) | ||
.map_err(|_| CliError::HubError(format!("invalid pkgname {package_name}")))?; | ||
let url = cli_conn_pkgname_to_url(&package_name, &access.remote, &self.target) | ||
.map_err(|_| anyhow!("invalid pkgname {package_name}"))?; | ||
|
||
let data = fluvio_hub_util::get_package(&url, &access) | ||
let data = get_package(&url, &access) | ||
.await | ||
.map_err(|err| { | ||
CliError::HubError(format!( | ||
"downloading {package_name} failed\nHub error: {err}" | ||
)) | ||
})?; | ||
.map_err(|err| anyhow!("downloading {package_name} failed\nServer: {err}"))?; | ||
|
||
std::fs::write(file_path, data).map_err(|err| { | ||
CliError::Other(format!( | ||
"unable to write downloaded package to the disk: {err}" | ||
)) | ||
})?; | ||
std::fs::write(file_path, data) | ||
.map_err(|err| anyhow!("unable to write downloaded package to the disk: {err}"))?; | ||
println!("... downloading complete"); | ||
Ok(()) | ||
} | ||
|
Oops, something went wrong.