Fix broken pipe panic

This commit is contained in:
Matthias Beyer 2018-02-14 23:38:18 +01:00
parent fc0610b77f
commit 125eebe1ce
2 changed files with 52 additions and 15 deletions

View file

@ -22,6 +22,7 @@ is-it-maintained-open-issues = { repository = "matthiasbeyer/imag" }
maintenance = { status = "actively-developed" } maintenance = { status = "actively-developed" }
[dependencies] [dependencies]
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
[dependencies.clap] [dependencies.clap]
version = ">=2.29" version = ">=2.29"

View file

@ -37,6 +37,8 @@ extern crate clap;
#[cfg(test)] #[cfg(test)]
extern crate toml; extern crate toml;
extern crate libimagerror;
mod ui; mod ui;
use std::fs::OpenOptions; use std::fs::OpenOptions;
@ -45,6 +47,9 @@ use std::path::PathBuf;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode;
const CONFIGURATION_STR : &'static str = include_str!("../imagrc.toml"); const CONFIGURATION_STR : &'static str = include_str!("../imagrc.toml");
const GITIGNORE_STR : &'static str = r#" const GITIGNORE_STR : &'static str = r#"
@ -63,6 +68,7 @@ imagrc.toml
fn main() { fn main() {
let app = ui::build_ui(); let app = ui::build_ui();
let matches = app.get_matches(); let matches = app.get_matches();
let mut out = ::std::io::stdout();
let path = matches let path = matches
.value_of("path") .value_of("path")
@ -73,8 +79,12 @@ fn main() {
.map(PathBuf::from) .map(PathBuf::from)
.map(|mut p| { p.push(".imag"); p }) .map(|mut p| { p.push(".imag"); p })
.map(|path| if path.exists() { .map(|path| if path.exists() {
println!("Path '{:?}' already exists!", path); let _ = writeln!(out, "Path '{:?}' already exists!", path)
println!("Cannot continue."); .to_exit_code()
.unwrap_or_exit();
let _ = writeln!(out, "Cannot continue.")
.to_exit_code()
.unwrap_or_exit();
::std::process::exit(1) ::std::process::exit(1)
} else { } else {
path path
@ -109,7 +119,9 @@ fn main() {
if find_command("git").is_some() && !matches.is_present("nogit") { if find_command("git").is_some() && !matches.is_present("nogit") {
// we initialize a git repository // we initialize a git repository
println!("Going to initialize a git repository in the imag directory..."); let _ = writeln!(out, "Going to initialize a git repository in the imag directory...")
.to_exit_code()
.unwrap_or_exit();
let gitignore_path = { let gitignore_path = {
let mut gitignore_path = path.clone(); let mut gitignore_path = path.clone();
@ -138,10 +150,16 @@ fn main() {
.expect("Calling 'git init' failed"); .expect("Calling 'git init' failed");
if output.status.success() { if output.status.success() {
println!("{}", String::from_utf8(output.stdout).expect("No UTF-8 output")); let _ = writeln!(out, "{}", String::from_utf8(output.stdout).expect("No UTF-8 output"))
println!("'git {} {} --no-pager init' succeeded", worktree, gitdir); .to_exit_code()
.unwrap_or_exit();
let _ = writeln!(out, "'git {} {} --no-pager init' succeeded", worktree, gitdir)
.to_exit_code()
.unwrap_or_exit();
} else { } else {
println!("{}", String::from_utf8(output.stderr).expect("No UTF-8 output")); let _ = writeln!(out, "{}", String::from_utf8(output.stderr).expect("No UTF-8 output"))
.to_exit_code()
.unwrap_or_exit();
::std::process::exit(output.status.code().unwrap_or(1)); ::std::process::exit(output.status.code().unwrap_or(1));
} }
} }
@ -152,10 +170,16 @@ fn main() {
.output() .output()
.expect("Calling 'git add' failed"); .expect("Calling 'git add' failed");
if output.status.success() { if output.status.success() {
println!("{}", String::from_utf8(output.stdout).expect("No UTF-8 output")); let _ = writeln!(out, "{}", String::from_utf8(output.stdout).expect("No UTF-8 output"))
println!("'git {} {} --no-pager add {}' succeeded", worktree, gitdir, gitignore_path); .to_exit_code()
.unwrap_or_exit();
let _ = writeln!(out, "'git {} {} --no-pager add {}' succeeded", worktree, gitdir, gitignore_path)
.to_exit_code()
.unwrap_or_exit();
} else { } else {
println!("{}", String::from_utf8(output.stderr).expect("No UTF-8 output")); let _ = writeln!(out, "{}", String::from_utf8(output.stderr).expect("No UTF-8 output"))
.to_exit_code()
.unwrap_or_exit();
::std::process::exit(output.status.code().unwrap_or(1)); ::std::process::exit(output.status.code().unwrap_or(1));
} }
} }
@ -166,20 +190,32 @@ fn main() {
.output() .output()
.expect("Calling 'git commit' failed"); .expect("Calling 'git commit' failed");
if output.status.success() { if output.status.success() {
println!("{}", String::from_utf8(output.stdout).expect("No UTF-8 output")); let _ = writeln!(out, "{}", String::from_utf8(output.stdout).expect("No UTF-8 output"))
println!("'git {} {} --no-pager commit {} -m 'Initial import'' succeeded", worktree, gitdir, gitignore_path); .to_exit_code()
.unwrap_or_exit();
let _ = writeln!(out, "'git {} {} --no-pager commit {} -m 'Initial import'' succeeded", worktree, gitdir, gitignore_path)
.to_exit_code()
.unwrap_or_exit();
} else { } else {
println!("{}", String::from_utf8(output.stderr).expect("No UTF-8 output")); let _ = writeln!(out, "{}", String::from_utf8(output.stderr).expect("No UTF-8 output"))
.to_exit_code()
.unwrap_or_exit();
::std::process::exit(output.status.code().unwrap_or(1)); ::std::process::exit(output.status.code().unwrap_or(1));
} }
} }
println!("git stuff finished!"); let _ = writeln!(out, "git stuff finished!")
.to_exit_code()
.unwrap_or_exit();
} else { } else {
println!("No git repository will be initialized"); let _ = writeln!(out, "No git repository will be initialized")
.to_exit_code()
.unwrap_or_exit();
} }
println!("Ready. Have fun with imag!"); let _ = writeln!(out, "Ready. Have fun with imag!")
.to_exit_code()
.unwrap_or_exit();
} }
fn get_config() -> String { fn get_config() -> String {