Skip to content

Commit

Permalink
Switch to use writeln!
Browse files Browse the repository at this point in the history
Allows better handling of SIGPIPE failures. Ref rust-lang/rust#46016
  • Loading branch information
ukanga committed Jul 16, 2022
1 parent e29ca04 commit cb0b96d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde_json::json;
use std::error::Error;
use std::fs;
use std::io;
use std::io::Write;
use std::path::Path;

// #[derive(Debug)]
Expand Down Expand Up @@ -162,7 +163,8 @@ fn file_url(client: &Client, file_id: i64) -> Result<String, Box<dyn Error>> {
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
// let files: Files = serde_json::from_str(data)?;
let stdout = io::stdout();
let mut handle = stdout.lock();
let path = Path::new(".");
let downloaded_files = list_dir(&path).unwrap();
let client = Client::new(&config.oauth_token);
Expand All @@ -171,7 +173,7 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
if file.file_type == "VIDEO" {
let download_url = file_url(&client, file.id).unwrap();
if downloaded_files.iter().find(|&x| x == &file.name) == None {
println!("{}", download_url);
writeln!(handle, "{}", download_url)?;
} else {
if file.parent_id != config.parent_id {
delete_file(&client, file.parent_id).unwrap();
Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
use std::env;
use std::io::{self, Result, Write};
use std::process;

use putio::run;
use putio::Config;

fn main() {
fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
let stdout = io::stdout();

let config = Config::new(&args).unwrap_or_else(|err| {
println!("\r\nProblems parsing arguments: {}\r\n", err);
let mut handle = stdout.lock();
writeln!(handle, "\r\nProblems parsing arguments: {}\r\n", err).unwrap();
process::exit(1);
});

if let Err(err) = run(config) {
println!("\r\nError: {}\r\n", err);
let mut handle = stdout.lock();
writeln!(handle, "\r\nError: {}\r\n", err).unwrap();

process::exit(1);
}
Ok(())
}

0 comments on commit cb0b96d

Please sign in to comment.