Add command plumbing

This commit is contained in:
Michael Vines
2019-03-15 10:54:54 -07:00
parent c8bbca08f8
commit 1d876df8b3
8 changed files with 277 additions and 22 deletions

View File

@@ -1,40 +1,76 @@
#[macro_use]
extern crate lazy_static;
use clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg, SubCommand};
//use clap::{crate_description, crate_version, load_yaml, App, AppSettings};
use std::error;
use std::time::Duration;
use solana_sdk::pubkey::Pubkey;
const TARGET: &str = env!("TARGET");
const BUILD_SECONDS_SINCE_UNIX_EPOCH: &str = env!("BUILD_SECONDS_SINCE_UNIX_EPOCH");
mod build_env;
mod command;
mod config;
mod defaults;
mod update_manifest;
fn main() -> Result<(), Box<dyn error::Error>> {
fn main() -> Result<(), String> {
solana_logger::setup();
let matches = App::new(crate_name!())
.about(crate_description!())
.version(crate_version!())
.setting(AppSettings::ArgRequiredElseHelp)
.setting(AppSettings::SubcommandRequiredElseHelp)
.arg({
let arg = Arg::with_name("config_file")
.short("c")
.long("config")
.value_name("PATH")
.takes_value(true)
.help("Configuration file to use");
match *defaults::CONFIG_FILE {
Some(ref config_file) => arg.default_value(&config_file),
None => arg.required(true),
}
})
.subcommand(
SubCommand::with_name("init")
.about("initializes a new installation")
.setting(AppSettings::DisableVersion)
.arg({
let arg = Arg::with_name("data_dir")
.short("d")
.long("data_dir")
.value_name("PATH")
.takes_value(true)
.help("Directory to store install data");
match *defaults::DATA_DIR {
Some(ref data_dir) => arg.default_value(&data_dir),
None => arg.required(true),
}
})
.arg(
Arg::with_name("json_rpc_url")
.short("u")
.long("url")
.value_name("URL")
.takes_value(true)
.default_value("https://api.testnet.solana.com/")
.default_value(defaults::JSON_RPC_URL)
.help("JSON RPC URL for the solana cluster"),
)
.arg(
Arg::with_name("update_pubkey")
.arg({
let arg = Arg::with_name("update_pubkey")
.short("p")
.long("pubkey")
.value_name("PUBKEY")
.takes_value(true)
.default_value("Solana-managed update manifest")
.help("Public key of the update manifest"),
),
.validator(|value| match value.parse::<Pubkey>() {
Ok(_) => Ok(()),
Err(err) => Err(format!("{:?}", err)),
})
.help("Public key of the update manifest");
match defaults::update_pubkey(build_env::TARGET) {
Some(default_value) => arg.default_value(default_value),
None => arg.required(true),
}
}),
)
.subcommand(
SubCommand::with_name("info")
@@ -91,11 +127,38 @@ fn main() -> Result<(), Box<dyn error::Error>> {
)
.get_matches();
println!("TARGET={}", TARGET);
println!(
"BUILD_SECONDS_SINCE_UNIX_EPOCH={:?}",
Duration::from_secs(u64::from_str_radix(BUILD_SECONDS_SINCE_UNIX_EPOCH, 10).unwrap())
);
println!("{:?}", matches);
Ok(())
let config_file = matches.value_of("config_file").unwrap();
match matches.subcommand() {
("init", Some(matches)) => {
let json_rpc_url = matches.value_of("json_rpc_url").unwrap();
let update_pubkey = matches
.value_of("update_pubkey")
.unwrap()
.parse::<Pubkey>()
.unwrap();
let data_dir = matches.value_of("data_dir").unwrap();
command::init(config_file, data_dir, json_rpc_url, &update_pubkey)
}
("info", Some(matches)) => {
let local_info_only = matches.is_present("local_info_only");
command::info(config_file, local_info_only)
}
("deploy", Some(matches)) => {
let download_url = matches.value_of("download_url").unwrap();
let update_manifest_keypair = matches.value_of("update_manifest_keypair").unwrap();
command::deploy(config_file, download_url, update_manifest_keypair)
}
("update", Some(_matches)) => command::update(config_file),
("run", Some(matches)) => {
let program_name = matches.value_of("program_name").unwrap();
let program_arguments = matches
.values_of("program_arguments")
.map(|iter| iter.collect())
.unwrap_or_else(|| vec![]);
command::run(config_file, program_name, program_arguments)
}
_ => unreachable!(),
}
}