-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
222b628
commit 88b9cd5
Showing
17 changed files
with
217 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.env | ||
config.yaml | ||
/target | ||
/migration/target | ||
note.txt | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,14 @@ A command line utility for analysing rust transactions | |
|
||
## Configuration | ||
|
||
Create a file `.env` with the following content: | ||
Create a file `config.yaml` with the following content: | ||
|
||
``` | ||
DATABASE_URL="mysql://admin:[email protected]:3306/starling_db" | ||
PERSONAL_TOKEN="XXX" | ||
BUSINESS_TOKEN="YYY" | ||
token: | ||
- person: "XXX" | ||
- business: "YYY" | ||
db: | ||
user: "admin" | ||
password: "ZZZ" | ||
name: starling_db | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
//! Command Line Interface `Accounts` commands | ||
//! | ||
use std::error::Error; | ||
use anyhow::Result; | ||
|
||
use crate::{ | ||
db, | ||
starling::client::{StarlingApiClient, StarlingClient}, | ||
}; | ||
|
||
/// Fetch account information from Starling and populate the database | ||
pub async fn get_accounts(client: &StarlingApiClient) -> Result<(), Box<dyn Error>> { | ||
pub async fn get_accounts() -> Result<()> { | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//! Command Line Interface `Database` commands | ||
//! | ||
use std::io::Write; | ||
use std::{io, process}; | ||
|
||
use crate::entities::account; | ||
use crate::{ | ||
config::Config, | ||
db, | ||
starling::client::{StarlingApiClient, StarlingClient}, | ||
}; | ||
use anyhow::Result; | ||
use sea_orm::*; | ||
|
||
/// Initialise the database. | ||
/// | ||
/// Drop and reinstate the tables if they exist, fetch account data from Starling and save, and initialise with | ||
/// transaction data for all accounts and saving spaces. | ||
/// | ||
pub async fn initialise_database() -> Result<()> { | ||
let config = Config::new(); | ||
|
||
if !proceed() { | ||
println!("Exiting."); | ||
process::exit(1); | ||
} | ||
|
||
// process accounts | ||
db::account::delete_all().await; | ||
db::transaction::delete_all().await; | ||
db::counterparty::delete_all().await; | ||
|
||
// delete the old ones | ||
let db = Database::connect(&config.db_url()) | ||
.await | ||
.expect("getting database"); | ||
|
||
let res: DeleteResult = account::Entity::delete_many() | ||
.exec(&db) | ||
.await | ||
.expect("deleting accounts"); | ||
|
||
for item in config.token.iter() { | ||
for (name, token) in item.iter() { | ||
// drop and reload the accounts | ||
let client = StarlingApiClient::new(token); | ||
for account in client.accounts().await.iter() { | ||
db::account::insert_or_update(&account); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
// Return true if user enters 'y' or 'Y' | ||
fn proceed() -> bool { | ||
println!("This will destroy the database and can't be undone"); | ||
print!("Proceed? (y/n) "); | ||
io::stdout().flush().unwrap(); | ||
let mut response = String::new(); | ||
io::stdin() | ||
.read_line(&mut response) | ||
.expect("failed to read response"); | ||
response.trim().to_lowercase() == String::from("y") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod accounts; | ||
pub mod database; | ||
pub mod transactions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! Functionality for managing a config file | ||
//! | ||
//! | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct Config { | ||
pub token: Vec<HashMap<String, String>>, | ||
pub db: DbConfig, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct DbConfig { | ||
pub user: String, | ||
pub password: String, | ||
pub name: String, | ||
} | ||
|
||
impl Config { | ||
pub fn new() -> Self { | ||
let f = std::fs::File::open("config.yaml").expect("opening file"); | ||
let d: Config = serde_yaml::from_reader(f).expect("decoding"); | ||
|
||
d | ||
} | ||
|
||
pub fn db_url(&self) -> String { | ||
format!( | ||
"mysql://{}:{}@db.kingswood:3306/{}", | ||
self.db.user, self.db.password, self.db.name | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//! Functions for interacting with table `accounts` | ||
use super::get_database; | ||
use crate::entities::account; | ||
use crate::starling::account::Account; | ||
use sea_orm::*; | ||
|
||
// DELETE * FROM account; | ||
pub async fn delete_all() { | ||
let db = get_database().await; | ||
account::Entity::delete_many() | ||
.exec(&db) | ||
.await | ||
.expect("deleting accounts"); | ||
} | ||
|
||
pub async fn insert_or_update(account: &Account) { | ||
let db = get_database().await; | ||
|
||
println!("{:#?}", account); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use super::get_database; | ||
use crate::entities::counterparty; | ||
use sea_orm::*; | ||
|
||
// DELETE * FROM account; | ||
pub async fn delete_all() { | ||
let db = get_database().await; | ||
counterparty::Entity::delete_many() | ||
.exec(&db) | ||
.await | ||
.expect("deleting accounts"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
pub mod service; | ||
pub mod account; | ||
pub mod counterparty; | ||
pub mod transaction; | ||
|
||
use crate::config::Config; | ||
use sea_orm::*; | ||
|
||
async fn get_database() -> DatabaseConnection { | ||
let config = Config::new(); | ||
Database::connect(&config.db_url()) | ||
.await | ||
.expect("getting database") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod commands; | ||
pub mod config; | ||
pub mod db; | ||
pub mod entities; | ||
pub mod starling; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters