Skip to content

Commit

Permalink
feat(cli): add endpoint create boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Jun 4, 2024
1 parent da5676e commit f4de78e
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,84 @@ use sdk::WebhooksSDK;
/// Cli app to manage webhook-rs server
#[derive(Debug, Parser)]
#[clap(name = "webhooks-cli", version, about)]
pub struct App {
pub struct Cli {
#[clap(subcommand)]
command: Command,
}

#[derive(Clone, Debug, Subcommand)]
enum Command {
/// Resource for application management
App {
Application {
#[clap(subcommand)]
subcommand: AppSubcommand,
subcommand: ApplicationSubcommand,
},
/// Resource for endpoints management
Endpoint {
#[clap(subcommand)]
subcommand: EndpointSubcommand,
},
}

#[derive(Clone, Debug, Subcommand)]
enum AppSubcommand {
enum ApplicationSubcommand {
/// Creates an application
Create {
/// App name
/// Application name
name: String,
},
}

#[derive(Clone, Debug, Subcommand)]
enum EndpointSubcommand {
/// Creates an endpoint
Create {
app_id: String,
url: String,
#[arg(value_parser, num_args = 1.., value_delimiter = ',', required = true)]
topics: Vec<String>,
},
}

#[tokio::main]
async fn main() {
dotenv().ok();

let app = App::parse();
let cli = Cli::parse();

let url = env::var("SERVER_URL").expect("env SERVER_URL is not set");
let sdk = WebhooksSDK::new(url.as_str());

match app.command {
Command::App { subcommand } => match subcommand {
AppSubcommand::Create { name } => {
match cli.command {
Command::Application { subcommand } => match subcommand {
ApplicationSubcommand::Create { name } => {
let app = sdk.application().create(name.as_str()).await.unwrap();

println!("App {} with name '{}' has been created", app.id, app.name);
}
},
Command::Endpoint { subcommand } => match subcommand {
EndpointSubcommand::Create {
app_id,
url,
topics,
} => {
println!("{}", app_id);
println!("{}", url);
println!("{:?}", topics);
}
},
};
}

#[cfg(test)]
mod test {
use clap::CommandFactory;

use crate::Cli;

#[test]
fn verify_cli() {
Cli::command().debug_assert()
}
}

0 comments on commit f4de78e

Please sign in to comment.