Skip to content

Commit

Permalink
added clap crate for commands management
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Sep 20, 2023
1 parent 88adea6 commit 7a4df04
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ easy-hasher = "2.2.1"
lightning-invoice = "0.22.0"
log = "0.4.17"
nostr-sdk = "0.24.0"
pretty_env_logger = "0.4.0"
serde = { version = "1.0.149" }
serde_json = "1.0.89"
sqlx = { version = "0.6.2", features = [
Expand All @@ -39,3 +38,4 @@ tokio-cron-scheduler = "*"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features =["env-filter"] }
config = "0.13.3"
clap = { version = "4.4.4" , features = ["derive"] }
2 changes: 1 addition & 1 deletion src/app/order.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::settings::Settings;
use crate::cli::settings::Settings;
use crate::util::{get_market_quote, publish_order, send_dm};

use anyhow::Result;
Expand Down
41 changes: 41 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pub mod settings;

use crate::cli::settings::init_default_dir;

use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;

#[derive(Parser)]
#[command(
name = "mostro p2p",
about = "A P2P lightning exchange over Nostr",
author,
help_template = "\
{before-help}{name}
{about-with-newline}
{author-with-newline}
{usage-heading} {usage}
{all-args}{after-help}
",
version
)]
#[command(propagate_version = true)]
#[command(arg_required_else_help(false))]
pub struct Cli {
/// Set folder for Mostro settings file - default is HOME/.mostro
#[arg(short, long)]
dirsettings: Option<String>,
}

pub fn settings_init() -> Result<PathBuf> {
let cli = Cli::parse();

if let Some(path) = cli.dirsettings.as_deref() {
init_default_dir(Some(path.to_string()))
} else {
init_default_dir(None)
}
}
2 changes: 1 addition & 1 deletion src/settings.rs → src/cli/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Settings {
}
}

pub fn init_default_dir(config_path: Option<&String>) -> Result<PathBuf> {
pub fn init_default_dir(config_path: Option<String>) -> Result<PathBuf> {
// , final_path : &mut PathBuf) -> Result<()> {
// Dir prefix
let home_dir: OsString;
Expand Down
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sqlx::Sqlite;
use sqlx::SqlitePool;
use uuid::Uuid;

use crate::settings::Settings;
use crate::cli::settings::Settings;

pub async fn connect() -> Result<Pool<Sqlite>, sqlx::Error> {
let db_settings = Settings::get_db();
Expand Down
2 changes: 1 addition & 1 deletion src/flow.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::settings::Settings;
use crate::cli::settings::Settings;
use crate::util::send_dm;

use log::info;
Expand Down
2 changes: 1 addition & 1 deletion src/lightning/invoice.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cli::settings::Settings;
use crate::error::MostroError;
use crate::settings::Settings;

use chrono::prelude::*;
use chrono::Duration;
Expand Down
2 changes: 1 addition & 1 deletion src/lightning/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod invoice;
use std::cmp::Ordering;

use crate::cli::settings::Settings;
use crate::lightning::invoice::decode_invoice;
use crate::settings::Settings;

use anyhow::Result;
use easy_hasher::easy_hasher::*;
Expand Down
29 changes: 6 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
pub mod app;
pub mod cli;
pub mod db;
pub mod error;
pub mod flow;
pub mod lightning;
pub mod messages;
pub mod models;
pub mod scheduler;
pub mod settings;
pub mod util;

use crate::app::run;
use crate::cli::settings::{init_global_settings, Settings};
use crate::cli::settings_init;
use anyhow::Result;
use lightning::LndConnector;
use nostr_sdk::prelude::*;
use scheduler::start_scheduler;
use settings::Settings;
use settings::{init_default_dir, init_global_settings};
use std::env;
use std::sync::Arc;
use std::{env::args, path::PathBuf, sync::OnceLock};
use std::sync::OnceLock;
use tokio::sync::Mutex;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

Expand All @@ -36,25 +36,8 @@ async fn main() -> Result<()> {

let rate_list: Arc<Mutex<Vec<Event>>> = Arc::new(Mutex::new(vec![]));

// File settings path
let mut config_path = PathBuf::new();

let args: Vec<String> = args().collect();
// Create install path string
match args.len() {
1 => {
// No dir parameter on cli
config_path = init_default_dir(None)?;
}
3 => {
if args[1] == "--dir" {
config_path = init_default_dir(Some(&args[2]))?;
}
}
_ => {
println!("Can't get what you're sayin! Run mostro or mostro --dir /path/to_config_file")
}
}
// Init path from cli
let config_path = settings_init()?;

// Create config global var
init_global_settings(Settings::new(config_path)?);
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cli::settings::Settings;
use crate::db::*;
use crate::lightning::LndConnector;
use crate::settings::Settings;
use crate::util::update_order_event;

use anyhow::Result;
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::cli::settings::Settings;
use crate::error::MostroError;
use crate::lightning;
use crate::lightning::LndConnector;
use crate::messages;
use crate::models::Yadio;
use crate::settings::Settings;
use crate::{db, flow};

use anyhow::{Context, Result};
Expand Down

0 comments on commit 7a4df04

Please sign in to comment.