Skip to content

Commit

Permalink
[hab] Add warn & fatal methods to UI to remove remaining coloring.
Browse files Browse the repository at this point in the history
This change removes the final raw colored `println!()` calls in favor of
2 new methods: `UI#fatal()` and `UI#warn()`.

Signed-off-by: Fletcher Nichol <[email protected]>
  • Loading branch information
fnichol committed Sep 15, 2016
1 parent a651029 commit 77cb638
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 56 deletions.
38 changes: 37 additions & 1 deletion components/common/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Status {
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 @@ -104,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
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))
}
}
57 changes: 27 additions & 30 deletions components/hab/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate ansi_term;
#[macro_use]
extern crate clap;
extern crate env_logger;
Expand All @@ -29,7 +28,6 @@ use std::path::Path;
use std::str::FromStr;
use std::thread;

use ansi_term::Colour::Red;
use clap::ArgMatches;

use common::ui::UI;
Expand Down Expand Up @@ -62,17 +60,16 @@ const MAX_FILE_UPLOAD_SIZE_BYTES: u64 = 4096;

fn main() {
env_logger::init().unwrap();
let mut ui = UI::default();
thread::spawn(|| analytics::instrument_subcommand());
if let Err(e) = start() {
println!("{}",
Red.bold().paint(format!("✗✗✗\n✗✗✗ {}\n✗✗✗", e)));
if let Err(e) = start(&mut ui) {
ui.fatal(e).unwrap();
std::process::exit(1)
}
}

fn start() -> Result<()> {
let mut ui = UI::default();
try!(exec_subcommand_if_called(&mut ui));
fn start(ui: &mut UI) -> Result<()> {
try!(exec_subcommand_if_called(ui));

let (args, remaining_args) = raw_parse_args();
debug!("clap cli args: {:?}", &args);
Expand All @@ -83,35 +80,35 @@ fn start() -> Result<()> {
e.exit();
});
match app_matches.subcommand() {
("apply", Some(m)) => try!(sub_config_apply(&mut ui, m)),
("apply", Some(m)) => try!(sub_config_apply(ui, m)),
("cli", Some(matches)) => {
match matches.subcommand() {
("setup", Some(_)) => try!(sub_cli_setup(&mut ui)),
("setup", Some(_)) => try!(sub_cli_setup(ui)),
_ => unreachable!(),
}
}
("config", Some(matches)) => {
match matches.subcommand() {
("apply", Some(m)) => try!(sub_config_apply(&mut ui, m)),
("apply", Some(m)) => try!(sub_config_apply(ui, m)),
_ => unreachable!(),
}
}
("file", Some(matches)) => {
match matches.subcommand() {
("upload", Some(m)) => try!(sub_file_upload(&mut ui, m)),
("upload", Some(m)) => try!(sub_file_upload(ui, m)),
_ => unreachable!(),
}
}
("install", Some(m)) => try!(sub_pkg_install(&mut ui, m)),
("install", Some(m)) => try!(sub_pkg_install(ui, m)),
("origin", Some(matches)) => {
match matches.subcommand() {
("key", Some(m)) => {
match m.subcommand() {
("download", Some(sc)) => try!(sub_origin_key_download(&mut ui, sc)),
("download", Some(sc)) => try!(sub_origin_key_download(ui, sc)),
("export", Some(sc)) => try!(sub_origin_key_export(sc)),
("generate", Some(sc)) => try!(sub_origin_key_generate(&mut ui, sc)),
("import", Some(_)) => try!(sub_origin_key_import(&mut ui)),
("upload", Some(sc)) => try!(sub_origin_key_upload(&mut ui, sc)),
("generate", Some(sc)) => try!(sub_origin_key_generate(ui, sc)),
("import", Some(_)) => try!(sub_origin_key_import(ui)),
("upload", Some(sc)) => try!(sub_origin_key_upload(ui, sc)),
_ => unreachable!(),
}
}
Expand All @@ -120,18 +117,18 @@ fn start() -> Result<()> {
}
("pkg", Some(matches)) => {
match matches.subcommand() {
("binlink", Some(m)) => try!(sub_pkg_binlink(&mut ui, m)),
("build", Some(m)) => try!(sub_pkg_build(&mut ui, m)),
("binlink", Some(m)) => try!(sub_pkg_binlink(ui, m)),
("build", Some(m)) => try!(sub_pkg_build(ui, m)),
("exec", Some(m)) => try!(sub_pkg_exec(m, remaining_args)),
("export", Some(m)) => try!(sub_pkg_export(&mut ui, m)),
("export", Some(m)) => try!(sub_pkg_export(ui, m)),
("hash", Some(m)) => try!(sub_pkg_hash(m)),
("install", Some(m)) => try!(sub_pkg_install(&mut ui, m)),
("install", Some(m)) => try!(sub_pkg_install(ui, m)),
("path", Some(m)) => try!(sub_pkg_path(m)),
("provides", Some(m)) => try!(sub_pkg_provides(m)),
("search", Some(m)) => try!(sub_pkg_search(m)),
("sign", Some(m)) => try!(sub_pkg_sign(&mut ui, m)),
("upload", Some(m)) => try!(sub_pkg_upload(&mut ui, m)),
("verify", Some(m)) => try!(sub_pkg_verify(&mut ui, m)),
("sign", Some(m)) => try!(sub_pkg_sign(ui, m)),
("upload", Some(m)) => try!(sub_pkg_upload(ui, m)),
("verify", Some(m)) => try!(sub_pkg_verify(ui, m)),
_ => unreachable!(),
}
}
Expand All @@ -140,8 +137,8 @@ fn start() -> Result<()> {
("key", Some(m)) => {
match m.subcommand() {
("export", Some(sc)) => try!(sub_ring_key_export(sc)),
("import", Some(_)) => try!(sub_ring_key_import(&mut ui)),
("generate", Some(sc)) => try!(sub_ring_key_generate(&mut ui, sc)),
("import", Some(_)) => try!(sub_ring_key_import(ui)),
("generate", Some(sc)) => try!(sub_ring_key_generate(ui, sc)),
_ => unreachable!(),
}
}
Expand All @@ -152,19 +149,19 @@ fn start() -> Result<()> {
match matches.subcommand() {
("key", Some(m)) => {
match m.subcommand() {
("generate", Some(sc)) => try!(sub_service_key_generate(&mut ui, sc)),
("generate", Some(sc)) => try!(sub_service_key_generate(ui, sc)),
_ => unreachable!(),
}
}
_ => unreachable!(),
}
}
("setup", Some(_)) => try!(sub_cli_setup(&mut ui)),
("setup", Some(_)) => try!(sub_cli_setup(ui)),
("user", Some(matches)) => {
match matches.subcommand() {
("key", Some(m)) => {
match m.subcommand() {
("generate", Some(sc)) => try!(sub_user_key_generate(&mut ui, sc)),
("generate", Some(sc)) => try!(sub_user_key_generate(ui, sc)),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -396,7 +393,7 @@ fn sub_pkg_exec(m: &ArgMatches, cmd_args: Vec<OsString>) -> Result<()> {
fn sub_pkg_export(ui: &mut UI, m: &ArgMatches) -> Result<()> {
let ident = try!(PackageIdent::from_str(m.value_of("PKG_IDENT").unwrap()));
let format = &m.value_of("FORMAT").unwrap();
let export_fmt = try!(command::pkg::export::format_for(&format));
let export_fmt = try!(command::pkg::export::format_for(ui, &format));
command::pkg::export::start(ui, &ident, &export_fmt)
}

Expand Down

0 comments on commit 77cb638

Please sign in to comment.