Skip to content

Commit

Permalink
feat: implement context invite & identity new command (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic authored Nov 1, 2024
1 parent d284f72 commit 6edbe82
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 190 deletions.
8 changes: 6 additions & 2 deletions crates/meroctl/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use crate::output::{Format, Output, Report};

mod app;
mod context;
mod identity;
mod jsonrpc;

use app::AppCommand;
use context::ContextCommand;
use identity::IdentityCommand;
use jsonrpc::CallCommand;

pub const EXAMPLES: &str = r"
Expand Down Expand Up @@ -44,8 +46,9 @@ pub struct RootCommand {

#[derive(Debug, Subcommand)]
pub enum SubCommands {
Context(ContextCommand),
App(AppCommand),
Context(ContextCommand),
Identity(IdentityCommand),
JsonRpc(CallCommand),
}

Expand Down Expand Up @@ -81,8 +84,9 @@ impl RootCommand {
let environment = Environment::new(self.args, output);

let result = match self.action {
SubCommands::Context(context) => context.run(&environment).await,
SubCommands::App(application) => application.run(&environment).await,
SubCommands::Context(context) => context.run(&environment).await,
SubCommands::Identity(identity) => identity.run(&environment).await,
SubCommands::JsonRpc(jsonrpc) => jsonrpc.run(&environment).await,
};

Expand Down
4 changes: 4 additions & 0 deletions crates/meroctl/src/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use eyre::Result as EyreResult;
use crate::cli::context::create::CreateCommand;
use crate::cli::context::delete::DeleteCommand;
use crate::cli::context::get::GetCommand;
use crate::cli::context::invite::InviteCommand;
use crate::cli::context::join::JoinCommand;
use crate::cli::context::list::ListCommand;
use crate::cli::context::watch::WatchCommand;
Expand All @@ -15,6 +16,7 @@ use crate::output::Report;
mod create;
mod delete;
mod get;
mod invite;
mod join;
mod list;
mod watch;
Expand Down Expand Up @@ -47,6 +49,7 @@ pub enum ContextSubCommands {
List(ListCommand),
Create(Box<CreateCommand>),
Join(JoinCommand),
Invite(InviteCommand),
Get(GetCommand),
#[command(alias = "del")]
Delete(DeleteCommand),
Expand All @@ -68,6 +71,7 @@ impl ContextCommand {
ContextSubCommands::Create(create) => create.run(environment).await,
ContextSubCommands::Delete(delete) => delete.run(environment).await,
ContextSubCommands::Get(get) => get.run(environment).await,
ContextSubCommands::Invite(invite) => invite.run(environment).await,
ContextSubCommands::Join(join) => join.run(environment).await,
ContextSubCommands::List(list) => list.run(environment).await,
ContextSubCommands::Watch(watch) => watch.run(environment).await,
Expand Down
60 changes: 60 additions & 0 deletions crates/meroctl/src/cli/context/invite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use calimero_primitives::context::ContextId;
use calimero_primitives::identity::PublicKey;
use calimero_server_primitives::admin::{InviteToContextRequest, InviteToContextResponse};
use clap::Parser;
use eyre::Result as EyreResult;
use reqwest::Client;

use crate::cli::Environment;
use crate::common::{do_request, fetch_multiaddr, load_config, multiaddr_to_url, RequestType};
use crate::output::Report;

#[derive(Debug, Parser)]
#[command(about = "Create invitation to a context for a invitee")]
pub struct InviteCommand {
#[clap(
value_name = "CONTEXT_ID",
help = "The id of the context for which invitation is created"
)]
pub context_id: ContextId,

#[clap(value_name = "INVITER_ID", help = "The public key of the inviter")]
pub inviter_id: PublicKey,

#[clap(value_name = "INVITEE_ID", help = "The public key of the invitee")]
pub invitee_id: PublicKey,
}

impl Report for InviteToContextResponse {
fn report(&self) {
match self.data {
Some(ref payload) => {
println!("{:?}", payload)
}
None => println!("No invitation payload"),
}
}
}

impl InviteCommand {
pub async fn run(self, environment: &Environment) -> EyreResult<()> {
let config = load_config(&environment.args.home, &environment.args.node_name)?;

let response: InviteToContextResponse = do_request(
&Client::new(),
multiaddr_to_url(fetch_multiaddr(&config)?, "admin-api/dev/contexts/invite")?,
Some(InviteToContextRequest {
context_id: self.context_id,
inviter_id: self.inviter_id,
invitee_id: self.invitee_id,
}),
&config.identity,
RequestType::Post,
)
.await?;

environment.output.write(&response);

Ok(())
}
}
37 changes: 37 additions & 0 deletions crates/meroctl/src/cli/identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use clap::{Parser, Subcommand};
use const_format::concatcp;
use eyre::Result as EyreResult;

use crate::cli::identity::generate::GenerateCommand;
use crate::cli::Environment;

mod generate;

pub const EXAMPLES: &str = r"
#
$ meroctl -- --node-name node1 identity generate
";

#[derive(Debug, Parser)]
#[command(about = "Command for managing applications")]
#[command(after_help = concatcp!(
"Examples:",
EXAMPLES
))]
pub struct IdentityCommand {
#[command(subcommand)]
pub subcommand: IdentitySubCommands,
}

#[derive(Debug, Subcommand)]
pub enum IdentitySubCommands {
Generate(GenerateCommand),
}

impl IdentityCommand {
pub async fn run(self, environment: &Environment) -> EyreResult<()> {
match self.subcommand {
IdentitySubCommands::Generate(generate) => generate.run(environment).await,
}
}
}
40 changes: 40 additions & 0 deletions crates/meroctl/src/cli/identity/generate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use calimero_server_primitives::admin::GenerateContextIdentityResponse;
use clap::Parser;
use eyre::Result as EyreResult;
use reqwest::Client;

use crate::cli::Environment;
use crate::common::{do_request, fetch_multiaddr, load_config, multiaddr_to_url, RequestType};
use crate::output::Report;

#[derive(Debug, Parser)]
#[command(about = "Generate public/private key pair used for context identity")]
pub struct GenerateCommand;

impl Report for GenerateContextIdentityResponse {
fn report(&self) {
println!("public_key: {}", self.data.public_key);
println!("private_key: {}", self.data.private_key);
}
}

impl GenerateCommand {
pub async fn run(self, environment: &Environment) -> EyreResult<()> {
let config = load_config(&environment.args.home, &environment.args.node_name)?;

let url = multiaddr_to_url(fetch_multiaddr(&config)?, "admin-api/dev/identity/context")?;

let response: GenerateContextIdentityResponse = do_request(
&Client::new(),
url,
None::<()>,
&config.identity,
RequestType::Post,
)
.await?;

environment.output.write(&response);

Ok(())
}
}
Loading

0 comments on commit 6edbe82

Please sign in to comment.