Skip to content

Commit

Permalink
chore: move hub connector commands to fluvio-hub-util (#3611)
Browse files Browse the repository at this point in the history
This is the first half of a two-part feature related to #3578.

In this first part, we only move common code from `fluvio-cli` to `fluvio-hub-util` behind a feature flag. The second PR will reuse the command definitions in `cdk`.
  • Loading branch information
matheus-consoli committed Nov 1, 2023
1 parent 37a4a3b commit 472e361
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 84 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/fluvio-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fluvio-package-index = { workspace = true }
fluvio-extension-common = { workspace = true, features = ["target"] }
fluvio-channel = { workspace = true }
fluvio-cli-common = { workspace = true }
fluvio-hub-util = { workspace = true }
fluvio-hub-util = { workspace = true, features = ["connector-cmds"] }
fluvio-smartengine = { workspace = true, features = ["transformation"]}
fluvio-protocol = { workspace = true, features=["record","api"] }
fluvio-smartmodule = { workspace = true }
Expand Down
10 changes: 1 addition & 9 deletions crates/fluvio-cli/src/client/hub/connector/mod.rs
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;
Expand All @@ -11,16 +8,11 @@ use anyhow::Result;

use fluvio_extension_common::Terminal;

use super::{get_pkg_list, get_hub_access};

/// 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),
}
Expand Down
38 changes: 0 additions & 38 deletions crates/fluvio-cli/src/client/hub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,41 +59,3 @@ mod cmd {
}
}
}

use anyhow::Result;

use fluvio_hub_util as hubutil;
use hubutil::HubAccess;
use hubutil::PackageListMeta;

use crate::{CliError};

pub(crate) fn get_hub_access(remote: &Option<String>) -> Result<HubAccess> {
let access = HubAccess::default_load(remote).map_err(|_| {
CliError::HubError("missing access credentials, try 'fluvio cloud login'".into())
})?;
Ok(access)
}

pub(crate) async fn get_pkg_list(
endpoint: &str,
remote: &Option<String>,
) -> Result<PackageListMeta> {
use hubutil::http;

let access = get_hub_access(remote)?;

let action_token = access.get_list_token().await.map_err(|_| {
CliError::HubError("rejected access credentials, try 'fluvio cloud login'".into())
})?;
let url = format!("{}/{endpoint}", &access.remote);
let mut res = http::get(&url)
.header("Authorization", &action_token)
.await
.map_err(|e| CliError::HubError(format!("list api access error {e}")))?;
let pl: PackageListMeta = res
.body_json()
.await
.map_err(|e| CliError::HubError(format!("list api data parse error {e}")))?;
Ok(pl)
}
2 changes: 1 addition & 1 deletion crates/fluvio-cli/src/client/hub/smartmodule/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use fluvio_extension_common::Terminal;
use fluvio_extension_common::target::ClusterTarget;
use fluvio_hub_util as hubutil;
use hubutil::HubAccess;
use hubutil::cmd::get_hub_access;

use crate::CliError;
use crate::client::cmd::ClientCmd;
use crate::client::hub::get_hub_access;

/// Download a SmartModule from the hub
#[derive(Debug, Parser)]
Expand Down
3 changes: 1 addition & 2 deletions crates/fluvio-cli/src/client/hub/smartmodule/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use anyhow::Result;

use fluvio_extension_common::Terminal;
use fluvio_hub_util::HUB_API_LIST_META;
use fluvio_hub_util::cmd::get_pkg_list;

use crate::common::OutputFormat;

use super::get_pkg_list;

/// List available SmartModules in the hub
#[derive(Debug, Parser)]
pub struct SmartModuleHubListOpts {
Expand Down
2 changes: 0 additions & 2 deletions crates/fluvio-cli/src/client/hub/smartmodule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use fluvio::Fluvio;

use crate::client::ClientCmd;

use super::get_pkg_list;

/// List available SmartModules in the hub
#[derive(Debug, Parser)]
pub enum SmartModuleHubSubCmd {
Expand Down
12 changes: 12 additions & 0 deletions crates/fluvio-hub-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ authors = ["fluvio.io"]
repository = "https://github.com/infinyon/fluvio"
publish = false

[features]
connector-cmds = [
"dep:clap",
"dep:current_platform",
"dep:comfy-table",
"dep:fluvio-extension-common",
]

[dependencies]
anyhow = { workspace = true }
cargo_toml = { workspace = true }
Expand Down Expand Up @@ -35,10 +43,14 @@ tracing = { workspace = true }
thiserror = { workspace = true }
url = { workspace = true }
wasmparser = { workspace = true }
current_platform = { workspace = true, optional = true }
clap = { workspace = true, optional = true }
comfy-table = { workspace = true, optional = true }

fluvio-future = { workspace = true, features = ["fixture", "task"] }
fluvio-hub-protocol = { path = "../fluvio-hub-protocol" }
fluvio-types = { workspace = true }
fluvio-extension-common = { workspace = true, optional = true }

[dev-dependencies]
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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 {
Expand All @@ -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(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use clap::Parser;
use anyhow::Result;

use fluvio_extension_common::Terminal;
use fluvio_hub_util::HUB_API_CONN_LIST;

use crate::common::OutputFormat;
use fluvio_extension_common::OutputFormat;
use crate::HUB_API_CONN_LIST;

use super::get_pkg_list;

/// List all available SmartConnectors
#[derive(Debug, Parser)]
pub struct ConnectorHubListOpts {
#[clap(flatten)]
Expand All @@ -28,10 +28,7 @@ impl ConnectorHubListOpts {
}
}

// #[allow(dead_code)]
mod output {

//!
//! # Fluvio hub list - output processing
//!
//! Format SmartModules response based on output type
Expand All @@ -45,7 +42,7 @@ mod output {
use fluvio_extension_common::Terminal;
use fluvio_extension_common::output::TableOutputHandler;
use fluvio_extension_common::t_println;
use fluvio_hub_util::PackageMeta;
use crate::PackageMeta;

#[derive(Serialize)]
struct ListConnectors(Vec<PackageMeta>);
Expand Down
34 changes: 34 additions & 0 deletions crates/fluvio-hub-util/src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
mod list;
mod download;

pub use list::ConnectorHubListOpts;
pub use download::ConnectorHubDownloadOpts;
use crate::HubAccess;
use crate::PackageListMeta;

use anyhow::Result;
use anyhow::anyhow;

pub fn get_hub_access(remote: &Option<String>) -> Result<HubAccess> {
HubAccess::default_load(remote)
.map_err(|_| anyhow!("missing access credentials, try 'fluvio cloud login'"))
}

pub async fn get_pkg_list(endpoint: &str, remote: &Option<String>) -> Result<PackageListMeta> {
use crate::http;

let access = get_hub_access(remote)?;

let action_token = access
.get_list_token()
.await
.map_err(|_| anyhow!("rejected access credentials, try 'fluvio cloud login'"))?;
let url = format!("{}/{endpoint}", &access.remote);
let mut res = http::get(&url)
.header("Authorization", &action_token)
.await
.map_err(|e| anyhow!("list api access error {e}"))?;
res.body_json()
.await
.map_err(|e| anyhow!("list api data parse error {e}"))
}
3 changes: 3 additions & 0 deletions crates/fluvio-hub-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ mod package;
mod package_meta_ext;
mod utils;

#[cfg(feature = "connector-cmds")]
pub mod cmd;

pub mod keymgmt;
pub mod fvm;

Expand Down

0 comments on commit 472e361

Please sign in to comment.