added cli files overlooked in previous commit

This commit is contained in:
lunar-mining
2021-07-17 13:21:43 +02:00
parent 76cb1e37c0
commit 02f619a588
4 changed files with 549 additions and 0 deletions

157
src/cli/cli_config.rs Normal file
View File

@@ -0,0 +1,157 @@
use crate::util::join_config_path;
use crate::Result;
use serde::{Deserialize, Serialize};
use std::str;
use std::fs;
use std::path::PathBuf;
#[derive(Serialize, Deserialize, Debug)]
pub struct DrkConfig {
#[serde(default)]
pub rpc_url: String,
#[serde(default)]
pub log_path: String,
}
impl DrkConfig {
pub fn load(path: PathBuf) -> Result<Self> {
let toml = fs::read(&path)?;
let str_buff = str::from_utf8(&toml)?;
let config: Self = toml::from_str(str_buff)?;
Ok(config)
}
pub fn load_default(path: PathBuf) -> Result<Self> {
let toml = Self::default();
let config_file = toml::to_string(&toml)?;
fs::write(&path, &config_file)?;
let config = Self::load(path)?;
Ok(config)
}
}
impl Default for DrkConfig {
fn default() -> Self {
let rpc_url = String::from("http://127.0.0.1:8000");
let log_path = String::from("/tmp/drk_cli.log");
Self { rpc_url, log_path }
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DarkfidConfig {
#[serde(default)]
#[serde(rename = "connect_url")]
pub connect_url: String,
#[serde(default)]
#[serde(rename = "subscriber_url")]
pub subscriber_url: String,
#[serde(default)]
#[serde(rename = "rpc_url")]
pub rpc_url: String,
#[serde(default)]
#[serde(rename = "database_path")]
pub database_path: String,
#[serde(default)]
#[serde(rename = "log_path")]
pub log_path: String,
#[serde(default)]
#[serde(rename = "password")]
pub password: String,
}
impl DarkfidConfig {
pub fn load(path: PathBuf) -> Result<Self> {
let toml = fs::read(&path)?;
let str_buff = str::from_utf8(&toml)?;
let config: Self = toml::from_str(str_buff)?;
Ok(config)
}
pub fn load_default(path: PathBuf) -> Result<Self> {
let toml = Self::default();
let config_file = toml::to_string(&toml)?;
fs::write(&path, &config_file)?;
let config = Self::load(path)?;
Ok(config)
}
}
impl Default for DarkfidConfig {
fn default() -> Self {
let connect_url = String::from("127.0.0.1:3333");
let subscriber_url = String::from("127.0.0.1:4444");
let rpc_url = String::from("127.0.0.1:8000");
let database_path = String::from("database_client.db");
let database_path = join_config_path(&PathBuf::from(database_path))
.expect("error during join database_path to config path");
let database_path = String::from(
database_path
.to_str()
.expect("error convert Path to String"),
);
let log_path = String::from("/tmp/darkfid_service_daemon.log");
let password = String::new();
Self {
connect_url,
subscriber_url,
rpc_url,
database_path,
log_path,
password,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GatewaydConfig {
#[serde(default)]
#[serde(rename = "connect_url")]
pub accept_url: String,
#[serde(default)]
#[serde(rename = "publisher_url")]
pub publisher_url: String,
#[serde(default)]
#[serde(rename = "database_path")]
pub database_path: String,
#[serde(default)]
#[serde(rename = "log_path")]
pub log_path: String,
}
impl GatewaydConfig {
pub fn load(path: PathBuf) -> Result<Self> {
let toml = fs::read(&path)?;
let str_buff = str::from_utf8(&toml)?;
let config: Self = toml::from_str(str_buff)?;
Ok(config)
}
pub fn load_default(path: PathBuf) -> Result<Self> {
let toml = Self::default();
let config_file = toml::to_string(&toml)?;
fs::write(&path, &config_file)?;
let config = Self::load(path)?;
Ok(config)
}
}
impl Default for GatewaydConfig {
fn default() -> Self {
let accept_url = String::from("127.0.0.1:3333");
let publisher_url = String::from("127.0.0.1:4444");
let database_path = String::from("gatewayd.db");
let log_path = String::from("/tmp/gatewayd.log");
Self { accept_url, publisher_url, database_path, log_path }
}
}

121
src/cli/darkfid_cli.rs Normal file
View File

@@ -0,0 +1,121 @@
//use super::cli_config::DarkfidCliConfig;
use crate::Result;
use clap::{App, Arg};
pub struct DarkfidCli {
//pub change_config: bool,
pub verbose: bool,
}
impl DarkfidCli {
pub fn load() -> Result<Self> {
let app = App::new("Darkfi Daemon CLI")
.version("0.1.0")
.author("Amir Taaki <amir@dyne.org>")
.about("Run Darkfi Daemon")
.arg(
Arg::new("verbose")
.short('v')
.help_heading(Some("Increase verbosity"))
.long("verbose")
.takes_value(false),
)
//.subcommand(
// App::new("config")
// .about("Configuration settings")
// .aliases(&["get", "set"])
// .setting(AppSettings::SubcommandRequiredElseHelp)
// .subcommand(App::new("get").about("Get configuration settings"))
// .subcommand(
// App::new("set")
// .about("Set configuration settings")
// .args(&[
// Arg::new("connect_url")
// .about("Set Connect Url")
// .long("connect-url")
// .takes_value(true),
// Arg::new("subscriber_url")
// .about("Set Subscriber Url")
// .long("subscriber-url")
// .takes_value(true),
// Arg::new("rpc_url")
// .about("Set RPC Url")
// .long("rpc-url")
// .takes_value(true),
// Arg::new("database_path")
// .about("Set Database Path")
// .long("database-path")
// .takes_value(true),
// Arg::new("log_path")
// .about("Set Log Path")
// .long("log-path")
// .takes_value(true),
// Arg::new("password")
// .about("Set password")
// .long("password")
// .takes_value(true),
// ])
// .setting(AppSettings::ArgRequiredElseHelp),
// ),
//)
.get_matches();
//let mut change_config = false;
let verbose = app.is_present("verbose");
//match app.subcommand_matches("config") {
// Some(config_sub) => match config_sub.subcommand() {
// Some(c) => match c {
// ("get", _) => {
// change_config = true;
// println!("Connect Url: {}", config.connect_url);
// println!("Subscriber Url: {}", config.subscriber_url);
// println!("RPC Url: {}", config.rpc_url);
// println!("Database path: {}", config.database_path);
// println!("Log Path: {}", config.log_path);
// if config.password.trim().is_empty() {
// println!("Password is empty. Please set a password.")
// };
// }
// ("set", c) => {
// change_config = true;
// if let Some(v) = c.value_of("connect_url") {
// config.connect_url = v.to_string();
// println!("Change Connect Url To {}", config.connect_url);
// }
// if let Some(v) = c.value_of("subscriber_url") {
// config.subscriber_url = v.to_string();
// println!("Change Subscriber Url To {}", config.subscriber_url);
// }
// if let Some(v) = c.value_of("rpc_url") {
// config.rpc_url = v.to_string();
// println!("Change RPC Url To {}", config.rpc_url);
// }
// if let Some(v) = c.value_of("database_path") {
// config.database_path = v.to_string();
// println!("Change Database Path To {}", config.database_path);
// }
// if let Some(v) = c.value_of("log_path") {
// config.log_path = v.to_string();
// println!("Change Log Path To {}", config.log_path);
// }
// if let Some(v) = c.value_of("password") {
// config.password = v.to_string();
// println!("Updated password. New password: {}", config.password);
// }
// }
// _ => {}
// },
// None => {}
// },
// None => {}
//}
Ok(Self {
//change_config,
verbose,
})
}
}

206
src/cli/drk_cli.rs Normal file
View File

@@ -0,0 +1,206 @@
//use super::cli_config::DrkCliConfig;
use crate::Result;
use clap::{App, Arg};
pub struct Transfer {
pub pub_key: String,
pub amount: String,
}
impl Transfer {
pub fn new() -> Self {
Self {
pub_key: String::new(),
amount: String::new(),
}
}
pub fn verfiy_amount(amount: &str) -> Result<()> {
if amount.parse::<u64>().is_ok() || amount.parse::<f64>().is_ok() {
Ok(())
} else {
let err = format!(
"Unable to parse input amount as integer or float: {}",
amount
);
Err(crate::Error::ParseFailed(Box::leak(err.into_boxed_str())))
}
}
}
pub struct DrkCli {
//pub change_config: bool,
pub verbose: bool,
pub cashier: bool,
pub wallet: bool,
pub key: bool,
pub info: bool,
pub hello: bool,
pub stop: bool,
pub transfer: Option<Transfer>,
}
impl DrkCli {
pub fn load() -> Result<Self> {
let app = App::new("Drk CLI")
.version("0.1.0")
.author("Amir Taaki <amir@dyne.org>")
.about("Run Drk Client")
.arg(
Arg::new("verbose")
.short('v')
.help_heading(Some("Increase verbosity"))
.long("verbose")
.takes_value(false),
)
.arg(
Arg::new("hello")
.long("hello")
.help_heading(Some("Test Hello"))
.takes_value(false),
)
.arg(
Arg::new("cashier")
.short('c')
.long("cashier")
.help_heading(Some("Create a cashier wallet"))
.takes_value(false),
)
.arg(
Arg::new("key")
.short('k')
.long("key")
.help_heading(Some("Test key"))
.takes_value(false),
)
.arg(
Arg::new("wallet")
.short('w')
.long("wallet")
.help_heading(Some("Create a new wallet"))
.takes_value(false),
)
.arg(
Arg::new("info")
.short('i')
.long("info")
.help_heading(Some("Request info from daemon"))
.takes_value(false),
)
.arg(
Arg::new("stop")
.short('s')
.long("stop")
.help_heading(Some("Send a stop signal to the daemon"))
.takes_value(false),
)
.subcommand(
App::new("transfer")
.about("Transfer DBTC between users")
.arg(
Arg::new("address")
.value_name("RECIPIENT_ADDRESS")
.takes_value(true)
.index(1)
.help_heading(Some("Address of recipient"))
.required(true),
)
.arg(
Arg::new("amount")
.value_name("AMOUNT")
.takes_value(true)
.index(2)
.help_heading(Some("Amount to send, in DBTC"))
.required(true),
),
)
//.subcommand(
// App::new("config")
// .about("Configuration settings")
// .aliases(&["get", "set"])
// .setting(AppSettings::SubcommandRequiredElseHelp)
// .subcommand(App::new("get").about("Get configuration settings"))
// .subcommand(
// App::new("set")
// .about("Set configuration settings")
// .args(&[
// Arg::new("rpc_url")
// .about("Set RPC Url")
// .long("rpc-url")
// .takes_value(true),
// Arg::new("log_path")
// .about("Set Log Path")
// .long("log-path")
// .takes_value(true),
// ])
// .setting(AppSettings::ArgRequiredElseHelp),
// ),
//)
.get_matches();
//let mut change_config = false;
let verbose = app.is_present("verbose");
let cashier = app.is_present("cashier");
let wallet = app.is_present("wallet");
let key = app.is_present("key");
let info = app.is_present("info");
let hello = app.is_present("hello");
let stop = app.is_present("stop");
let mut transfer = None;
match app.subcommand_matches("transfer") {
Some(transfer_sub) => {
let mut trn = Transfer::new();
if let Some(address) = transfer_sub.value_of("address") {
trn.pub_key = address.to_string();
}
if let Some(amount) = transfer_sub.value_of("amount") {
Transfer::verfiy_amount(amount)?;
trn.amount = amount.to_string();
}
transfer = Some(trn);
}
None => {}
}
//match app.subcommand_matches("config") {
// Some(config_sub) => match config_sub.subcommand() {
// Some(c) => match c {
// ("get", _) => {
// change_config = true;
// println!("RPC Url: {}", config.rpc_url);
// println!("Log Path: {}", config.log_path);
// }
// ("set", c) => {
// change_config = true;
// if let Some(v) = c.value_of("rpc_url") {
// config.rpc_url = v.to_string();
// println!("Change RPC Url To {}", config.rpc_url);
// }
// if let Some(v) = c.value_of("log_path") {
// config.log_path = v.to_string();
// println!("Change Log Path To {}", config.log_path);
// }
// }
// _ => {}
// },
// None => {}
// },
// None => {}
//}
Ok(Self {
//change_config,
verbose,
cashier,
wallet,
key,
info,
hello,
stop,
transfer,
})
}
}

65
src/cli/gatewayd_cli.rs Normal file
View File

@@ -0,0 +1,65 @@
use crate::Result;
pub struct GatewaydCli {
//pub accept_addr: Option<SocketAddr>,
//pub pub_addr: Option<SocketAddr>,
pub verbose: bool,
//pub database_path: Box<std::path::PathBuf>,
//pub log_path: Box<std::path::PathBuf>,
}
impl GatewaydCli {
pub fn load() -> Result<Self> {
let app = clap_app!(dfi =>
(version: "0.1.0")
(author: "Amir Taaki <amir@dyne.org>")
(about: "run service daemon")
//(@arg ACCEPT: -a --accept +takes_value "Accept add//ress")
//(@arg PUB_ADDR: -p --pubaddr +takes_value "Publisher addr")
(@arg VERBOSE: -v --verbose "Increase verbosity")
//(@arg DATABASE_PATH: --database +takes_value "database path")
//(@arg LOG_PATH: --log +takes_value "Logfile path")
)
.get_matches();
//let accept_addr = if let Some(accept_addr) = app.value_of("ACCEPT") {
// Some(accept_addr.parse()?)
//} else {
// None
//};
//let pub_addr = if let Some(pub_addr) = app.value_of("PUB_ADDR") {
// Some(pub_addr.parse()?)
//} else {
// None
//};
let verbose = app.is_present("VERBOSE");
//let database_path = Box::new(
// if let Some(database_path) = app.value_of("DATABASE_PATH") {
// std::path::Path::new(database_path)
// } else {
// std::path::Path::new("database.db")
// }
// .to_path_buf(),
//);
//let log_path = Box::new(
// if let Some(log_path) = app.value_of("LOG_PATH") {
// std::path::Path::new(log_path)
// } else {
// std::path::Path::new("/tmp/darkfid_service_daemon.log")
// }
// .to_path_buf(),
//);
Ok(Self {
//accept_addr,
//pub_addr,
verbose,
//database_path,
//log_path,
})
}
}