Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement uname and which as builtin functions #124

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ rustyline = { version = "14.0.0", features = ["derive"] }
tokio = "1.40.0"
uu_ls = "0.0.27"
dirs = "5.0.1"
which = "6.0.3"
uu_uname = "0.0.27"

[package.metadata.release]
# Dont publish the binary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ use futures::{future::LocalBoxFuture, FutureExt};
use uu_ls::uumain as uu_ls;

use crate::execute;

pub mod uname;
pub mod which;

pub use uname::UnameCommand;
pub use which::WhichCommand;

pub struct LsCommand;

pub struct AliasCommand;
Expand Down
54 changes: 54 additions & 0 deletions crates/shell/src/commands/uname.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use deno_task_shell::{ExecuteResult, ShellCommand, ShellCommandContext};
use futures::future::LocalBoxFuture;
use uu_uname::{options, UNameOutput};
pub struct UnameCommand;

fn display(uname: &UNameOutput) -> String {
let mut output = String::new();
for name in [
uname.kernel_name.as_ref(),
uname.nodename.as_ref(),
uname.kernel_release.as_ref(),
uname.kernel_version.as_ref(),
uname.machine.as_ref(),
uname.os.as_ref(),
uname.processor.as_ref(),
uname.hardware_platform.as_ref(),
]
.into_iter()
.flatten()
{
output.push_str(name);
output.push(' ');
}
output
}

impl ShellCommand for UnameCommand {
fn execute(&self, mut context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> {
let matches = uu_uname::uu_app()
.no_binary_name(true)
.try_get_matches_from(context.args)
.unwrap();

let options = uu_uname::Options {
all: matches.get_flag(options::ALL),
kernel_name: matches.get_flag(options::KERNEL_NAME),
nodename: matches.get_flag(options::NODENAME),
kernel_release: matches.get_flag(options::KERNEL_RELEASE),
kernel_version: matches.get_flag(options::KERNEL_VERSION),
machine: matches.get_flag(options::MACHINE),
processor: matches.get_flag(options::PROCESSOR),
hardware_platform: matches.get_flag(options::HARDWARE_PLATFORM),
os: matches.get_flag(options::OS),
};

let uname = UNameOutput::new(&options).unwrap();
context
.stdout
.write_line(display(&uname).trim_end())
.unwrap();

Box::pin(futures::future::ready(ExecuteResult::from_exit_code(0)))
}
}
48 changes: 48 additions & 0 deletions crates/shell/src/commands/which.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use deno_task_shell::{ExecuteResult, ShellCommand, ShellCommandContext};
use futures::future::LocalBoxFuture;

pub struct WhichCommand;

impl ShellCommand for WhichCommand {
fn execute(&self, context: ShellCommandContext) -> LocalBoxFuture<'static, ExecuteResult> {
Box::pin(futures::future::ready(execute_which(context)))
}
}

fn execute_which(mut context: ShellCommandContext) -> ExecuteResult {
if context.args.len() != 1 {
context.stderr.write_line("Expected one argument.").unwrap();
return ExecuteResult::from_exit_code(1);
}
let arg = &context.args[0];

if let Some(alias) = context.state.alias_map().get(arg) {
context
.stdout
.write_line(&format!("alias: \"{}\"", alias.join(" ")))
.unwrap();
return ExecuteResult::from_exit_code(0);
}

if context.state.resolve_custom_command(arg).is_some() {
context.stdout.write_line("<builtin function>").unwrap();
return ExecuteResult::from_exit_code(0);
}

if let Some(path) = context.state.env_vars().get("PATH") {
let path = std::ffi::OsString::from(path);
let which_result = which::which_in_global(arg, Some(path))
.and_then(|mut i| i.next().ok_or(which::Error::CannotFindBinaryPath));

if let Ok(p) = which_result {
context.stdout.write_line(&p.to_string_lossy()).unwrap();
return ExecuteResult::from_exit_code(0);
}
}

context
.stderr
.write_line(&format!("{} not found", arg))
.unwrap();
ExecuteResult::from_exit_code(1)
}
8 changes: 8 additions & 0 deletions crates/shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ fn commands() -> HashMap<String, Rc<dyn ShellCommand>> {
"source".to_string(),
Rc::new(commands::SourceCommand) as Rc<dyn ShellCommand>,
),
(
"which".to_string(),
Rc::new(commands::WhichCommand) as Rc<dyn ShellCommand>,
),
(
"uname".to_string(),
Rc::new(commands::UnameCommand) as Rc<dyn ShellCommand>,
),
])
}

Expand Down
Loading