Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Refactor cli #148

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/cli/commands/checkpoint/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
use crate::cli::commands::checkpoint::list_checkpoints::{ListCheckpoints, ListCheckpointsArgs};
use crate::cli::{CommandLineHandler, GlobalArguments};

use clap::{Args, Subcommand};

mod list_checkpoints;

#[derive(Debug, Args)]
#[command(name = "checkpoint", about = "checkpoint related commands")]
#[command(args_conflicts_with_subcommands = true)]
pub(crate) struct CheckpointCommandsArgs {
#[command(subcommand)]
command: Commands,
}

impl CheckpointCommandsArgs {
pub async fn handle(&self, global: &GlobalArguments) -> anyhow::Result<()> {
match &self.command {
Commands::List(args) => ListCheckpoints::handle(global, args).await,
}
}
}

#[derive(Debug, Subcommand)]
pub(crate) enum Commands {
List(ListCheckpointsArgs),
}
43 changes: 43 additions & 0 deletions src/cli/commands/config/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
use crate::cli::{CommandLineHandler, GlobalArguments};
use crate::config::DEFAULT_CONFIG_TEMPLATE;
use async_trait::async_trait;
use std::io::Write;

use clap::Args;

/// The command to initialize a new config template in a specific path
pub(crate) struct InitConfig;

#[async_trait]
impl CommandLineHandler for InitConfig {
type Arguments = InitConfigArgs;

async fn handle(global: &GlobalArguments, _arguments: &Self::Arguments) -> anyhow::Result<()> {
let path = global.config_path();
log::debug!("initializing empty config file in {}", path);

let file_path = std::path::Path::new(&path);
if let Some(parent) = file_path.parent() {
std::fs::create_dir_all(parent)?;
}
let mut file = std::fs::File::create(&path).map_err(|e| {
log::error!("couldn't create config file");
e
})?;
file.write_all(DEFAULT_CONFIG_TEMPLATE.as_bytes())
.map_err(|e| {
log::error!("error populating empty config template");
e
})?;

log::info!("Empty config populated successful in {}", &path);

Ok(())
}
}

#[derive(Debug, Args)]
#[command(about = "Arguments to initialize a new empty config file")]
pub(crate) struct InitConfigArgs {}
36 changes: 36 additions & 0 deletions src/cli/commands/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
//! This mod triggers a config reload in the IPC-Agent Json RPC server.

mod init;
mod reload;

use clap::{Args, Subcommand};
use std::fmt::Debug;

use crate::cli::commands::config::init::{InitConfig, InitConfigArgs};
use crate::cli::commands::config::reload::{ReloadConfig, ReloadConfigArgs};
use crate::cli::{CommandLineHandler, GlobalArguments};

#[derive(Debug, Args)]
#[command(name = "config", about = "config related commands")]
#[command(args_conflicts_with_subcommands = true)]
pub(crate) struct ConfigCommandsArgs {
#[command(subcommand)]
command: Commands,
}

impl ConfigCommandsArgs {
pub async fn handle(&self, global: &GlobalArguments) -> anyhow::Result<()> {
match &self.command {
Commands::Reload(args) => ReloadConfig::handle(global, args).await,
Commands::Init(args) => InitConfig::handle(global, args).await,
}
}
}

#[derive(Debug, Subcommand)]
pub(crate) enum Commands {
Reload(ReloadConfigArgs),
Init(InitConfigArgs),
}
46 changes: 3 additions & 43 deletions src/cli/commands/config.rs → src/cli/commands/config/reload.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
//! This mod triggers a config reload in the IPC-Agent Json RPC server.

use async_trait::async_trait;
use clap::Args;
use std::fmt::Debug;
use std::io::Write;

use crate::cli::commands::get_ipc_agent_url;
use crate::cli::{CommandLineHandler, GlobalArguments};
use crate::config::{json_rpc_methods, DEFAULT_CONFIG_TEMPLATE};
use crate::config::json_rpc_methods;
use crate::jsonrpc::{JsonRpcClient, JsonRpcClientImpl};
use crate::server::ReloadConfigParams;
use async_trait::async_trait;
use clap::Args;

/// The command to reload the agent config after an update
pub(crate) struct ReloadConfig;
@@ -55,38 +50,3 @@ pub(crate) struct ReloadConfigArgs {
#[arg(short, long, help = "The JSON RPC server url for ipc agent, optional")]
pub ipc_agent_url: Option<String>,
}

/// The command to initialize a new config template in a specific path
pub(crate) struct InitConfig;

#[async_trait]
impl CommandLineHandler for InitConfig {
type Arguments = InitConfigArgs;

async fn handle(global: &GlobalArguments, _arguments: &Self::Arguments) -> anyhow::Result<()> {
let path = global.config_path();
log::debug!("initializing empty config file in {}", path);

let file_path = std::path::Path::new(&path);
if let Some(parent) = file_path.parent() {
std::fs::create_dir_all(parent)?;
}
let mut file = std::fs::File::create(&path).map_err(|e| {
log::error!("couldn't create config file");
e
})?;
file.write_all(DEFAULT_CONFIG_TEMPLATE.as_bytes())
.map_err(|e| {
log::error!("error populating empty config template");
e
})?;

log::info!("Empty config populated successful in {}", &path);

Ok(())
}
}

#[derive(Debug, Args)]
#[command(about = "Arguments to initialize a new empty config file")]
pub(crate) struct InitConfigArgs {}
File renamed without changes.
45 changes: 45 additions & 0 deletions src/cli/commands/crossmsg/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2022-2023 Protocol Labs
// SPDX-License-Identifier: MIT
use crate::cli::commands::crossmsg::fund::Fund;
use crate::cli::commands::crossmsg::propagate::Propagate;
use crate::cli::commands::crossmsg::release::Release;
use crate::cli::commands::crossmsg::whitelist::WhitelistPropagator;
use crate::cli::{CommandLineHandler, GlobalArguments};
use fund::FundArgs;
use propagate::PropagateArgs;
use release::ReleaseArgs;
use whitelist::WhitelistPropagatorArgs;

use clap::{Args, Subcommand};

pub mod fund;
pub mod propagate;
pub mod release;
pub mod whitelist;

#[derive(Debug, Args)]
#[command(name = "crossmsg", about = "cross network messages related commands")]
#[command(args_conflicts_with_subcommands = true)]
pub(crate) struct CrossMsgsCommandsArgs {
#[command(subcommand)]
command: Commands,
}

impl CrossMsgsCommandsArgs {
pub async fn handle(&self, global: &GlobalArguments) -> anyhow::Result<()> {
match &self.command {
Commands::Fund(args) => Fund::handle(global, args).await,
Commands::Release(args) => Release::handle(global, args).await,
Commands::Propagate(args) => Propagate::handle(global, args).await,
Commands::WhitelistPropagator(args) => WhitelistPropagator::handle(global, args).await,
}
}
}

#[derive(Debug, Subcommand)]
pub(crate) enum Commands {
Fund(FundArgs),
Release(ReleaseArgs),
Propagate(PropagateArgs),
WhitelistPropagator(WhitelistPropagatorArgs),
}
File renamed without changes.
15 changes: 0 additions & 15 deletions src/cli/commands/manager/mod.rs

This file was deleted.

72 changes: 21 additions & 51 deletions src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -2,31 +2,27 @@
// SPDX-License-Identifier: MIT
//! This mod contains the different command line implementations.

mod checkpoint;
mod config;
mod crossmsg;
mod daemon;
mod manager;
mod subnet;
mod wallet;

use crate::cli::commands::config::{InitConfig, InitConfigArgs, ReloadConfig, ReloadConfigArgs};
use crate::cli::commands::checkpoint::CheckpointCommandsArgs;
use crate::cli::commands::crossmsg::CrossMsgsCommandsArgs;
use crate::cli::commands::daemon::{LaunchDaemon, LaunchDaemonArgs};
pub use crate::cli::commands::manager::create::{CreateSubnet, CreateSubnetArgs};
use crate::cli::commands::manager::fund::{Fund, FundArgs};
pub use crate::cli::commands::manager::join::{JoinSubnet, JoinSubnetArgs};
pub use crate::cli::commands::manager::kill::{KillSubnet, KillSubnetArgs};
pub use crate::cli::commands::manager::leave::{LeaveSubnet, LeaveSubnetArgs};
use crate::cli::commands::manager::list_checkpoints::{ListCheckpoints, ListCheckpointsArgs};
use crate::cli::commands::manager::list_subnets::{ListSubnets, ListSubnetsArgs};
use crate::cli::commands::manager::net_addr::{SetValidatorNetAddr, SetValidatorNetAddrArgs};
use crate::cli::commands::manager::propagate::{Propagate, PropagateArgs};
use crate::cli::commands::manager::release::{Release, ReleaseArgs};
use crate::cli::commands::manager::whitelist::{WhitelistPropagator, WhitelistPropagatorArgs};
use crate::cli::{CommandLineHandler, GlobalArguments};
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::fmt::Debug;
use subnet::SubnetCommandsArgs;
use url::Url;

use self::manager::send_value::{SendValue, SendValueArgs};
use self::manager::wallet::{WalletNew, WalletNewArgs};
use crate::cli::commands::config::ConfigCommandsArgs;
use crate::cli::commands::wallet::WalletCommandsArgs;

pub use subnet::*;

/// The collection of all subcommands to be called, see clap's documentation for usage. Internal
/// to the current mode. Register a new command accordingly.
@@ -38,25 +34,11 @@ enum Commands {
/// and not in the background as what daemon processes are. Still, this struct contains `Daemon`
/// due to the convention from `lotus` and the expected behavior from the filecoin user group.
Daemon(LaunchDaemonArgs),

/// Config commands
ReloadConfig(ReloadConfigArgs),
InitConfig(InitConfigArgs),

/// Subnet manager commands
CreateSubnet(CreateSubnetArgs),
ListSubnets(ListSubnetsArgs),
JoinSubnet(JoinSubnetArgs),
LeaveSubnet(LeaveSubnetArgs),
KillSubnet(KillSubnetArgs),
Fund(FundArgs),
Release(ReleaseArgs),
Propagate(PropagateArgs),
WhitelistPropagator(WhitelistPropagatorArgs),
SendValue(SendValueArgs),
WalletNew(WalletNewArgs),
SetValidatorNetAddr(SetValidatorNetAddrArgs),
ListCheckpoints(ListCheckpointsArgs),
Config(ConfigCommandsArgs),
Subnet(SubnetCommandsArgs),
Wallet(WalletCommandsArgs),
CrossMsg(CrossMsgsCommandsArgs),
Checkpoint(CheckpointCommandsArgs),
}
#[derive(Debug, Parser)]
#[command(
@@ -107,23 +89,11 @@ pub async fn cli() {
let global = &args.global_params;
let r = match &args.command {
Commands::Daemon(args) => LaunchDaemon::handle(global, args).await,
// Config commands
Commands::ReloadConfig(args) => ReloadConfig::handle(global, args).await,
Commands::InitConfig(args) => InitConfig::handle(global, args).await,
// Subnet manager commands
Commands::CreateSubnet(args) => CreateSubnet::handle(global, args).await,
Commands::ListSubnets(args) => ListSubnets::handle(global, args).await,
Commands::JoinSubnet(args) => JoinSubnet::handle(global, args).await,
Commands::LeaveSubnet(args) => LeaveSubnet::handle(global, args).await,
Commands::KillSubnet(args) => KillSubnet::handle(global, args).await,
Commands::Fund(args) => Fund::handle(global, args).await,
Commands::Release(args) => Release::handle(global, args).await,
Commands::Propagate(args) => Propagate::handle(global, args).await,
Commands::WhitelistPropagator(args) => WhitelistPropagator::handle(global, args).await,
Commands::SendValue(args) => SendValue::handle(global, args).await,
Commands::WalletNew(args) => WalletNew::handle(global, args).await,
Commands::SetValidatorNetAddr(args) => SetValidatorNetAddr::handle(global, args).await,
Commands::ListCheckpoints(args) => ListCheckpoints::handle(global, args).await,
Commands::Config(args) => args.handle(global).await,
Commands::Subnet(args) => args.handle(global).await,
Commands::CrossMsg(args) => args.handle(global).await,
Commands::Wallet(args) => args.handle(global).await,
Commands::Checkpoint(args) => args.handle(global).await,
};

if let Err(e) = r {
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ impl CommandLineHandler for CreateSubnet {
}

#[derive(Debug, Args)]
#[command(about = "Create a new subnet actor")]
#[command(name = "create", about = "Create a new subnet actor")]
pub struct CreateSubnetArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ impl CommandLineHandler for JoinSubnet {
}

#[derive(Debug, Args)]
#[command(about = "Join a subnet")]
#[command(name = "join", about = "Join a subnet")]
pub struct JoinSubnetArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ impl CommandLineHandler for KillSubnet {
}

#[derive(Debug, Args)]
#[command(about = "Kill an existing subnet")]
#[command(name = "kill", about = "Kill an existing subnet")]
pub struct KillSubnetArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ impl CommandLineHandler for LeaveSubnet {
}

#[derive(Debug, Args)]
#[command(about = "Leaving a subnet")]
#[command(name = "leave", about = "Leaving a subnet")]
pub struct LeaveSubnetArgs {
#[arg(long, short, help = "The JSON RPC server url for ipc agent")]
pub ipc_agent_url: Option<String>,
Original file line number Diff line number Diff line change
@@ -62,6 +62,7 @@ impl CommandLineHandler for ListSubnets {

#[derive(Debug, Args)]
#[command(
name = "list",
about = "List all child subnets registered in the gateway (i.e. that have provided enough collateral)"
)]
pub(crate) struct ListSubnetsArgs {
Loading