improve error handling

Fixes #4
This commit is contained in:
Hugh Rundle 2024-04-20 13:07:49 +10:00
parent 87e2a39a56
commit fc2b43a659
Signed by: hugh
GPG key ID: A7E35779918253F9

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,21 +284,26 @@ 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"));
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())
@ -308,7 +318,8 @@ fn write() -> Result<(), Box<dyn std::error::Error>> {
"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.");
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" => {
@ -321,11 +332,27 @@ fn write() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
}
}
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())
}
}