Skip to content

Commit

Permalink
Merge branch 'miraclx/calimero-identity-dump' into miraclx/use-config…
Browse files Browse the repository at this point in the history
…-contract
  • Loading branch information
miraclx committed Sep 10, 2024
2 parents 0aa7e04 + 369eba1 commit 253e08a
Show file tree
Hide file tree
Showing 22 changed files with 401 additions and 36 deletions.
3 changes: 2 additions & 1 deletion 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/meroctl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "meroctl"
version = "0.1.0"
version = "0.2.0-beta.1"
authors.workspace = true
edition.workspace = true
repository.workspace = true
Expand All @@ -24,6 +24,7 @@ rand.workspace = true
reqwest = { workspace = true, features = ["json"] }
semver.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
tokio = { workspace = true, features = ["io-std", "macros"] }
toml.workspace = true
toml_edit.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions crates/meroctl/src/cli/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use clap::{Parser, Subcommand};
use eyre::Result as EyreResult;

use super::RootArgs;
use crate::cli::app::get::GetCommand;
use crate::cli::app::install::InstallCommand;
use crate::cli::app::list::ListCommand;

mod get;
mod install;
mod list;

Expand All @@ -16,6 +18,7 @@ pub struct AppCommand {

#[derive(Debug, Subcommand)]
pub enum AppSubCommands {
Get(GetCommand),
Install(InstallCommand),
#[command(alias = "ls")]
List(ListCommand),
Expand All @@ -24,6 +27,7 @@ pub enum AppSubCommands {
impl AppCommand {
pub async fn run(self, args: RootArgs) -> EyreResult<()> {
match self.subcommand {
AppSubCommands::Get(get) => get.run(args).await,
AppSubCommands::Install(install) => install.run(args).await,
AppSubCommands::List(list) => list.run(args).await,
}
Expand Down
56 changes: 56 additions & 0 deletions crates/meroctl/src/cli/app/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use clap::{Parser, ValueEnum};
use eyre::{bail, Result as EyreResult};
use reqwest::Client;

use crate::cli::RootArgs;
use crate::common::RequestType::GET;
use crate::common::{get_response, multiaddr_to_url};
use crate::config_file::ConfigFile;

#[derive(Parser, Debug)]
pub struct GetCommand {
#[arg(long, short)]
pub method: GetValues,

#[arg(long, short)]
pub app_id: String,
}
#[derive(ValueEnum, Debug, Clone)]
pub enum GetValues {
Details,
}

impl GetCommand {
pub async fn run(self, args: RootArgs) -> EyreResult<()> {
let path = args.home.join(&args.node_name);

if !ConfigFile::exists(&path) {
bail!("Config file does not exist")
};

let Ok(config) = ConfigFile::load(&path) else {
bail!("Failed to load config file")
};

let Some(multiaddr) = config.network.server.listen.first() else {
bail!("No address.")
};

let client = Client::new();

let url = multiaddr_to_url(
multiaddr,
&format!("admin-api/dev/applications/{}", self.app_id),
)?;

let response = get_response(&client, url, None::<()>, &config.identity, GET).await?;

if !response.status().is_success() {
bail!("Request failed with status: {}", response.status())
}

println!("{}", response.text().await?);

Ok(())
}
}
54 changes: 44 additions & 10 deletions crates/meroctl/src/cli/app/install.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
use calimero_server_primitives::admin::{InstallApplicationResponse, InstallDevApplicationRequest};
use calimero_primitives::hash::Hash;
use calimero_server_primitives::admin::{
InstallApplicationRequest, InstallApplicationResponse, InstallDevApplicationRequest,
};
use camino::Utf8PathBuf;
use clap::Parser;
use eyre::{bail, Result};
use reqwest::Client;
use semver::Version;
use tracing::info;
use url::Url;

use crate::cli::RootArgs;
use crate::common::RequestType::POST;
use crate::common::{get_response, multiaddr_to_url};
use crate::config_file::ConfigFile;

#[derive(Debug, Parser)]
pub struct InstallCommand {
/// Path to the application
#[arg(long, short)]
pub path: Utf8PathBuf,
#[arg(long, short, conflicts_with = "url")]
pub path: Option<Utf8PathBuf>,

/// Url of the application
#[clap(long, short, conflicts_with = "path", requires = "metadata")]
pub url: Option<String>,

/// Version of the application
#[clap(short, long, help = "Version of the application")]
pub version: Option<Version>,

#[clap(short, long, help = "Metadata for the application")]
pub metadata: Option<Vec<u8>>,

#[clap(long, help = "Hash of the application")]
pub hash: Option<Hash>,
}

impl InstallCommand {
Expand All @@ -40,19 +53,40 @@ impl InstallCommand {

let client = Client::new();

let install_url = multiaddr_to_url(multiaddr, "admin-api/dev/install-application")?;
let mut is_dev_installation = false;

let install_request = InstallDevApplicationRequest::new(
self.path.canonicalize_utf8()?,
self.version,
self.metadata.unwrap_or_default(),
);
let install_request = if let Some(app_path) = self.path {
let install_dev_request = InstallDevApplicationRequest::new(
app_path.canonicalize_utf8()?,
self.version,
self.metadata.unwrap_or_default(),
);
is_dev_installation = true;
serde_json::to_value(install_dev_request)?
} else if let Some(app_url) = self.url {
let install_request = InstallApplicationRequest::new(
Url::parse(&app_url)?,
self.version,
self.hash,
self.metadata.unwrap_or_default(),
);
serde_json::to_value(install_request)?
} else {
bail!("Either path or url must be provided");
};

let install_url = if is_dev_installation {
multiaddr_to_url(multiaddr, "admin-api/dev/install-dev-application")?
} else {
multiaddr_to_url(multiaddr, "admin-api/dev/install-application")?
};

let install_response = get_response(
&client,
install_url,
Some(install_request),
&config.identity,
POST,
)
.await?;

Expand Down
3 changes: 2 additions & 1 deletion crates/meroctl/src/cli/app/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use eyre::{bail, Result as EyreResult};
use reqwest::Client;

use crate::cli::RootArgs;
use crate::common::RequestType::GET;
use crate::common::{get_response, multiaddr_to_url};
use crate::config_file::ConfigFile;

Expand All @@ -25,7 +26,7 @@ impl ListCommand {

let url = multiaddr_to_url(multiaddr, "admin-api/dev/applications")?;
let client = Client::new();
let response = get_response(&client, url, None::<()>, &config.identity).await?;
let response = get_response(&client, url, None::<()>, &config.identity, GET).await?;

if !response.status().is_success() {
bail!("Request failed with status: {}", response.status())
Expand Down
11 changes: 10 additions & 1 deletion crates/meroctl/src/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use const_format::concatcp;
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::join::JoinCommand;
use crate::cli::context::list::ListCommand;
use crate::cli::RootArgs;

mod create;
mod delete;
mod get;
mod join;
mod list;

Expand Down Expand Up @@ -39,14 +43,19 @@ pub enum ContextSubCommands {
List(ListCommand),
Create(Box<CreateCommand>),
Join(JoinCommand),
Get(GetCommand),
#[command(alias = "del")]
Delete(DeleteCommand),
}

impl ContextCommand {
pub async fn run(self, args: RootArgs) -> EyreResult<()> {
match self.subcommand {
ContextSubCommands::List(list) => list.run(args).await,
ContextSubCommands::Create(create) => create.run(args).await,
ContextSubCommands::Delete(delete) => delete.run(args).await,
ContextSubCommands::Get(get) => get.run(args).await,
ContextSubCommands::Join(join) => join.run(args).await,
ContextSubCommands::List(list) => list.run(args).await,
}
}
}
11 changes: 6 additions & 5 deletions crates/meroctl/src/cli/context/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tokio::runtime::Handle;
use tokio::sync::mpsc;

use crate::cli::RootArgs;
use crate::common::RequestType::{GET, POST};
use crate::common::{get_response, multiaddr_to_url};
use crate::config_file::ConfigFile;

Expand Down Expand Up @@ -143,7 +144,7 @@ async fn create_context(
params.map(String::into_bytes).unwrap_or_default(),
);

let response = get_response(client, url, Some(request), &keypair).await?;
let response = get_response(client, url, Some(request), &keypair, POST).await?;

if response.status().is_success() {
let context_response: CreateContextResponse = response.json().await?;
Expand Down Expand Up @@ -243,7 +244,7 @@ async fn update_context_application(

let request = UpdateContextApplicationRequest::new(application_id);

let response = get_response(client, url, Some(request), keypair).await?;
let response = get_response(client, url, Some(request), keypair, POST).await?;

if response.status().is_success() {
println!(
Expand Down Expand Up @@ -274,7 +275,7 @@ async fn app_installed(
&format!("admin-api/dev/application/{application_id}"),
)?;

let response = get_response(client, url, None::<()>, keypair).await?;
let response = get_response(client, url, None::<()>, keypair, GET).await?;

if !response.status().is_success() {
bail!("Request failed with status: {}", response.status())
Expand All @@ -292,13 +293,13 @@ async fn install_app(
metadata: Option<Vec<u8>>,
keypair: &Keypair,
) -> EyreResult<ApplicationId> {
let install_url = multiaddr_to_url(base_multiaddr, "admin-api/dev/install-application")?;
let install_url = multiaddr_to_url(base_multiaddr, "admin-api/dev/install-dev-application")?;

let install_request =
InstallDevApplicationRequest::new(path, None, metadata.unwrap_or_default());

let install_response =
get_response(client, install_url, Some(install_request), keypair).await?;
get_response(client, install_url, Some(install_request), keypair, POST).await?;

if !install_response.status().is_success() {
let status = install_response.status();
Expand Down
55 changes: 55 additions & 0 deletions crates/meroctl/src/cli/context/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use clap::Parser;
use eyre::{bail, Result as EyreResult};
use libp2p::Multiaddr;
use reqwest::Client;

use crate::cli::RootArgs;
use crate::common::{get_response, multiaddr_to_url, RequestType};
use crate::config_file::ConfigFile;

#[derive(Debug, Parser)]
pub struct DeleteCommand {
#[clap(long, short)]
pub context_id: String,
}

impl DeleteCommand {
pub async fn run(self, root_args: RootArgs) -> EyreResult<()> {
let path = root_args.home.join(&root_args.node_name);
if !ConfigFile::exists(&path) {
bail!("Config file does not exist")
}
let Ok(config) = ConfigFile::load(&path) else {
bail!("Failed to load config file");
};
let Some(multiaddr) = config.network.server.listen.first() else {
bail!("No address.")
};

let client = Client::new();

self.delete_context(multiaddr, &client, &config.identity)
.await
}

async fn delete_context(
&self,
multiaddr: &Multiaddr,
client: &Client,
keypair: &libp2p::identity::Keypair,
) -> EyreResult<()> {
let url = multiaddr_to_url(
multiaddr,
&format!("admin-api/dev/contexts/{}", self.context_id),
)?;
let response = get_response(client, url, None::<()>, keypair, RequestType::DELETE).await?;

if !response.status().is_success() {
bail!("Request failed with status: {}", response.status())
}

let text = response.text().await?;
println!("Context deleted successfully: {}", text);
Ok(())
}
}
Loading

0 comments on commit 253e08a

Please sign in to comment.