Skip to content

Commit

Permalink
Refactor settings
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 9, 2024
1 parent c2da228 commit 4e9246b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use serde_json::json;
use std::env;
use std::env::Args;
use std::error::Error;

mod db;
mod rpc;
mod settings;

const UNAUTHORIZED_ACTIONS: [&str; 2] = ["login", "help"];

Expand All @@ -23,8 +22,7 @@ fn main() -> Result<()> {
.next()
.ok_or("you need to provide an action, run btcmap-cli help to see all supported actions")?;
let action = action.as_str();
let password = db::query_settings_string("password", &db::connect()?)?;
if password.is_empty() && !UNAUTHORIZED_ACTIONS.contains(&action) {
if settings::get_str("password")?.is_empty() && !UNAUTHORIZED_ACTIONS.contains(&action) {
Err("you need to login first, run btcmap-cli login <password>")?;
}
match action {
Expand All @@ -37,12 +35,12 @@ fn main() -> Result<()> {
"dev" => "http://127.0.0.1:8000/rpc",
_ => url,
};
db::insert_settings_string("api_url", &url, &db::connect()?)?;
settings::put_str("api_url", &url)?;
println!("saved {url} as a server for all future actions");
}
"login" => {
let token = get_arg(&mut args)?;
db::insert_settings_string("password", &token, &db::connect()?)?;
settings::put_str("password", &token)?;
println!("saved {token} as a password for all future actions");
}
"get-element" => {
Expand Down
9 changes: 3 additions & 6 deletions src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::db;
use crate::settings;
use crate::Result;
use colored_json::ToColoredJson;
use serde_json::{json, Value};
Expand All @@ -7,14 +7,11 @@ pub fn call_remote_procedure(name: &str, mut params: Value) -> Result<()> {
let params = params
.as_object_mut()
.ok_or("params value is not a valid JSON object")?;
params.insert(
"password".into(),
db::query_settings_string("password", &db::connect()?)?.into(),
);
params.insert("password".into(), settings::get_str("password")?.into());
let args = json!(
{"jsonrpc": "2.0", "method": name, "params": params, "id": 1}
);
let mut api_url = db::query_settings_string("api_url", &db::connect()?)?;
let mut api_url = settings::get_str("api_url")?;
if api_url.trim().is_empty() {
api_url = "https://api.btcmap.org/rpc".into();
}
Expand Down
19 changes: 10 additions & 9 deletions src/db.rs → src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ use dirs::data_dir;
use rusqlite::{params, Connection};
use std::{fs::create_dir, path::PathBuf};

pub fn connect() -> Result<Connection> {
let conn = Connection::open(path()?)?;
init(&conn)?;
Ok(conn)
}

pub fn insert_settings_string(name: &str, value: &str, conn: &Connection) -> Result<()> {
conn.execute(
pub fn put_str(name: &str, value: &str) -> Result<()> {
connect()?.execute(
&format!("UPDATE settings SET json = json_set(json, '$.{name}', ?1);"),
params![value],
)?;
Ok(())
}

pub fn query_settings_string(name: &str, conn: &Connection) -> Result<String> {
pub fn get_str(name: &str) -> Result<String> {
let conn = connect()?;
let mut stmt = conn.prepare(&format!(
"SELECT json_extract(json, '$.{name}') FROM settings;"
))?;
Expand All @@ -29,6 +24,12 @@ pub fn query_settings_string(name: &str, conn: &Connection) -> Result<String> {
Ok(res)
}

fn connect() -> Result<Connection> {
let conn = Connection::open(path()?)?;
init(&conn)?;
Ok(conn)
}

fn path() -> Result<PathBuf> {
let data_dir = data_dir().ok_or("failed to locate system app data directory")?;
let data_dir = data_dir.join("btcmap-cli");
Expand Down

0 comments on commit 4e9246b

Please sign in to comment.