Skip to content

Commit

Permalink
use failure for better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Timm Behner committed Jun 20, 2019
1 parent 0f20e23 commit f089955
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ git2 = "0.8"
regex = "1.1.6"
tico = "1.0.0"
nix = "0.14"
failure = "0.1.5"
21 changes: 12 additions & 9 deletions src/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::{App, Arg, ArgMatches, SubCommand};
use std::env;
use nix::unistd;
use failure::Error;

const INSERT_SYMBOL: &str = "❯";
const COMMAND_SYMBOL: &str = "⬢";
Expand All @@ -9,17 +10,15 @@ const NO_ERROR: &str = "0";
const SSH_SESSION_ENV: &str = "SSH_TTY";


fn get_username() -> String {
match env::var("USER") {
Ok(name) => name,
Err(_) => "".to_string(),
}
fn get_username() -> Result<String, Error> {
Ok(env::var("USER")?)
}

fn get_hostname() -> String {
fn get_hostname() -> Result<String, Error> {
let mut buf = [0u8; 64];
let hostname_cstr = unistd::gethostname(&mut buf).expect("Failed getting hostname");
hostname_cstr.to_str().expect("Hostname wasn't valid UTF-8").to_string()
let hostname_cstr = unistd::gethostname(&mut buf)?;
let hostname = hostname_cstr.to_str()?;
Ok(hostname.to_string())
}

pub fn display(sub_matches: &ArgMatches<'_>) {
Expand All @@ -44,7 +43,11 @@ pub fn display(sub_matches: &ArgMatches<'_>) {
};

let ssh_user_host = match env::var(SSH_SESSION_ENV) {
Ok(_) => format!("{}@{} ", get_username(), get_hostname()),
Ok(_) => {
let username = get_username().unwrap_or("".to_string());
let hostname = get_hostname().unwrap_or("".to_string());
format!("{}@{} ", username, hostname)
},
Err(_) => "".to_string(),
};

Expand Down

0 comments on commit f089955

Please sign in to comment.