Merge pull request #6 from hughrun/patch

improve error handling
fixes #4
This commit is contained in:
Hugh Rundle 2024-04-20 13:09:48 +10:00 committed by GitHub
commit a6cdf52f7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -140,6 +140,11 @@ fn publish() -> Result<(), Box<dyn std::error::Error>> {
let post = fs::read_to_string(format!("{}/{}", dirpath, &filename))?;
let post_lines: Vec<&str> = post.lines().collect();
let title = post_lines[0].strip_prefix("# ");
if title.is_none() {
println!("ABORTING: Your latest post does not have a title.");
println!("Use a '# Heading' on the first line.");
return Ok(())
}
let entry_string = format!("=> {} {} ({})", filename, sliced, title.unwrap());
let home_entry_string = format!("=> /{}/{} {} ({})", &year, filename, sliced, title.unwrap());
if let Ok(index) = fs::read_to_string(&indexfilepath) {
@ -279,53 +284,75 @@ 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...");
fn write(args: &Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
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 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"));
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.");
match args.len() {
2 => {
let remote_dir: std::path::PathBuf = expanduser(&config.remote_dir)?;
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);
println!("Checking server for latest post...");
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 a post published with soyuz-web, run 'soyuz sync down' first.");
println!("To edit a published post already saved locally, use 'soyuz write --edit'");
Ok(())
},
"false" => {
// open a new file
Ok(open_file(filepath.into()))
},
_ => {
println!("Something went wrong checking your gemini server");
println!("Error: {:?}", out2);
Ok(())
}
}
},
3 => {
match args[2].as_str() {
"--edit" => Ok(open_file(filepath.into())),
_ => {
println!("Unknown command. Try 'soyuz help'.");
Ok(())
}
}
},
_ => {
println!("Unknown command. Try 'soyuz help'.");
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>> {
match args[1].as_str() {
"write" => write(),
"write" => write(args),
"settings" => settings(),
"publish" => publish(),
"sync" => sync(args),
@ -338,7 +365,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>>{
match &args.len() {
1 => Ok(help()),
2 => match_single_arg(&args),
3 | 4 => sync(&args),
3 | 4 => {
match args[1].as_str() {
"sync" => sync(&args),
"write" => write(&args),
&_ => Ok(help())
}
},
_ => Ok(help())
}
}