Integrate spinner for nicer progress reports

This commit is contained in:
Matthias Beyer 2018-01-03 16:00:46 +01:00
parent f324deb41e
commit de7a4d2dfb
2 changed files with 28 additions and 15 deletions

View file

@ -22,6 +22,7 @@ maintenance = { status = "actively-developed" }
[dependencies] [dependencies]
clap = ">=2.17" clap = ">=2.17"
version = "2.0.1" version = "2.0.1"
spinner = "0.3"
[dev-dependencies] [dev-dependencies]
toml = "0.4" toml = "0.4"

View file

@ -33,6 +33,7 @@
)] )]
extern crate clap; extern crate clap;
extern crate spinner;
#[macro_use] extern crate version; #[macro_use] extern crate version;
#[cfg(test)] #[cfg(test)]
@ -46,6 +47,8 @@ use std::path::PathBuf;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use spinner::SpinnerBuilder;
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#"
# We ignore the imagrc.toml file by default # We ignore the imagrc.toml file by default
@ -61,14 +64,21 @@ imagrc.toml
"#; "#;
fn main() { fn main() {
let sp = SpinnerBuilder::new("imag-init".into()).start();
sp.update("Building ui".into());
let app = ui::build_ui(); let app = ui::build_ui();
sp.update("Getting matches".into());
let matches = app.get_matches(); let matches = app.get_matches();
sp.update("Evaluating commandline".into());
let path = matches let path = matches
.value_of("path") .value_of("path")
.map(String::from) .map(String::from)
.map(PathBuf::from) .map(PathBuf::from)
.unwrap_or_else(|| { .unwrap_or_else(|| {
sp.update("Fetching $HOME and building imag path".into());
::std::env::var("HOME") ::std::env::var("HOME")
.map(PathBuf::from) .map(PathBuf::from)
.map(|mut p| { p.push(".imag"); p }) .map(|mut p| { p.push(".imag"); p })
@ -82,6 +92,7 @@ fn main() {
.expect("Failed to retrieve/build path for imag directory.") .expect("Failed to retrieve/build path for imag directory.")
}); });
sp.update("Creating imag directories".into());
let _ = ::std::fs::create_dir_all(path.clone()) let _ = ::std::fs::create_dir_all(path.clone())
.expect("Failed to create directory"); .expect("Failed to create directory");
@ -91,6 +102,7 @@ fn main() {
config_path config_path
}; };
sp.update("Writing configuration file".into());
let _ = OpenOptions::new() let _ = OpenOptions::new()
.write(true) .write(true)
.create(true) .create(true)
@ -107,6 +119,7 @@ fn main() {
}) })
.expect("Failed to open new configuration file"); .expect("Failed to open new configuration file");
sp.update("Checking for git".into());
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..."); println!("Going to initialize a git repository in the imag directory...");
@ -117,6 +130,7 @@ fn main() {
gitignore_path.to_str().map(String::from).expect("Cannot convert path to string") gitignore_path.to_str().map(String::from).expect("Cannot convert path to string")
}; };
sp.update("Writing gitignore".into());
let _ = OpenOptions::new() let _ = OpenOptions::new()
.write(true) .write(true)
.create(true) .create(true)
@ -131,46 +145,43 @@ fn main() {
let worktree = format!("--work-tree={}", path_str); let worktree = format!("--work-tree={}", path_str);
let gitdir = format!("--git-dir={}", path_str); let gitdir = format!("--git-dir={}", path_str);
sp.update("Initializing git repository".into());
{ {
let output = Command::new("git") let output = Command::new("git")
.args(&[&worktree, &gitdir, "--no-pager", "init"]) .args(&[&worktree, &gitdir, "--no-pager", "init"])
.output() .output()
.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")); sp.close();
println!("'git {} {} --no-pager init' succeeded", worktree, gitdir);
} else {
println!("{}", String::from_utf8(output.stderr).expect("No UTF-8 output")); println!("{}", String::from_utf8(output.stderr).expect("No UTF-8 output"));
::std::process::exit(output.status.code().unwrap_or(1)); ::std::process::exit(output.status.code().unwrap_or(1))
} }
} }
sp.update("Adding gitignore to repository".into());
{ {
let output = Command::new("git") let output = Command::new("git")
.args(&[&worktree, &gitdir, "--no-pager", "add", &gitignore_path]) .args(&[&worktree, &gitdir, "--no-pager", "add", &gitignore_path])
.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")); sp.close();
println!("'git {} {} --no-pager add {}' succeeded", worktree, gitdir, gitignore_path);
} else {
println!("{}", String::from_utf8(output.stderr).expect("No UTF-8 output")); println!("{}", String::from_utf8(output.stderr).expect("No UTF-8 output"));
::std::process::exit(output.status.code().unwrap_or(1)); ::std::process::exit(output.status.code().unwrap_or(1))
} }
} }
sp.update("Committing gitignore to repository".into());
{ {
let output = Command::new("git") let output = Command::new("git")
.args(&[&worktree, &gitdir, "--no-pager", "commit", &gitignore_path, "-m", "'Initial import'"]) .args(&[&worktree, &gitdir, "--no-pager", "commit", &gitignore_path, "-m", "'Initial import'"])
.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")); sp.close();
println!("'git {} {} --no-pager commit {} -m 'Initial import'' succeeded", worktree, gitdir, gitignore_path);
} else {
println!("{}", String::from_utf8(output.stderr).expect("No UTF-8 output")); println!("{}", String::from_utf8(output.stderr).expect("No UTF-8 output"));
::std::process::exit(output.status.code().unwrap_or(1)); ::std::process::exit(output.status.code().unwrap_or(1))
} }
} }
@ -179,6 +190,7 @@ fn main() {
println!("No git repository will be initialized"); println!("No git repository will be initialized");
} }
sp.message("Done".into());
println!("Ready. Have fun with imag!"); println!("Ready. Have fun with imag!");
} }