use nu_engine::command_prelude::*;
use nu_protocol::shell_error::generic::GenericError;
use uucore::{localized_help_template, translate};
#[derive(Clone)]
pub struct Whoami;
impl Command for Whoami {
fn name(&self) -> &str {
"whoami"
}
fn description(&self) -> &str {
"Get the current username using uutils/coreutils whoami."
}
fn signature(&self) -> Signature {
Signature::build("whoami")
.input_output_types(vec![(Type::Nothing, Type::String)])
.allow_variants_without_examples(true)
.category(Category::Platform)
}
fn search_terms(&self) -> Vec<&str> {
vec!["username", "coreutils"]
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let _ = localized_help_template("touch");
let output = match uu_whoami::whoami() {
Ok(username) => username.to_string_lossy().to_string(),
Err(e) => {
return Err(ShellError::Generic(GenericError::new(
format!("{e}"),
translate!(&e.to_string()),
call.head,
)));
}
};
Ok(Value::string(output, call.head).into_pipeline_data())
}
fn examples(&self) -> Vec<Example<'_>> {
vec![Example {
description: "Get the current username.",
example: "whoami",
result: None,
}]
}
}
#[cfg(test)]
mod tests {
use super::Whoami;
#[test]
fn examples_work_as_expected() -> nu_test_support::Result {
nu_test_support::test().examples(Whoami)
}
}