Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit d84f367

Browse files
authored
Extract duplicate clap helpers into clap-utils (#6812)
1 parent 95d6586 commit d84f367

21 files changed

+89
-95
lines changed

Cargo.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

archiver/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ homepage = "https://solana.com/"
1010
[dependencies]
1111
clap = "2.33.0"
1212
console = "0.9.1"
13+
solana-clap-utils = { path = "../clap-utils", version = "0.21.0" }
1314
solana-core = { path = "../core", version = "0.21.0" }
1415
solana-logger = { path = "../logger", version = "0.21.0" }
1516
solana-metrics = { path = "../metrics", version = "0.21.0" }

archiver/src/main.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clap::{crate_description, crate_name, crate_version, App, Arg};
22
use console::style;
3+
use solana_clap_utils::input_validators::is_keypair;
34
use solana_core::{
45
archiver::Archiver,
56
cluster_info::{Node, VALIDATOR_PORT_RANGE},
@@ -11,13 +12,6 @@ use solana_sdk::{
1112
};
1213
use std::{net::SocketAddr, path::PathBuf, process::exit, sync::Arc};
1314

14-
// Return an error if a keypair file cannot be parsed.
15-
fn is_keypair(string: String) -> Result<(), String> {
16-
read_keypair_file(&string)
17-
.map(|_| ())
18-
.map_err(|err| format!("{:?}", err))
19-
}
20-
2115
fn main() {
2216
solana_logger::setup();
2317

clap-utils/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "solana-clap-utils"
3+
version = "0.21.0"
4+
description = "Solana utilities for the clap"
5+
authors = ["Solana Maintainers <[email protected]>"]
6+
repository = "https://github.com/solana-labs/solana"
7+
license = "Apache-2.0"
8+
homepage = "https://solana.com/"
9+
edition = "2018"
10+
11+
[dependencies]
12+
clap = "2.33.0"
13+
semver = "0.9.0"
14+
solana-sdk = { path = "../sdk", version = "0.21.0" }
15+
url = "2.1.0"
16+
17+
[lib]
18+
name = "solana_clap_utils"
File renamed without changes.

cli/src/input_validators.rs renamed to clap-utils/src/input_validators.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,17 @@ pub fn is_url(string: String) -> Result<(), String> {
3434
Err(err) => Err(format!("{:?}", err)),
3535
}
3636
}
37+
38+
pub fn is_semver(semver: &str) -> Result<(), String> {
39+
match semver::Version::parse(&semver) {
40+
Ok(_) => Ok(()),
41+
Err(err) => Err(format!("{:?}", err)),
42+
}
43+
}
44+
45+
pub fn is_release_channel(channel: &str) -> Result<(), String> {
46+
match channel {
47+
"edge" | "beta" | "stable" => Ok(()),
48+
_ => Err(format!("Invalid release channel {}", channel)),
49+
}
50+
}

clap-utils/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod input_parsers;
2+
pub mod input_validators;

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ serde_derive = "1.0.102"
2727
serde_json = "1.0.41"
2828
serde_yaml = "0.8.11"
2929
solana-budget-api = { path = "../programs/budget_api", version = "0.21.0" }
30+
solana-clap-utils = { path = "../clap-utils", version = "0.21.0" }
3031
solana-client = { path = "../client", version = "0.21.0" }
3132
solana-config-api = { path = "../programs/config_api", version = "0.21.0" }
3233
solana-drone = { path = "../drone", version = "0.21.0" }

cli/src/cli.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::{
2-
cluster_query::*, display::println_name_value, input_parsers::*, input_validators::*, stake::*,
3-
storage::*, validator_info::*, vote::*,
2+
cluster_query::*, display::println_name_value, stake::*, storage::*, validator_info::*, vote::*,
43
};
54
use chrono::prelude::*;
65
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
76
use log::*;
87
use num_traits::FromPrimitive;
98
use serde_json::{self, json, Value};
9+
1010
use solana_budget_api::budget_instruction::{self, BudgetError};
11+
use solana_clap_utils::{input_parsers::*, input_validators::*};
1112
use solana_client::{client_error::ClientError, rpc_client::RpcClient};
1213
#[cfg(not(test))]
1314
use solana_drone::drone::request_airdrop_transaction;
@@ -32,6 +33,7 @@ use solana_sdk::{
3233
use solana_stake_api::stake_state::{Lockup, StakeAuthorize};
3334
use solana_storage_api::storage_instruction::StorageAccountType;
3435
use solana_vote_api::vote_state::VoteAuthorize;
36+
3537
use std::{
3638
fs::File,
3739
io::{Read, Write},

cli/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ pub mod cli;
55
pub mod cluster_query;
66
pub mod config;
77
pub mod display;
8-
pub mod input_parsers;
9-
pub mod input_validators;
108
pub mod stake;
119
pub mod storage;
1210
pub mod validator_info;

cli/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use clap::{crate_description, crate_name, crate_version, Arg, ArgGroup, ArgMatches, SubCommand};
22
use console::style;
3+
4+
use solana_clap_utils::input_validators::is_url;
35
use solana_cli::{
46
cli::{app, parse_command, process_command, CliCommandInfo, CliConfig, CliError},
57
config::{self, Config},
68
display::{println_name_value, println_name_value_or},
7-
input_validators::is_url,
89
};
910
use solana_sdk::signature::read_keypair_file;
11+
1012
use std::error;
1113

1214
fn parse_settings(matches: &ArgMatches<'_>) -> Result<bool, Box<dyn error::Error>> {

cli/src/stake.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
use crate::{
2-
cli::{
3-
build_balance_message, check_account_for_fee, check_unique_pubkeys,
4-
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
5-
ProcessResult,
6-
},
7-
input_parsers::*,
8-
input_validators::*,
1+
use crate::cli::{
2+
build_balance_message, check_account_for_fee, check_unique_pubkeys,
3+
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult,
94
};
105
use clap::{App, Arg, ArgMatches, SubCommand};
116
use console::style;
7+
use solana_clap_utils::{input_parsers::*, input_validators::*};
128
use solana_client::rpc_client::RpcClient;
139
use solana_sdk::signature::Keypair;
1410
use solana_sdk::{

cli/src/storage.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use crate::{
2-
cli::{
3-
check_account_for_fee, check_unique_pubkeys, log_instruction_custom_error, CliCommand,
4-
CliCommandInfo, CliConfig, CliError, ProcessResult,
5-
},
6-
input_parsers::*,
7-
input_validators::*,
1+
use crate::cli::{
2+
check_account_for_fee, check_unique_pubkeys, log_instruction_custom_error, CliCommand,
3+
CliCommandInfo, CliConfig, CliError, ProcessResult,
84
};
95
use clap::{App, Arg, ArgMatches, SubCommand};
6+
use solana_clap_utils::{input_parsers::*, input_validators::*};
107
use solana_client::rpc_client::RpcClient;
118
use solana_sdk::signature::Keypair;
129
use solana_sdk::{

cli/src/validator_info.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
use crate::{
22
cli::{check_account_for_fee, CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult},
33
display::println_name_value,
4-
input_parsers::pubkey_of,
5-
input_validators::{is_pubkey, is_url},
64
};
75
use bincode::deserialize;
86
use clap::{App, Arg, ArgMatches, SubCommand};
97
use reqwest::Client;
108
use serde_derive::{Deserialize, Serialize};
119
use serde_json::{Map, Value};
10+
11+
use solana_clap_utils::{
12+
input_parsers::pubkey_of,
13+
input_validators::{is_pubkey, is_url},
14+
};
1215
use solana_client::rpc_client::RpcClient;
1316
use solana_config_api::{config_instruction, get_config_data, ConfigKeys, ConfigState};
1417
use solana_sdk::{
@@ -19,6 +22,7 @@ use solana_sdk::{
1922
signature::{Keypair, KeypairUtil},
2023
transaction::Transaction,
2124
};
25+
2226
use std::error;
2327

2428
pub const MAX_SHORT_FIELD_LENGTH: usize = 70;

cli/src/vote.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
use crate::{
2-
cli::{
3-
build_balance_message, check_account_for_fee, check_unique_pubkeys,
4-
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError,
5-
ProcessResult,
6-
},
7-
input_parsers::*,
8-
input_validators::*,
1+
use crate::cli::{
2+
build_balance_message, check_account_for_fee, check_unique_pubkeys,
3+
log_instruction_custom_error, CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult,
94
};
105
use clap::{value_t_or_exit, App, Arg, ArgMatches, SubCommand};
6+
use solana_clap_utils::{input_parsers::*, input_validators::*};
117
use solana_client::rpc_client::RpcClient;
128
use solana_sdk::signature::Keypair;
139
use solana_sdk::{

gossip/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ homepage = "https://solana.com/"
1010

1111
[dependencies]
1212
clap = "2.33.0"
13+
solana-clap-utils = { path = "../clap-utils", version = "0.21.0" }
1314
solana-core = { path = "../core", version = "0.21.0" }
1415
solana-client = { path = "../client", version = "0.21.0" }
1516
solana-logger = { path = "../logger", version = "0.21.0" }

gossip/src/main.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@ use clap::{
44
crate_description, crate_name, crate_version, value_t_or_exit, App, AppSettings, Arg,
55
SubCommand,
66
};
7+
use solana_clap_utils::input_validators::is_pubkey;
78
use solana_client::rpc_client::RpcClient;
89
use solana_core::{contact_info::ContactInfo, gossip_service::discover};
910
use solana_sdk::pubkey::Pubkey;
1011
use std::error;
1112
use std::net::SocketAddr;
1213
use std::process::exit;
1314

14-
fn is_pubkey(pubkey: String) -> Result<(), String> {
15-
match pubkey.parse::<Pubkey>() {
16-
Ok(_) => Ok(()),
17-
Err(err) => Err(format!("{:?}", err)),
18-
}
19-
}
20-
2115
fn main() -> Result<(), Box<dyn error::Error>> {
2216
solana_logger::setup_with_filter("solana=info");
2317

install/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ lazy_static = "1.4.0"
2323
log = "0.4.8"
2424
nix = "0.15.0"
2525
reqwest = { version = "0.9.22", default-features = false, features = ["rustls-tls"] }
26-
semver = "0.9.0"
2726
serde = "1.0.102"
2827
serde_derive = "1.0.102"
2928
serde_yaml = "0.8.11"
3029
sha2 = "0.8.0"
30+
solana-clap-utils = { path = "../clap-utils", version = "0.21.0" }
3131
solana-client = { path = "../client", version = "0.21.0" }
3232
solana-config-api = { path = "../programs/config_api", version = "0.21.0" }
3333
solana-logger = { path = "../logger", version = "0.21.0" }

install/src/lib.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
extern crate lazy_static;
33

44
use clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg, SubCommand};
5+
use solana_clap_utils::input_validators::{is_pubkey, is_release_channel, is_semver, is_url};
56
use solana_sdk::pubkey::Pubkey;
67

78
mod build_env;
@@ -11,42 +12,6 @@ mod defaults;
1112
mod stop_process;
1213
mod update_manifest;
1314

14-
// Return an error if a url cannot be parsed.
15-
fn is_url(string: String) -> Result<(), String> {
16-
match url::Url::parse(&string) {
17-
Ok(url) => {
18-
if url.has_host() {
19-
Ok(())
20-
} else {
21-
Err("no host provided".to_string())
22-
}
23-
}
24-
Err(err) => Err(format!("{:?}", err)),
25-
}
26-
}
27-
28-
// Return an error if a pubkey cannot be parsed.
29-
fn is_pubkey(string: String) -> Result<(), String> {
30-
match string.parse::<Pubkey>() {
31-
Ok(_) => Ok(()),
32-
Err(err) => Err(format!("{:?}", err)),
33-
}
34-
}
35-
36-
fn is_semver(semver: &str) -> Result<(), String> {
37-
match semver::Version::parse(&semver) {
38-
Ok(_) => Ok(()),
39-
Err(err) => Err(format!("{:?}", err)),
40-
}
41-
}
42-
43-
fn is_release_channel(channel: &str) -> Result<(), String> {
44-
match channel {
45-
"edge" | "beta" | "stable" => Ok(()),
46-
_ => Err(format!("Invalid release channel {}", channel)),
47-
}
48-
}
49-
5015
pub fn main() -> Result<(), String> {
5116
solana_logger::setup();
5217

validator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ log = "0.4.8"
1717
indicatif = "0.13.0"
1818
reqwest = { version = "0.9.22", default-features = false }
1919
serde_json = "1.0.41"
20+
solana-clap-utils = { path = "../clap-utils", version = "0.21.0" }
2021
solana-client = { path = "../client", version = "0.21.0" }
2122
solana-core = { path = "../core", version = "0.21.0" }
2223
solana-drone = { path = "../drone", version = "0.21.0" }

0 commit comments

Comments
 (0)