Check remote for latest post

Fixes #2
This commit is contained in:
Hugh Rundle 2023-04-01 20:28:32 +11:00
parent 3f1bd0aded
commit c066383e8a
3 changed files with 43 additions and 9 deletions

6
Cargo.lock generated
View file

@ -75,9 +75,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.23"
version = "0.4.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b"
dependencies = [
"iana-time-zone",
"js-sys",
@ -426,7 +426,7 @@ dependencies = [
[[package]]
name = "soyuz-cli"
version = "1.0.0"
version = "1.0.1"
dependencies = [
"chrono",
"expanduser",

View file

@ -1,12 +1,12 @@
[package]
name = "soyuz-cli"
version = "1.0.0"
version = "1.0.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.23"
chrono = "0.4.24"
expanduser = "1.2.2"
regex = "1.7.1"
serde = { version = "1.0.152", features = ["derive"] }

View file

@ -9,7 +9,7 @@ use std::path::PathBuf;
use regex::Regex;
use serde::Deserialize;
use std::fs;
use std::process::Command;
use std::process::{Command, Stdio};
use toml;
#[derive(Debug, Deserialize)]
@ -280,13 +280,47 @@ fn sync(args: &Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
}
fn write() -> Result<(), Box<dyn std::error::Error>> {
println!("Checking server for latest post...");
let config = read_config()?;
let dt = Local::now();
let year = &dt.format("%Y").to_string();
let local_dir: std::path::PathBuf = expanduser(config.local_dir)?;
let local_dir: std::path::PathBuf = expanduser(&config.local_dir)?;
let remote_dir: std::path::PathBuf = expanduser(&config.remote_dir)?;
fs::create_dir_all(format!("{}{}", &local_dir.display(), &year))?;
let filepath = format!("{}{}/{}.gmi", local_dir.display(), year, dt.format("%Y-%m-%d"));
Ok(open_file(filepath.into()))
let spath = format!("{}", remote_dir.display());
let remote_vec = spath.split(':').collect::<Vec<_>>();
let server_name = remote_vec[0];
let server_path = remote_vec[1];
let remote_filepath = format!("{}{}/{}.gmi", server_path, year, dt.format("%Y-%m-%d"));
let cmd = format!("[[ -f {} ]] && echo 'true' || echo 'false';", &remote_filepath);
let check = Command::new("ssh")
.args(["-q", &server_name, &cmd])
.stdout(Stdio::piped())
.spawn()
.expect("reading from server failed");
let output = check
.wait_with_output()
.expect("something fucked up");
let out = std::str::from_utf8(&output.stdout);
let out2 = out.clone()?.trim();
match out?.trim() {
"true" => {
// user has already published today
println!("\x1B[1;31mYou have already published today!\x1B[0m");
println!("To edit your post, run 'sync down' first.");
Ok(())
},
"false" => {
// open a new file
Ok(open_file(filepath.into()))
},
_ => {
println!("Something went wrong checking your gemini server");
println!("Error: {:?}", out2);
Ok(())
}
}
}
fn match_single_arg(args: &Vec<String>) -> Result<(), Box<dyn std::error::Error>> {