From d6398cd46ed3efe7208524f02ae93346c363e7dd Mon Sep 17 00:00:00 2001 From: Nicolas Burtey Date: Thu, 1 Sep 2022 22:43:44 +0100 Subject: [PATCH 1/2] chore: ask for confirmation before executing batch payment --- src/client/mod.rs | 19 ++++++++++++++-- src/main.rs | 56 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index f3c42be..a35de5b 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -12,6 +12,8 @@ use batch::Batch; use rust_decimal::Decimal; +use std::io; + pub struct GaloyClient { graphql_client: Client, api: String, @@ -179,8 +181,7 @@ impl GaloyClient { } } - // TODO: check if we can do self without & - pub fn batch(self, filename: String, price: Decimal) -> anyhow::Result<()> { + pub fn batch(self, filename: String, price: Decimal, force: bool) -> anyhow::Result<()> { let mut batch = Batch::new(self, price); batch.add_csv(filename).context("can't load file")?; @@ -196,6 +197,20 @@ impl GaloyClient { println!("going to execute:"); batch.show(); + if !force { + println!("do you want to confirm? y/n"); + + let mut input = String::new(); + + io::stdin() + .read_line(&mut input) + .expect("error: unable to read user input"); + + if !(input.trim() == "y" || input.trim() == "yes") { + bail!("didn't confirm batch payment. exiting") + } + } + batch.execute().context("can't make payment successfully")?; Ok(()) diff --git a/src/main.rs b/src/main.rs index 2c2f231..a10a406 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use url::Url; use galoy_client::GaloyClient; -use anyhow::Context; +use anyhow::{bail, Context}; use jsonwebtoken::decode_header; @@ -61,7 +61,12 @@ enum Commands { /// get JWT of an account Login { phone: String, code: String }, /// execute a batch payment - Batch { filename: String, price: Decimal }, + Batch { + filename: String, + price: Decimal, + #[clap(short, long)] + force: bool, + }, } fn main() -> anyhow::Result<()> { @@ -83,7 +88,7 @@ fn main() -> anyhow::Result<()> { } info!("using api: {api} and jwt: {:?}", &jwt); - let galoy_client = GaloyClient::new(api, jwt); + let galoy_client = GaloyClient::new(api, jwt.clone()); match cli.command { Commands::Getinfo {} => { @@ -94,7 +99,24 @@ fn main() -> anyhow::Result<()> { let result = galoy_client.default_wallet(username)?; println!("{:#?}", result); } + Commands::RequestPhoneCode { phone } => { + let result = galoy_client + .request_auth_code(phone) + .context("issue getting code")?; + println!("{:#?}", result); + } + Commands::Login { phone, code } => { + let result = galoy_client + .user_login(phone, code) + .context("issue logging in")?; + println!("{:#?}", result); + } + Commands::Me => { + if jwt.is_none() { + bail!("need JWT"); + } + let result = galoy_client.me().context("can't get me")?; println!("{:#?}", result); } @@ -103,26 +125,26 @@ fn main() -> anyhow::Result<()> { amount, memo, } => { + if jwt.is_none() { + bail!("need JWT"); + } + let result = galoy_client .intraleger_send(username, amount, memo) .context("issue sending intraledger")?; println!("{:#?}", result); } - Commands::RequestPhoneCode { phone } => { - let result = galoy_client - .request_auth_code(phone) - .context("issue getting code")?; - println!("{:#?}", result); - } - Commands::Login { phone, code } => { - let result = galoy_client - .user_login(phone, code) - .context("issue logging in")?; - println!("{:#?}", result); - } - Commands::Batch { filename, price } => { + Commands::Batch { + filename, + price, + force, + } => { + if jwt.is_none() { + bail!("need JWT"); + } + let result = galoy_client - .batch(filename, price) + .batch(filename, price, force) .context("issue batching payment"); println!("{:#?}", result); } From c06be023e213d9cefb815bd409d96918db9abd14 Mon Sep 17 00:00:00 2001 From: Nicolas Burtey Date: Fri, 2 Sep 2022 08:58:23 +0100 Subject: [PATCH 2/2] chore: more specific import --- tests/common/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index a3bba9a..8e2b504 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,4 +1,4 @@ -use super::*; +use galoy_client::GaloyClient; pub fn unauth_client() -> galoy_client::GaloyClient { let api = "http://localhost:4002/graphql".to_string();