Skip to content

Commit

Permalink
feat: add cdk hub command (#3612)
Browse files Browse the repository at this point in the history
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
matheus-consoli committed Nov 1, 2023
1 parent 472e361 commit d0dd859
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ fluvio = { workspace = true }
fluvio-cli-common = { workspace = true, features = ["version-cmd"] }
fluvio-connector-deployer = { path = "../fluvio-connector-deployer"}
fluvio-connector-package = { workspace = true, features = ["toml"]}
fluvio-extension-common = { workspace = true }
fluvio-future = { workspace = true, features = ["subscriber"]}
fluvio-hub-util = { workspace = true }
fluvio-hub-util = { workspace = true, features = ["connector-cmds"] }
4 changes: 4 additions & 0 deletions crates/cdk/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::deploy::DeployCmd;
use crate::test::TestCmd;
use crate::publish::PublishCmd;
use crate::set_public::SetPublicCmd;
use crate::hub::HubCmd;

/// Connector Development Kit
#[derive(Debug, Parser)]
Expand All @@ -19,6 +20,8 @@ pub enum CdkCommand {
Deploy(DeployCmd),
Publish(PublishCmd),
Version(BasicVersionCmd),
#[command(subcommand)]
Hub(HubCmd),

#[command(name = "set-public")]
SetPublic(SetPublicCmd),
Expand All @@ -34,6 +37,7 @@ impl CdkCommand {
CdkCommand::SetPublic(opt) => opt.process(),
CdkCommand::Generate(opt) => opt.process(),
CdkCommand::Version(opt) => opt.process("CDK"),
CdkCommand::Hub(opt) => opt.process(),
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions crates/cdk/src/hub.rs
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)),
}
}
}
1 change: 1 addition & 0 deletions crates/cdk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod publish;
mod set_public;

pub(crate) mod utils;
mod hub;

fn main() -> anyhow::Result<()> {
use clap::Parser;
Expand Down
57 changes: 56 additions & 1 deletion crates/fluvio-cli/src/client/hub/connector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ use anyhow::Result;

use fluvio_extension_common::Terminal;

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 {
Expand All @@ -21,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);
}
}
9 changes: 9 additions & 0 deletions tests/cli/cdk_smoke_tests/cdk-basic.bats
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ setup_file() {
# Ensure the correct path is added
cat ./.hub/package-meta.yaml | grep '../../testing/README.md'
assert_success

# clean up
rm -r ../testing
}

@test "Run connector with --ipkg" {
Expand Down Expand Up @@ -157,3 +160,9 @@ setup_file() {
assert_output --partial "Connector runs with process id"
}

# fix CI authentication to hub service first:
# https://github.com/infinyon/fluvio/issues/3634
# @test "List connectors from hub" {
# run timeout 15s $CDK_BIN hub list
# assert_success
# }

0 comments on commit d0dd859

Please sign in to comment.