super crude remote sync system

This commit is contained in:
əlemi 2022-03-20 23:48:33 +01:00
parent dd865ab683
commit 0d5ca6bf58
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
3 changed files with 65 additions and 1 deletions

View file

@ -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.

View file

@ -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<String>,
}
@ -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());
}
}
}

40
src/remote.rs Normal file
View file

@ -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<u8> = vec![0;0];
resp.read_to_end(&mut data)?;
f.write(data.as_slice())?;
return Ok(());
}
}