Skip to content

Commit 68a0369

Browse files
authored
Merge pull request #8669 from bakanovskii/add-pinky-lookup-flag
pinky: add --lookup flag
2 parents e6ebc9b + 59bf18f commit 68a0369

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

src/uu/pinky/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pinky-help-omit-headings = omit the line of column headings in short format
1919
pinky-help-omit-name = omit the user's full name in short format
2020
pinky-help-omit-name-host = omit the user's full name and remote host in short format
2121
pinky-help-omit-name-host-time = omit the user's full name, remote host and idle time in short format
22+
pinky-help-lookup = attempt to canonicalize hostnames via DNS
2223
pinky-help-help = Print help information
2324
2425
# Column headers for short format

src/uu/pinky/locales/fr-FR.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pinky-help-omit-headings = omettre la ligne des en-têtes de colonnes en format
1919
pinky-help-omit-name = omettre le nom complet de l'utilisateur en format court
2020
pinky-help-omit-name-host = omettre le nom complet et l'hôte distant de l'utilisateur en format court
2121
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
22+
pinky-help-lookup = tenter de donner un forme canonique aux noms d'hôte avec DNS
2223
pinky-help-help = Afficher les informations d'aide
2324
2425
# En-têtes de colonnes pour le format court

src/uu/pinky/src/pinky.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod platform;
1313

1414
mod options {
1515
pub const LONG_FORMAT: &str = "long_format";
16+
pub const LOOKUP: &str = "lookup";
1617
pub const OMIT_HOME_DIR: &str = "omit_home_dir";
1718
pub const OMIT_PROJECT_FILE: &str = "omit_project_file";
1819
pub const OMIT_PLAN_FILE: &str = "omit_plan_file";
@@ -101,6 +102,12 @@ pub fn uu_app() -> Command {
101102
.action(ArgAction::Append)
102103
.value_hint(clap::ValueHint::Username),
103104
)
105+
.arg(
106+
Arg::new(options::LOOKUP)
107+
.long(options::LOOKUP)
108+
.help(translate!("pinky-help-lookup"))
109+
.action(ArgAction::SetTrue),
110+
)
104111
.arg(
105112
// Redefine the help argument to not include the short flag
106113
// since that conflicts with omit_project_file.

src/uu/pinky/src/platform/unix.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6464
// if true, use the "short" output format.
6565
let do_short_format = !matches.get_flag(options::LONG_FORMAT);
6666

67+
// If true, attempt to canonicalize hostname via a DNS lookup.
68+
let do_lookup = matches.get_flag(options::LOOKUP);
69+
6770
/* if true, display the ut_host field. */
6871
let mut include_where = true;
6972

@@ -81,6 +84,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8184
}
8285

8386
let pk = Pinky {
87+
do_lookup,
8488
include_idle,
8589
include_heading,
8690
include_fullname,
@@ -103,6 +107,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
103107
}
104108

105109
struct Pinky {
110+
do_lookup: bool,
106111
include_idle: bool,
107112
include_heading: bool,
108113
include_fullname: bool,
@@ -215,10 +220,16 @@ impl Pinky {
215220

216221
print!(" {}", time_string(ut));
217222

218-
let mut s = ut.host();
219-
if self.include_where && !s.is_empty() {
220-
s = ut.canon_host()?;
221-
print!(" {s}");
223+
if self.include_where {
224+
let s: String = if self.do_lookup {
225+
ut.canon_host().unwrap_or(ut.host())
226+
} else {
227+
ut.host()
228+
};
229+
230+
if !s.is_empty() {
231+
print!(" {s}");
232+
}
222233
}
223234

224235
println!();

tests/by-util/test_pinky.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ fn test_short_format_i() {
9090
assert_eq!(v_actual, v_expect);
9191
}
9292

93+
#[cfg(unix)]
94+
#[test]
95+
#[cfg(not(target_os = "openbsd"))]
96+
fn test_lookup() {
97+
let args = ["--lookup"];
98+
let ts = TestScenario::new(util_name!());
99+
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str();
100+
let expect = unwrap_or_return!(expected_result(&ts, &[])).stdout_move_str();
101+
let v_actual: Vec<&str> = actual.split_whitespace().collect();
102+
let v_expect: Vec<&str> = expect.split_whitespace().collect();
103+
assert_eq!(v_actual, v_expect);
104+
}
105+
93106
#[cfg(unix)]
94107
#[test]
95108
#[cfg(not(target_os = "openbsd"))]

0 commit comments

Comments
 (0)