Skip to content

Commit

Permalink
Merge pull request #1239 from habitat-sh/fnichol/final-hab-ui-adds
Browse files Browse the repository at this point in the history
[hab] Final UI additions to control coloring/formatting for errors and analytics
  • Loading branch information
reset authored Sep 15, 2016
2 parents 6a96909 + 77cb638 commit 56b8ddd
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 70 deletions.
40 changes: 39 additions & 1 deletion components/common/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum Status {
Applying,
Cached,
Creating,
Deleting,
Downloading,
Encrypting,
Installed,
Expand All @@ -48,10 +49,11 @@ impl Status {
Status::Applying => ('↑', "Applying".into(), Colour::Green),
Status::Cached => ('☑', "Cached".into(), Colour::Green),
Status::Creating => ('Ω', "Creating".into(), Colour::Green),
Status::Deleting => ('☒', "Deleting".into(), Colour::Green),
Status::Downloading => ('↓', "Downloading".into(), Colour::Green),
Status::Encrypting => ('☛', "Encypting".into(), Colour::Green),
Status::Installed => ('✓', "Installed".into(), Colour::Green),
Status::Missing => ('∵', "Missing".into(), Colour::Cyan),
Status::Missing => ('∵', "Missing".into(), Colour::Red),
Status::Signed => ('✓', "Signed".into(), Colour::Cyan),
Status::Signing => ('☛', "Signing".into(), Colour::Cyan),
Status::Uploaded => ('✓', "Uploaded".into(), Colour::Green),
Expand Down Expand Up @@ -102,6 +104,42 @@ impl UI {
Ok(())
}

pub fn warn<T: fmt::Display>(&mut self, message: T) -> Result<()> {
let ref mut stream = self.shell.err;
match stream.is_colored() {
true => {
try!(write!(stream,
"{}\n",
Colour::Yellow.bold().paint(format!("∅ {}", message.to_string()))));
}
false => {
try!(write!(stream, "∅ {}\n", message.to_string()));
}
}
try!(stream.flush());
Ok(())
}

pub fn fatal<T: fmt::Display>(&mut self, message: T) -> Result<()> {
let ref mut stream = self.shell.err;
match stream.is_colored() {
true => {
try!(write!(stream,
"{}\n",
Colour::Red.bold()
.paint(format!("✗✗✗\n✗✗✗ {}\n✗✗✗",
message.to_string()))));
}
false => {
try!(write!(stream,
"✗✗✗\n✗✗✗ {}\n✗✗✗\n",
message.to_string()));
}
}
try!(stream.flush());
Ok(())
}

pub fn progress(&mut self) -> Option<ProgressBar> {
if self.shell.out.is_a_terminal() {
Some(ProgressBar::default())
Expand Down
23 changes: 11 additions & 12 deletions components/hab/src/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ use std::io::{Read, Write};
use std::path::Path;
use std::time::{UNIX_EPOCH, SystemTime};

use ansi_term::Colour::{Blue, Green, Yellow};
use clap;
use common::ui::{Status, UI};
use hcore;
use http_client::ApiClient;
use url::Url;
Expand Down Expand Up @@ -343,23 +343,23 @@ pub fn instrument_clap_error(err: &clap::Error) {
/// * If the parent directory cannot be created
/// * If an opt-out if exists but cannot be deleted
/// * If an opt-in file cannot be created
pub fn opt_in(analytics_path: &Path, origin_generated: bool) -> Result<()> {
println!("{}", Yellow.bold().paint(Opting in to analytics"));
pub fn opt_in(ui: &mut UI, analytics_path: &Path, origin_generated: bool) -> Result<()> {
try!(ui.begin("Opting in to analytics"));
// Create the parent directory which will contain the opt-in file
try!(fs::create_dir_all(analytics_path));
// Get the path to the opt-out file
let opt_out_path = analytics_path.join(OPTED_OUT_METAFILE);
// If the opt-out file exists, delete it from disk
if opt_out_path.exists() {
println!("{} {}", Green.paint("☒ Deleting"), opt_out_path.display());
try!(ui.status(Status::Deleting, opt_out_path.display()));
try!(fs::remove_file(&opt_out_path));
}
// Get the path to the opt-in file
let opt_in_path = analytics_path.join(OPTED_IN_METAFILE);
println!("{} {}", Green.paint("☑ Creating"), opt_in_path.display());
try!(ui.status(Status::Creating, opt_in_path.display()));
// Create the opt-in file
let _ = try!(File::create(opt_in_path));
println!("{}", Blue.paint("★ Analytics opted in, thank you!"));
try!(ui.end("Analytics opted in, thank you!"));
// Record an event that the setup subcommand was invoked
record_event(Event::Subcommand,
&format!("{}--{}--{}", PRODUCT, "cli", "setup"));
Expand All @@ -386,24 +386,23 @@ pub fn opt_in(analytics_path: &Path, origin_generated: bool) -> Result<()> {
/// * If the parent directory cannot be created
/// * If an opt-in if exists but cannot be deleted
/// * If an opt-out file cannot be created
pub fn opt_out(analytics_path: &Path) -> Result<()> {
println!("{}", Yellow.bold().paint(Opting out of analytics"));
pub fn opt_out(ui: &mut UI, analytics_path: &Path) -> Result<()> {
try!(ui.begin("Opting out of analytics"));
// Create the parent directory which will contain the opt-in file
try!(fs::create_dir_all(analytics_path));
// Get the path to the opt-in file
let opt_in_path = analytics_path.join(OPTED_IN_METAFILE);
// If the opt-in file exists, delete it from disk
if opt_in_path.exists() {
println!("{} {}", Green.paint("☒ Deleting"), opt_in_path.display());
try!(ui.status(Status::Deleting, opt_in_path.display()));
try!(fs::remove_file(&opt_in_path));
}
// Get the path to the opt-out file
let opt_out_path = analytics_path.join(OPTED_OUT_METAFILE);
println!("{} {}", Green.paint("☑ Creating"), opt_out_path.display());
try!(ui.status(Status::Creating, opt_out_path.display()));
// Create the opt-out file
let _ = try!(File::create(opt_out_path));
println!("{}",
Blue.paint("★ Analytics opted out, we salute you just the same!"));
try!(ui.end("Analytics opted out, we salute you just the same!"));
// Return an empty Ok, representing a successful operation
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions components/hab/src/command/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ pub mod setup {
}

fn opt_in_analytics(ui: &mut UI, analytics_path: &Path, generated_origin: bool) -> Result<()> {
let result = analytics::opt_in(analytics_path, generated_origin);
let result = analytics::opt_in(ui, analytics_path, generated_origin);
try!(ui.br());
result
}

fn opt_out_analytics(ui: &mut UI, analytics_path: &Path) -> Result<()> {
let result = analytics::opt_out(analytics_path);
let result = analytics::opt_out(ui, analytics_path);
try!(ui.br());
result
}
Expand Down
1 change: 0 additions & 1 deletion components/hab/src/command/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ pub mod key {
Ok(())
}

// TODO fin: use UI to signal whether or not to use progress bars
fn download_key(ui: &mut UI,
depot_client: &Client,
nwr: &str,
Expand Down
33 changes: 14 additions & 19 deletions components/hab/src/command/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ pub mod export {
inner::start(ui, ident, format)
}

pub fn format_for(value: &str) -> Result<ExportFormat> {
inner::format_for(value)
pub fn format_for(ui: &mut UI, value: &str) -> Result<ExportFormat> {
inner::format_for(ui, value)
}

#[cfg(target_os = "linux")]
Expand All @@ -182,7 +182,7 @@ pub mod export {
use error::{Error, Result};
use super::ExportFormat;

pub fn format_for(value: &str) -> Result<ExportFormat> {
pub fn format_for(_ui: &mut UI, value: &str) -> Result<ExportFormat> {
match value {
"docker" => {
let format = ExportFormat {
Expand Down Expand Up @@ -239,31 +239,28 @@ pub mod export {

#[cfg(not(target_os = "linux"))]
mod inner {
use ansi_term::Colour::Yellow;
use error::{Error, Result};
use common::UI;
use hcore::package::PackageIdent;
use std::env;
use super::ExportFormat;

// TODO fin: add a warning or error method on `UI` to handle messages below
pub fn format_for(value: &str) -> Result<ExportFormat> {
let msg = format!("∅ Exporting {} packages from this operating system is not yet \
pub fn format_for(ui: &mut UI, value: &str) -> Result<ExportFormat> {
try!(ui.warn(format!("∅ Exporting {} packages from this operating system is not yet \
supported. Try running this command again on a 64-bit Linux \
operating system.\n",
value);
println!("{}", Yellow.bold().paint(msg));
value)));
try!(ui.br());
let e = Error::UnsupportedExportFormat(value.to_string());
Err(e)
}

pub fn start(_ui: &mut UI, _ident: &PackageIdent, _format: &ExportFormat) -> Result<()> {
let subcmd = env::args().nth(1).unwrap_or("<unknown>".to_string());
let subsubcmd = env::args().nth(2).unwrap_or("<unknown>".to_string());
let msg = format!("∅ Exporting packages from this operating system is not yet \
supported. Try running this command again on a 64-bit Linux \
operating system.\n");
println!("{}", Yellow.bold().paint(msg));
try!(ui.warn("Exporting packages from this operating system is not yet supported. Try \
running this command again on a 64-bit Linux operating system."));
try!(ui.br());
Err(Error::SubcommandNotSupported(format!("{} {}", subcmd, subsubcmd)))

}
Expand Down Expand Up @@ -442,7 +439,6 @@ pub mod upload {
use std::path::{Path, PathBuf};

use ansi_term::Colour::Red;
use common::ui::{Status, UI};
use depot_client::{self, Client};
use hcore::crypto::artifact::get_artifact_header;
Expand Down Expand Up @@ -580,11 +576,10 @@ pub mod upload {
}
}
} else {
// TODO fin: replace with a warning or error `UI` method call
println!("{} artifact for {} was not found in {}",
Red.bold().paint("✗ Missing"),
ident.archive_name().unwrap(),
archives_dir.display());
try!(ui.status(Status::Missing,
format!("artifact for {} was not found in {}",
ident.archive_name().unwrap(),
archives_dir.display())));
return Err(Error::FileNotFound(archives_dir.to_string_lossy()
.into_owned()));
}
Expand Down
8 changes: 3 additions & 5 deletions components/hab/src/command/sup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,15 @@ mod inner {
use std::env;
use std::ffi::OsString;

use ansi_term::Colour::Yellow;
use common::ui::UI;

use error::{Error, Result};

pub fn start(_ui: &mut UI, _args: Vec<OsString>) -> Result<()> {
let subcmd = env::args().nth(1).unwrap_or("<unknown>".to_string());
let msg = format!("∅ Launching a native Supervisor on this operating system is not yet \
supported. Try running this command again on a 64-bit Linux \
operating system.\n");
println!("{}", Yellow.bold().paint(msg));
try!(ui.warn("Launching a native Supervisor on this operating system is not yet supported. \
Try running this command again on a 64-bit Linux operating system."));
try!(ui.br());
Err(Error::SubcommandNotSupported(subcmd))
}
}
Loading

0 comments on commit 56b8ddd

Please sign in to comment.