mirror of
https://github.com/jwinterm/grin-wallet.git
synced 2026-10-17 15:21:39 +02:00
Rework directory checks/setups at launch (#318)
* rework setup code to ensure --top_level_dir flag works and all files are only output into the desired directory * check for existence of wallet configuration file in target directory before asking recovery phrase and password * test fixes and add test_mode flag to parse_init_args
This commit is contained in:
+29
-3
@@ -19,6 +19,7 @@ extern crate clap;
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
use crate::config::ConfigError;
|
||||
use crate::core::global;
|
||||
use crate::util::init_logger;
|
||||
use clap::App;
|
||||
@@ -27,6 +28,7 @@ use grin_wallet_impls::HTTPNodeClient;
|
||||
use grin_wallet_util::grin_core as core;
|
||||
use grin_wallet_util::grin_util as util;
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use grin_wallet::cmd;
|
||||
|
||||
@@ -80,6 +82,19 @@ fn real_main() -> i32 {
|
||||
};
|
||||
|
||||
let mut current_dir = None;
|
||||
let mut create_path = false;
|
||||
|
||||
if args.is_present("top_level_dir") {
|
||||
let res = args.value_of("top_level_dir");
|
||||
match res {
|
||||
Some(d) => {
|
||||
current_dir = Some(PathBuf::from(d));
|
||||
}
|
||||
None => {
|
||||
warn!("Argument --top_level_dir needs a value. Defaulting to current directory")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// special cases for certain lifecycle commands
|
||||
match args.subcommand() {
|
||||
@@ -89,15 +104,26 @@ fn real_main() -> i32 {
|
||||
panic!("Error creating config file: {}", e);
|
||||
}));
|
||||
}
|
||||
create_path = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Load relevant config, try and load a wallet config file
|
||||
// Use defaults for configuration if config file not found anywhere
|
||||
let mut config = config::initial_setup_wallet(&chain_type, current_dir).unwrap_or_else(|e| {
|
||||
panic!("Error loading wallet configuration: {}", e);
|
||||
});
|
||||
let mut config = match config::initial_setup_wallet(&chain_type, current_dir, create_path) {
|
||||
Ok(c) => c,
|
||||
Err(e) => match e {
|
||||
ConfigError::PathNotFoundError(m) => {
|
||||
println!("Wallet configuration not found at {}. (Run `grin-wallet init` to create a new wallet)", m);
|
||||
return 0;
|
||||
}
|
||||
m => {
|
||||
println!("Unable to load wallet configuration: {} (Run `grin-wallet init` to create a new wallet)", m);
|
||||
return 0;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
//config.members.as_mut().unwrap().wallet.chain_type = Some(chain_type);
|
||||
|
||||
|
||||
+10
-2
@@ -19,7 +19,7 @@ use crate::util::{Mutex, ZeroingString};
|
||||
/// Argument parsing and error handling for wallet commands
|
||||
use clap::ArgMatches;
|
||||
use failure::Fail;
|
||||
use grin_wallet_config::{TorConfig, WalletConfig};
|
||||
use grin_wallet_config::{config_file_exists, TorConfig, WalletConfig};
|
||||
use grin_wallet_controller::command;
|
||||
use grin_wallet_controller::{Error, ErrorKind};
|
||||
use grin_wallet_impls::tor::config::is_tor_address;
|
||||
@@ -58,6 +58,8 @@ pub enum ParseError {
|
||||
ArgumentError(String),
|
||||
#[fail(display = "Parsing IO error: {}", _0)]
|
||||
IOError(String),
|
||||
#[fail(display = "Wallet configuration already exists: {}", _0)]
|
||||
WalletExists(String),
|
||||
#[fail(display = "User Cancelled")]
|
||||
CancelledError,
|
||||
}
|
||||
@@ -298,6 +300,7 @@ pub fn parse_init_args<L, C, K>(
|
||||
config: &WalletConfig,
|
||||
g_args: &command::GlobalArgs,
|
||||
args: &ArgMatches,
|
||||
test_mode: bool,
|
||||
) -> Result<command::InitArgs, ParseError>
|
||||
where
|
||||
DefaultWalletImpl<'static, C>: WalletInst<'static, L, C, K>,
|
||||
@@ -305,6 +308,10 @@ where
|
||||
C: NodeClient + 'static,
|
||||
K: keychain::Keychain + 'static,
|
||||
{
|
||||
if config_file_exists(&config.data_file_dir) && !test_mode {
|
||||
return Err(ParseError::WalletExists(config.data_file_dir.clone()));
|
||||
}
|
||||
|
||||
let list_length = match args.is_present("short_wordlist") {
|
||||
false => 32,
|
||||
true => 16,
|
||||
@@ -951,7 +958,8 @@ where
|
||||
wallet.clone(),
|
||||
&wallet_config,
|
||||
&global_wallet_args,
|
||||
&args
|
||||
&args,
|
||||
test_mode,
|
||||
));
|
||||
command::init(wallet, &global_wallet_args, a)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user