Merge pull request #6 from hughrun/patch
improve error handling fixes #4
This commit is contained in:
commit
a6cdf52f7d
105
src/main.rs
105
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,53 +284,75 @@ 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"));
|
||||||
let spath = format!("{}", remote_dir.display());
|
|
||||||
let remote_vec = spath.split(':').collect::<Vec<_>>();
|
match args.len() {
|
||||||
let server_name = remote_vec[0];
|
2 => {
|
||||||
let server_path = remote_vec[1];
|
let remote_dir: std::path::PathBuf = expanduser(&config.remote_dir)?;
|
||||||
let remote_filepath = format!("{}{}/{}.gmi", server_path, year, dt.format("%Y-%m-%d"));
|
let spath = format!("{}", remote_dir.display());
|
||||||
let cmd = format!("[[ -f {} ]] && echo 'true' || echo 'false';", &remote_filepath);
|
let remote_vec = spath.split(':').collect::<Vec<_>>();
|
||||||
let check = Command::new("ssh")
|
let server_name = remote_vec[0];
|
||||||
.args(["-q", &server_name, &cmd])
|
let server_path = remote_vec[1];
|
||||||
.stdout(Stdio::piped())
|
let remote_filepath = format!("{}{}/{}.gmi", server_path, year, dt.format("%Y-%m-%d"));
|
||||||
.spawn()
|
let cmd = format!("[[ -f {} ]] && echo 'true' || echo 'false';", &remote_filepath);
|
||||||
.expect("reading from server failed");
|
|
||||||
let output = check
|
println!("Checking server for latest post...");
|
||||||
.wait_with_output()
|
let check = Command::new("ssh")
|
||||||
.expect("something fucked up");
|
.args(["-q", &server_name, &cmd])
|
||||||
let out = std::str::from_utf8(&output.stdout);
|
.stdout(Stdio::piped())
|
||||||
let out2 = out.clone()?.trim();
|
.spawn()
|
||||||
match out?.trim() {
|
.expect("reading from server failed");
|
||||||
"true" => {
|
let output = check
|
||||||
// user has already published today
|
.wait_with_output()
|
||||||
println!("\x1B[1;31mYou have already published today!\x1B[0m");
|
.expect("something fucked up");
|
||||||
println!("To edit your post, run 'sync down' first.");
|
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(())
|
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>> {
|
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