From 0d5ca6bf5850d579c6bdd951d8bd31753bd5e245 Mon Sep 17 00:00:00 2001 From: alemidev Date: Sun, 20 Mar 2022 23:48:33 +0100 Subject: [PATCH] super crude remote sync system --- Cargo.toml | 2 ++ src/main.rs | 24 +++++++++++++++++++++++- src/remote.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/remote.rs diff --git a/Cargo.toml b/Cargo.toml index 757c108..d56c862 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ edition = "2021" [dependencies] regex = "1.5.5" +base64 = "0.13.0" chrono = "0.4.19" colored = "2.0.0" git-version = "0.3.5" # ughh just for git hash @@ -17,6 +18,7 @@ const_format = "0.2.22" # ughh just for git hash libnotify = "1.0.3" clap = { version = "3.1.6", features = ["derive"] } rusqlite = { version="0.27.0", features=["chrono"] } +ureq = { version="2.4.0", features=["json"] } [profile.release] strip = true # Automatically strip symbols from the binary. diff --git a/src/main.rs b/src/main.rs index c024650..56a6cf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod storage; mod utils; +mod remote; use chrono::{DateTime, Local, Utc}; use clap::{Parser, Subcommand}; @@ -7,8 +8,9 @@ use colored::Colorize; use const_format::concatcp; use git_version::git_version; use regex::Regex; -use storage::{open_sqlite_storage, Memo, MemoStorage}; +use storage::{open_sqlite_storage, Memo, MemoStorage, SQLiteStorage}; use utils::{find_by_regex, parse_human_duration, HumanDisplay}; +use remote::RemoteSync; const GIT_VERSION: &str = git_version!(); const PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -27,6 +29,8 @@ struct Cli { notify: bool, #[clap(long, help = "show completed tasks")] old: bool, + #[clap(short, long, help = "synchronize memo db")] + sync: bool, #[clap(short, long, help = "location for database file")] path: Option, } @@ -68,6 +72,15 @@ fn main() { db_path = db; } + if args.sync { + let res = SQLiteStorage::fetch("asdasd", "http://127.0.0.1:8443"); + if res.is_ok() { + println!("[v] downloaded remote db"); + } else { + println!("[!] could not fetch db : {}", res.err().unwrap().to_string()); + } + } + let storage = open_sqlite_storage(&db_path).unwrap(); match args.command { @@ -163,4 +176,13 @@ fn main() { } } } + + if args.sync { + let res = SQLiteStorage::store("asdasd", "http://127.0.0.1:8443"); + if res.is_ok() { + println!("[^] uploaded local db"); + } else { + println!("[!] could not upload db : {}", res.err().unwrap().to_string()); + } + } } diff --git a/src/remote.rs b/src/remote.rs new file mode 100644 index 0000000..5b919f6 --- /dev/null +++ b/src/remote.rs @@ -0,0 +1,40 @@ +use crate::storage::SQLiteStorage; +use std::fs; +use std::io::{Write, Read}; + +pub trait RemoteSync { + fn store(hash:&str, server:&str) -> Result<(), ureq::Error>; + fn fetch(hash:&str, server:&str) -> Result<(), ureq::Error>; +} + +impl RemoteSync for SQLiteStorage { + + fn store(hash:&str, server:&str) -> Result<(), ureq::Error> { + let home_dir = env!("HOME").to_string(); + let contents = fs::read(home_dir + "/.local/share/memo-cli.db")?; + + let dest = server.to_string() + "/put"; + let _resp = ureq::post(dest.as_str()) + .send_json(ureq::json!({ + "file":hash, + "payload":base64::encode(contents.as_slice()) + }))?; + return Ok(()); + } + + fn fetch(hash:&str, server:&str) -> Result<(), ureq::Error> { + let dest = server.to_string() + "/get"; + let mut resp = ureq::post(dest.as_str()) + .send_json(ureq::json!({ + "file":hash, + "payload":"" + }))?.into_reader(); + + let home_dir = env!("HOME").to_string(); + let mut f = fs::File::create(home_dir + "/.local/share/memo-cli.db")?; + let mut data : Vec = vec![0;0]; + resp.read_to_end(&mut data)?; + f.write(data.as_slice())?; + return Ok(()); + } +}