Skip to content

Commit

Permalink
Merge branch 'master' into type-tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar authored Dec 25, 2020
2 parents 8a1c855 + c235ecd commit 0bd7690
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 34 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

8 changes: 6 additions & 2 deletions cmd/generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@

use anyhow::Result;
use scmd::CmdContext;
use starcoin_config::StarcoinOpt;
use starcoin_config::{StarcoinOpt, APP_VERSION, CRATE_VERSION};
use starcoin_generator::cli_state::CliState;
use starcoin_generator::gen_data::GenDataCommand;
use starcoin_generator::gen_genesis::GenGenesisCommand;
use starcoin_generator::gen_genesis_config::GenGenesisConfigCommand;
use starcoin_logger::prelude::*;

fn run() -> Result<()> {
let context = CmdContext::<CliState, StarcoinOpt>::with_state(CliState);
let context = CmdContext::<CliState, StarcoinOpt>::with_state(
CRATE_VERSION,
Some(APP_VERSION.as_str()),
CliState,
);
context
.command(GenGenesisConfigCommand)
.command(GenGenesisCommand)
Expand Down
4 changes: 3 additions & 1 deletion cmd/starcoin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use scmd::error::CmdError;
use scmd::CmdContext;
use starcoin_cmd::*;
use starcoin_cmd::{CliState, StarcoinOpt};
use starcoin_config::Connect;
use starcoin_config::{Connect, APP_VERSION, CRATE_VERSION};
use starcoin_logger::prelude::*;
use starcoin_node::crash_handler;
use starcoin_node_api::errors::NodeStartError;
Expand All @@ -20,6 +20,8 @@ static EXIT_CODE_NEED_HELP: i32 = 120;
fn run() -> Result<()> {
let logger_handle = starcoin_logger::init();
let context = CmdContext::<CliState, StarcoinOpt>::with_default_action(
CRATE_VERSION,
Some(APP_VERSION.as_str()),
|opt| -> Result<CliState> {
info!("Starcoin opts: {:?}", opt);
let connect = opt.connect.as_ref().unwrap_or(&Connect::IPC(None));
Expand Down
1 change: 0 additions & 1 deletion commons/scmd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ serde = { version = "1.0", features = ["derive"] }
rustyline = "7.1.0"
structopt = "0.3.21"
clap = "2.33.3"
git-version = "0.3.4"
serde_json = { version="1.0", features = ["arbitrary_precision"]}
rust-flatten-json = "0.2.0"
cli-table = "0.3.2"
Expand Down
2 changes: 2 additions & 0 deletions commons/scmd/examples/hello_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ struct TestOpts {

pub(crate) fn init_context() -> CmdContext<Counter, GlobalOpts> {
let context = CmdContext::<Counter, GlobalOpts>::with_default_action(
"0.1",
None,
|global_opt| -> Result<Counter> { Ok(Counter::new(global_opt.counter)) },
|_app, _opt, _state| {
let running = Arc::new(AtomicBool::new(true));
Expand Down
57 changes: 31 additions & 26 deletions commons/scmd/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use crate::error::CmdError;
use crate::{print_action_result, Command, CommandAction, CommandExec, OutputFormat};
use anyhow::Result;
use clap::{crate_authors, crate_version, App, Arg, SubCommand};
use git_version::git_version;
use clap::{crate_authors, App, Arg, SubCommand};
use once_cell::sync::Lazy;
use serde_json::Value;
use std::collections::HashMap;
Expand Down Expand Up @@ -33,16 +32,6 @@ pub static DEFAULT_CONSOLE_CONFIG: Lazy<ConsoleConfig> = Lazy::new(|| {
});

static OUTPUT_FORMAT_ARG: &str = "output-format";
static VERSION: &str = crate_version!();
static GIT_VERSION: &str = git_version!(fallback = "unknown");

static LONG_VERSION: Lazy<String> = Lazy::new(|| {
if GIT_VERSION != "unknown" {
format!("{} (build:{})", VERSION, GIT_VERSION)
} else {
VERSION.to_string()
}
});

pub struct CmdContext<State, GlobalOpt>
where
Expand All @@ -65,35 +54,50 @@ where
GlobalOpt: StructOpt + 'static,
{
/// Init new CmdContext with State
pub fn with_state(state: State) -> Self {
Self::with_initializer(|_opts| Ok(state))
pub fn with_state(
version: &'static str,
long_version: Option<&'static str>,
state: State,
) -> Self {
Self::with_initializer(version, long_version, |_opts| Ok(state))
}

/// Init new CmdContext with state_initializer
/// default action is print help.
pub fn with_initializer<I>(state_initializer: I) -> Self
pub fn with_initializer<I>(
version: &'static str,
long_version: Option<&'static str>,
state_initializer: I,
) -> Self
where
I: FnOnce(&GlobalOpt) -> Result<State> + 'static,
{
Self::with_default_action(state_initializer, |mut app, _opt, _state| {
app.print_long_help().expect("print help should success.");
})
Self::with_default_action(
version,
long_version,
state_initializer,
|mut app, _opt, _state| {
app.print_long_help().expect("print help should success.");
},
)
}

/// Init new CmdContext with state_initializer, and default_action, console_start_action, console_quit_action
/// Init new CmdContext with state_initializer, and default_action
/// default_action executed when no subcommand is provided.
/// console_start_action executed when start a console.
/// console_quit_action executed when input quit subcommand at console.
pub fn with_default_action<I, D>(state_initializer: I, default_action: D) -> Self
pub fn with_default_action<I, D>(
version: &'static str,
long_version: Option<&'static str>,
state_initializer: I,
default_action: D,
) -> Self
where
I: FnOnce(&GlobalOpt) -> Result<State> + 'static,
D: FnOnce(App, GlobalOpt, State) + 'static,
{
let mut app = GlobalOpt::clap();
//TODO pass version as argument
app = app
.version(VERSION)
.long_version(LONG_VERSION.as_str())
.version(version)
.long_version(long_version.unwrap_or(version))
.arg(
Arg::with_name(OUTPUT_FORMAT_ARG)
.short("o")
Expand Down Expand Up @@ -361,7 +365,8 @@ where
app.print_long_help().expect("print help should success.");
}
"version" => {
println!("{}", LONG_VERSION.as_str());
let mut out = std::io::stdout();
let _ = app.write_long_version(&mut out);
}
"output" => {
if params.len() == 1 {
Expand Down
7 changes: 5 additions & 2 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ pub use storage_config::{RocksdbConfig, StorageConfig};
pub use sync_config::SyncMode;
pub use txpool_config::TxPoolConfig;

static CRATE_VERSION: &str = crate_version!();
static GIT_VERSION: &str = git_version!(fallback = "unknown");
pub static CRATE_VERSION: &str = crate_version!();
pub static GIT_VERSION: &str = git_version!(
args = ["--tags", "--dirty", "--always"],
fallback = "unknown"
);

pub static APP_NAME: &str = "starcoin";
pub static APP_VERSION: Lazy<String> = Lazy::new(|| {
Expand Down
7 changes: 6 additions & 1 deletion testsuite/tests/steps/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use scmd::CmdContext;
use serde_json::Value;
use starcoin_cmd::add_command;
use starcoin_cmd::{CliState, StarcoinOpt};
use starcoin_config::{APP_VERSION, CRATE_VERSION};
use starcoin_logger::prelude::*;
use static_assertions::_core::time::Duration;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -37,7 +38,11 @@ pub fn steps() -> Steps<MyWorld> {
Some(Duration::from_secs(5)),
None,
);
let context = CmdContext::<CliState, StarcoinOpt>::with_state(state);
let context = CmdContext::<CliState, StarcoinOpt>::with_state(
CRATE_VERSION,
Some(APP_VERSION.as_str()),
state,
);
// get last cmd result as current parameter
let mut vec = vec!["starcoin"];
let parameters = get_command_args(world, (*args[1]).parse().unwrap());
Expand Down

0 comments on commit 0bd7690

Please sign in to comment.