mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-02 19:55:56 +01:00
feat(cli): working dir
This commit is contained in:
@@ -23,3 +23,4 @@ tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber = "0.3.18"
|
||||
rand = "0.8.5"
|
||||
home = "0.5.9"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -14,6 +15,8 @@ use rand::Rng;
|
||||
|
||||
mod sub_commands;
|
||||
|
||||
const DEFAULT_WORK_DIR: &str = ".cdk-cli";
|
||||
|
||||
/// Simple CLI application to interact with cashu
|
||||
#[derive(Parser)]
|
||||
#[command(name = "cashu-tool")]
|
||||
@@ -25,18 +28,12 @@ struct Cli {
|
||||
#[arg(short, long, default_value = "sqlite")]
|
||||
engine: String,
|
||||
/// Path to Seed
|
||||
#[arg(short, long, default_value = "./seed")]
|
||||
seed_path: String,
|
||||
/// File Path to save proofs
|
||||
#[arg(short, long)]
|
||||
db_path: Option<String>,
|
||||
work_dir: Option<PathBuf>,
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
const DEFAULT_REDB_DB_PATH: &str = "./cashu_tool.redb";
|
||||
const DEFAULT_SQLITE_DB_PATH: &str = "./cashu_tool.sqlite";
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Decode a token
|
||||
@@ -72,22 +69,39 @@ async fn main() -> Result<()> {
|
||||
// Parse input
|
||||
let args: Cli = Cli::parse();
|
||||
|
||||
let work_dir = match &args.work_dir {
|
||||
Some(work_dir) => work_dir.clone(),
|
||||
None => {
|
||||
let home_dir = home::home_dir().unwrap();
|
||||
home_dir.join(DEFAULT_WORK_DIR)
|
||||
}
|
||||
};
|
||||
|
||||
fs::create_dir_all(&work_dir)?;
|
||||
|
||||
let localstore: Arc<dyn WalletDatabase<Err = cdk_database::Error> + Send + Sync> =
|
||||
match args.engine.as_str() {
|
||||
"sqlite" => {
|
||||
let sql = WalletSQLiteDatabase::new(DEFAULT_SQLITE_DB_PATH).await?;
|
||||
let sql_path = work_dir.join("cdk-cli.sqlite");
|
||||
let sql = WalletSQLiteDatabase::new(&sql_path).await?;
|
||||
|
||||
sql.migrate().await;
|
||||
|
||||
Arc::new(sql)
|
||||
}
|
||||
"redb" => Arc::new(RedbWalletDatabase::new(DEFAULT_REDB_DB_PATH)?),
|
||||
"redb" => {
|
||||
let redb_path = work_dir.join("cdk-cli.redb");
|
||||
|
||||
Arc::new(RedbWalletDatabase::new(&redb_path)?)
|
||||
}
|
||||
_ => bail!("Unknown DB engine"),
|
||||
};
|
||||
|
||||
let mnemonic = match fs::metadata(args.seed_path.clone()) {
|
||||
let seed_path = work_dir.join("seed");
|
||||
|
||||
let mnemonic = match fs::metadata(seed_path.clone()) {
|
||||
Ok(_) => {
|
||||
let contents = fs::read_to_string(args.seed_path.clone())?;
|
||||
let contents = fs::read_to_string(seed_path.clone())?;
|
||||
Mnemonic::from_str(&contents)?
|
||||
}
|
||||
Err(_e) => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -36,7 +37,7 @@ pub struct MintRedbDatabase {
|
||||
}
|
||||
|
||||
impl MintRedbDatabase {
|
||||
pub fn new(path: &str) -> Result<Self, Error> {
|
||||
pub fn new(path: &Path) -> Result<Self, Error> {
|
||||
let db = Database::create(path)?;
|
||||
|
||||
let write_txn = db.begin_write()?;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -40,7 +41,7 @@ pub struct RedbWalletDatabase {
|
||||
}
|
||||
|
||||
impl RedbWalletDatabase {
|
||||
pub fn new(path: &str) -> Result<Self, Error> {
|
||||
pub fn new(path: &Path) -> Result<Self, Error> {
|
||||
let db = Database::create(path)?;
|
||||
|
||||
let write_txn = db.begin_write()?;
|
||||
|
||||
@@ -20,6 +20,9 @@ pub enum Error {
|
||||
/// Could Not Initialize Db
|
||||
#[error("Could not initialize Db")]
|
||||
CouldNotInitialize,
|
||||
/// Invalid Database Path
|
||||
#[error("Invalid database path")]
|
||||
InvalidDbPath,
|
||||
}
|
||||
|
||||
impl From<Error> for cdk::cdk_database::Error {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! SQLite Mint
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
use async_trait::async_trait;
|
||||
@@ -23,7 +24,8 @@ pub struct MintSqliteDatabase {
|
||||
}
|
||||
|
||||
impl MintSqliteDatabase {
|
||||
pub async fn new(path: &str) -> Result<Self, Error> {
|
||||
pub async fn new(path: &Path) -> Result<Self, Error> {
|
||||
let path = path.to_str().ok_or(Error::InvalidDbPath)?;
|
||||
let _conn = SqliteConnectOptions::from_str(path)?
|
||||
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal)
|
||||
.read_only(false)
|
||||
|
||||
@@ -5,21 +5,21 @@ pub enum Error {
|
||||
/// SQLX Error
|
||||
#[error(transparent)]
|
||||
SQLX(#[from] sqlx::Error),
|
||||
/// NUT02 Error
|
||||
/// Serde Error
|
||||
#[error(transparent)]
|
||||
Serde(#[from] serde_json::Error),
|
||||
/// NUT02 Error
|
||||
/// Wallet Error
|
||||
#[error(transparent)]
|
||||
CDKWallet(#[from] cdk::wallet::error::Error),
|
||||
/// NUT07 Error
|
||||
#[error(transparent)]
|
||||
CDKNUT07(#[from] cdk::nuts::nut07::Error),
|
||||
/// NUT02 Error
|
||||
#[error(transparent)]
|
||||
CDKNUT02(#[from] cdk::nuts::nut02::Error),
|
||||
/// NUT01 Error
|
||||
#[error(transparent)]
|
||||
CDKNUT01(#[from] cdk::nuts::nut01::Error),
|
||||
/// NUT02 Error
|
||||
#[error(transparent)]
|
||||
CDKNUT02(#[from] cdk::nuts::nut02::Error),
|
||||
/// NUT07 Error
|
||||
#[error(transparent)]
|
||||
CDKNUT07(#[from] cdk::nuts::nut07::Error),
|
||||
/// Secret Error
|
||||
#[error(transparent)]
|
||||
CDKSECRET(#[from] cdk::secret::Error),
|
||||
@@ -29,6 +29,9 @@ pub enum Error {
|
||||
/// Could Not Initialize Db
|
||||
#[error("Could not initialize Db")]
|
||||
CouldNotInitialize,
|
||||
/// Invalid Database Path
|
||||
#[error("Invalid database path")]
|
||||
InvalidDbPath,
|
||||
}
|
||||
|
||||
impl From<Error> for cdk::cdk_database::Error {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! SQLite Wallet Database
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
use async_trait::async_trait;
|
||||
@@ -25,7 +26,8 @@ pub struct WalletSQLiteDatabase {
|
||||
}
|
||||
|
||||
impl WalletSQLiteDatabase {
|
||||
pub async fn new(path: &str) -> Result<Self, Error> {
|
||||
pub async fn new(path: &Path) -> Result<Self, Error> {
|
||||
let path = path.to_str().ok_or(Error::InvalidDbPath)?;
|
||||
let _conn = SqliteConnectOptions::from_str(path)?
|
||||
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal)
|
||||
.read_only(false)
|
||||
|
||||
Reference in New Issue
Block a user