Skip to content

Commit

Permalink
[aptos-cli] Update to use clap instead of structopt
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario authored and aptos-bot committed Apr 1, 2022
1 parent aa4684d commit ca10da0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/aptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ edition = "2018"
[dependencies]
anyhow = "1.0.52"
base64 = "0.13.0"
clap = "3.1.6"
futures = "0.3.12"
hex = "0.4.3"
itertools = "0.10.0"
rand = "0.8.3"
serde = "1.0.124"
serde_json = "1.0.64"
serde_yaml = "0.8.17"
structopt = "0.3.21"
thiserror = "1.0.24"
tokio = { version = "1.8.1", features = ["full"] }
tokio-util = { version = "0.6.4", features = ["compat"] }
Expand Down
14 changes: 7 additions & 7 deletions crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use clap::Parser;
use std::{fmt::Debug, str::FromStr};
use structopt::StructOpt;
use thiserror::Error;

/// A common result to be returned to users
Expand Down Expand Up @@ -42,7 +42,7 @@ pub enum Error {
}

/// Types of Keys used by the blockchain
#[derive(Clone, Copy, Debug, StructOpt)]
#[derive(Clone, Copy, Debug, Parser)]
pub enum KeyType {
Ed25519,
X25519,
Expand All @@ -61,7 +61,7 @@ impl FromStr for KeyType {
}

/// Types of encodings used by the blockchain
#[derive(Clone, Copy, Debug, StructOpt)]
#[derive(Clone, Copy, Debug, Parser)]
pub enum EncodingType {
/// Binary Canonical Serialization
BCS,
Expand All @@ -85,17 +85,17 @@ impl FromStr for EncodingType {
}

/// An insertable option for use with prompts.
#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct PromptOptions {
/// Assume yes for all yes/no prompts
#[structopt(long)]
#[clap(long)]
pub assume_yes: bool,
}

/// An insertable option for use with encodings.
#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct EncodingOptions {
/// Encoding of data as `base64`, `bcs`, or `hex`
#[structopt(long, default_value = "hex")]
#[clap(long, default_value = "hex")]
pub encoding: EncodingType,
}
7 changes: 4 additions & 3 deletions crates/aptos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ pub mod common;
pub mod op;

use crate::common::types::{CliResult, Error};
use structopt::StructOpt;
use clap::Parser;

/// CLI tool for interacting with the Aptos blockchain and nodes
///
#[derive(Debug, StructOpt)]
#[structopt(name = "aptos")]
#[derive(Debug, Parser)]
#[clap(name = "aptos")]
pub enum Tool {
#[clap(subcommand)]
Op(op::OpTool),
}

Expand Down
4 changes: 2 additions & 2 deletions crates/aptos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#![forbid(unsafe_code)]

use aptos::Tool;
use clap::Parser;
use std::process::exit;
use structopt::StructOpt;

#[tokio::main]
async fn main() {
// Run the corresponding tools
let result = Tool::from_args().execute().await;
let result = Tool::parse().execute().await;

// At this point, we'll want to print and determine whether to exit for an error code
match result {
Expand Down
26 changes: 13 additions & 13 deletions crates/aptos/src/op/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,44 @@ use aptos_crypto::{
ed25519, ed25519::Ed25519PrivateKey, x25519, PrivateKey, Uniform, ValidCryptoMaterial,
ValidCryptoMaterialStringExt,
};
use clap::{Parser, Subcommand};
use rand::SeedableRng;
use std::{
fs::File,
io::Write,
path::{Path, PathBuf},
};
use structopt::StructOpt;

pub const PUBLIC_KEY_EXTENSION: &str = ".pub";

/// CLI tool for generating, inspecting, and interacting with keys.
#[derive(Debug, StructOpt)]
#[derive(Debug, Subcommand)]
pub enum KeyTool {
Generate(GenerateKey),
}

impl KeyTool {
pub async fn execute(self) -> CliResult {
match self {
KeyTool::Generate(generate) => to_common_success_result(generate.execute()),
KeyTool::Generate(tool) => to_common_success_result(tool.execute()),
}
}
}

/// Generates a `x25519` or `ed25519` key.
///
/// This can be used for generating an identity.
#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct GenerateKey {
/// Key type: `x25519` or `ed25519`
#[structopt(long, default_value = "ed25519")]
#[clap(long, default_value = "ed25519")]
key_type: KeyType,
#[structopt(flatten)]
#[clap(flatten)]
save_params: SaveKey,
}

impl GenerateKey {
fn execute(self) -> Result<(), Error> {
pub(crate) fn execute(self) -> Result<(), Error> {
self.save_params.check_key_file()?;

// Generate a ed25519 key
Expand Down Expand Up @@ -78,7 +78,7 @@ impl GenerateKey {
key_file = key_file.to_str().unwrap(),
encoding_type = encoding_type,
);
let command = GenerateKey::from_iter(args.split_whitespace());
let command = GenerateKey::parse_from(args.split_whitespace());
command.execute()?;
Ok((
load_key(key_file, encoding_type)?,
Expand All @@ -100,7 +100,7 @@ impl GenerateKey {
key_file = key_file.to_str().unwrap(),
encoding_type = encoding_type,
);
let command = GenerateKey::from_iter(args.split_whitespace());
let command = GenerateKey::parse_from(args.split_whitespace());
command.execute()?;
Ok((
load_key(key_file, encoding_type)?,
Expand All @@ -118,14 +118,14 @@ impl GenerateKey {
}
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct SaveKey {
/// Private key output file name. Public key will be saved to <key-file>.pub
#[structopt(long, parse(from_os_str))]
#[clap(long, parse(from_os_str))]
key_file: PathBuf,
#[structopt(flatten)]
#[clap(flatten)]
encoding_options: EncodingOptions,
#[structopt(flatten)]
#[clap(flatten)]
prompt_options: PromptOptions,
}

Expand Down
7 changes: 4 additions & 3 deletions crates/aptos/src/op/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
//!
use crate::CliResult;
use structopt::StructOpt;
use clap::{ArgEnum, Subcommand};

pub mod key;

/// CLI tool for performing operational tasks
///
#[derive(Debug, StructOpt)]
#[derive(Debug, ArgEnum, Subcommand)]
pub enum OpTool {
#[clap(subcommand)]
Key(key::KeyTool),
}

impl OpTool {
pub async fn execute(self) -> CliResult {
match self {
OpTool::Key(key_tool) => key_tool.execute().await,
OpTool::Key(tool) => tool.execute().await,
}
}
}

0 comments on commit ca10da0

Please sign in to comment.