Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make batch interactive #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use batch::Batch;

use rust_decimal::Decimal;

use std::io;

pub struct GaloyClient {
graphql_client: Client,
api: String,
Expand Down Expand Up @@ -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")?;
Expand All @@ -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")
}
Comment on lines +201 to +211
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I've never written any rust that had interaction like this so I don't know if there is a library or a standard convention on how to do this. Would have to go looking at other code to know if there is a better way.

Without doing any research I'd say this looks fine.

You could avoid the double call to trim but afaict it doesn't allocate so is negligible for this use-case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

batch.execute().context("can't make payment successfully")?;

Ok(())
Expand Down
56 changes: 39 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use url::Url;

use galoy_client::GaloyClient;

use anyhow::Context;
use anyhow::{bail, Context};

use jsonwebtoken::decode_header;

Expand Down Expand Up @@ -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<()> {
Expand All @@ -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 {} => {
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down