diff --git a/src/uu/pinky/locales/en-US.ftl b/src/uu/pinky/locales/en-US.ftl index 65b12d0cae2..eba1148b7c2 100644 --- a/src/uu/pinky/locales/en-US.ftl +++ b/src/uu/pinky/locales/en-US.ftl @@ -19,6 +19,7 @@ pinky-help-omit-headings = omit the line of column headings in short format pinky-help-omit-name = omit the user's full name in short format pinky-help-omit-name-host = omit the user's full name and remote host in short format pinky-help-omit-name-host-time = omit the user's full name, remote host and idle time in short format +pinky-help-lookup = attempt to canonicalize hostnames via DNS pinky-help-help = Print help information # Column headers for short format diff --git a/src/uu/pinky/locales/fr-FR.ftl b/src/uu/pinky/locales/fr-FR.ftl index 3be28ec7a17..ee37681c940 100644 --- a/src/uu/pinky/locales/fr-FR.ftl +++ b/src/uu/pinky/locales/fr-FR.ftl @@ -19,6 +19,7 @@ pinky-help-omit-headings = omettre la ligne des en-têtes de colonnes en format pinky-help-omit-name = omettre le nom complet de l'utilisateur en format court pinky-help-omit-name-host = omettre le nom complet et l'hôte distant de l'utilisateur en format court pinky-help-omit-name-host-time = omettre le nom complet, l'hôte distant et le temps d'inactivité de l'utilisateur en format court +pinky-help-lookup = tenter de donner un forme canonique aux noms d'hôte avec DNS pinky-help-help = Afficher les informations d'aide # En-têtes de colonnes pour le format court diff --git a/src/uu/pinky/src/pinky.rs b/src/uu/pinky/src/pinky.rs index 942695654db..1ba9d5a5109 100644 --- a/src/uu/pinky/src/pinky.rs +++ b/src/uu/pinky/src/pinky.rs @@ -13,6 +13,7 @@ mod platform; mod options { pub const LONG_FORMAT: &str = "long_format"; + pub const LOOKUP: &str = "lookup"; pub const OMIT_HOME_DIR: &str = "omit_home_dir"; pub const OMIT_PROJECT_FILE: &str = "omit_project_file"; pub const OMIT_PLAN_FILE: &str = "omit_plan_file"; @@ -101,6 +102,12 @@ pub fn uu_app() -> Command { .action(ArgAction::Append) .value_hint(clap::ValueHint::Username), ) + .arg( + Arg::new(options::LOOKUP) + .long(options::LOOKUP) + .help(translate!("pinky-help-lookup")) + .action(ArgAction::SetTrue), + ) .arg( // Redefine the help argument to not include the short flag // since that conflicts with omit_project_file. diff --git a/src/uu/pinky/src/platform/unix.rs b/src/uu/pinky/src/platform/unix.rs index 1a749e6b9e2..8acc4cf2aa0 100644 --- a/src/uu/pinky/src/platform/unix.rs +++ b/src/uu/pinky/src/platform/unix.rs @@ -64,6 +64,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // if true, use the "short" output format. let do_short_format = !matches.get_flag(options::LONG_FORMAT); + // If true, attempt to canonicalize hostname via a DNS lookup. + let do_lookup = matches.get_flag(options::LOOKUP); + /* if true, display the ut_host field. */ let mut include_where = true; @@ -81,6 +84,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } let pk = Pinky { + do_lookup, include_idle, include_heading, include_fullname, @@ -103,6 +107,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } struct Pinky { + do_lookup: bool, include_idle: bool, include_heading: bool, include_fullname: bool, @@ -206,10 +211,16 @@ impl Pinky { print!(" {}", time_string(ut)); - let mut s = ut.host(); - if self.include_where && !s.is_empty() { - s = ut.canon_host()?; - print!(" {s}"); + if self.include_where { + let s: String = if self.do_lookup { + ut.canon_host().unwrap_or(ut.host()) + } else { + ut.host() + }; + + if !s.is_empty() { + print!(" {s}"); + } } println!(); diff --git a/tests/by-util/test_pinky.rs b/tests/by-util/test_pinky.rs index 6418906ae55..8a58c3f1ea3 100644 --- a/tests/by-util/test_pinky.rs +++ b/tests/by-util/test_pinky.rs @@ -90,6 +90,19 @@ fn test_short_format_i() { assert_eq!(v_actual, v_expect); } +#[cfg(unix)] +#[test] +#[cfg(not(target_os = "openbsd"))] +fn test_lookup() { + let args = ["--lookup"]; + let ts = TestScenario::new(util_name!()); + let actual = ts.ucmd().args(&args).succeeds().stdout_move_str(); + let expect = unwrap_or_return!(expected_result(&ts, &[])).stdout_move_str(); + let v_actual: Vec<&str> = actual.split_whitespace().collect(); + let v_expect: Vec<&str> = expect.split_whitespace().collect(); + assert_eq!(v_actual, v_expect); +} + #[cfg(unix)] #[test] #[cfg(not(target_os = "openbsd"))]