improve error handling #6
45
src/main.rs
45
src/main.rs
|
@ -140,6 +140,11 @@ fn publish() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let post = fs::read_to_string(format!("{}/{}", dirpath, &filename))?;
|
let post = fs::read_to_string(format!("{}/{}", dirpath, &filename))?;
|
||||||
let post_lines: Vec<&str> = post.lines().collect();
|
let post_lines: Vec<&str> = post.lines().collect();
|
||||||
let title = post_lines[0].strip_prefix("# ");
|
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 entry_string = format!("=> {} {} ({})", filename, sliced, title.unwrap());
|
||||||
let home_entry_string = format!("=> /{}/{} {} ({})", &year, filename, sliced, title.unwrap());
|
let home_entry_string = format!("=> /{}/{} {} ({})", &year, filename, sliced, title.unwrap());
|
||||||
if let Ok(index) = fs::read_to_string(&indexfilepath) {
|
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>> {
|
fn write(args: &Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("Checking server for latest post...");
|
|
||||||
let config = read_config()?;
|
let config = read_config()?;
|
||||||
let dt = Local::now();
|
let dt = Local::now();
|
||||||
let year = &dt.format("%Y").to_string();
|
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))?;
|
fs::create_dir_all(format!("{}{}", &local_dir.display(), &year))?;
|
||||||
let filepath = format!("{}{}/{}.gmi", local_dir.display(), year, dt.format("%Y-%m-%d"));
|
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 spath = format!("{}", remote_dir.display());
|
||||||
let remote_vec = spath.split(':').collect::<Vec<_>>();
|
let remote_vec = spath.split(':').collect::<Vec<_>>();
|
||||||
let server_name = remote_vec[0];
|
let server_name = remote_vec[0];
|
||||||
let server_path = remote_vec[1];
|
let server_path = remote_vec[1];
|
||||||
let remote_filepath = format!("{}{}/{}.gmi", server_path, year, dt.format("%Y-%m-%d"));
|
let remote_filepath = format!("{}{}/{}.gmi", server_path, year, dt.format("%Y-%m-%d"));
|
||||||
let cmd = format!("[[ -f {} ]] && echo 'true' || echo 'false';", &remote_filepath);
|
let cmd = format!("[[ -f {} ]] && echo 'true' || echo 'false';", &remote_filepath);
|
||||||
|
|
||||||
|
println!("Checking server for latest post...");
|
||||||
let check = Command::new("ssh")
|
let check = Command::new("ssh")
|
||||||
.args(["-q", &server_name, &cmd])
|
.args(["-q", &server_name, &cmd])
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
|
@ -308,7 +318,8 @@ fn write() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
"true" => {
|
"true" => {
|
||||||
// user has already published today
|
// user has already published today
|
||||||
println!("\x1B[1;31mYou have already published today!\x1B[0m");
|
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(())
|
Ok(())
|
||||||
},
|
},
|
||||||
"false" => {
|
"false" => {
|
||||||
|
@ -321,11 +332,27 @@ fn write() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
Ok(())
|
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>> {
|
fn match_single_arg(args: &Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
match args[1].as_str() {
|
match args[1].as_str() {
|
||||||
"write" => write(),
|
"write" => write(args),
|
||||||
"settings" => settings(),
|
"settings" => settings(),
|
||||||
"publish" => publish(),
|
"publish" => publish(),
|
||||||
"sync" => sync(args),
|
"sync" => sync(args),
|
||||||
|
@ -338,7 +365,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>>{
|
||||||
match &args.len() {
|
match &args.len() {
|
||||||
1 => Ok(help()),
|
1 => Ok(help()),
|
||||||
2 => match_single_arg(&args),
|
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())
|
_ => Ok(help())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue