Add link
applet
This commit is contained in:
parent
2f58e82de2
commit
76e7036da9
@ -1,5 +1,5 @@
|
||||
use clap::Command;
|
||||
use super::Cmd;
|
||||
use clap::Command;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Clear;
|
||||
|
@ -30,12 +30,8 @@ impl Cmd for Groups {
|
||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")))
|
||||
};
|
||||
let groups = match matches.get_one::<String>("user") {
|
||||
Some(u) => {
|
||||
pw::get_group_names_for_name(&u)?
|
||||
},
|
||||
None => {
|
||||
pw::get_group_names()?
|
||||
},
|
||||
Some(u) => pw::get_group_names_for_name(&u)?,
|
||||
None => pw::get_group_names()?,
|
||||
};
|
||||
let len = groups.len();
|
||||
for (idx, group) in groups.into_iter().enumerate() {
|
||||
|
60
src/cmd/link/mod.rs
Normal file
60
src/cmd/link/mod.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use super::Cmd;
|
||||
use clap::{Arg, ArgAction, Command};
|
||||
use std::{fs, io};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Link;
|
||||
|
||||
impl Cmd for Link {
|
||||
fn name(&self) -> &str {
|
||||
"link"
|
||||
}
|
||||
|
||||
fn cli(&self) -> clap::Command {
|
||||
Command::new("link")
|
||||
.about("call the link function to create a link to a file")
|
||||
.author("Nathan Fisher")
|
||||
.version(env!("CARGO_PKG_VERSION"))
|
||||
.args([
|
||||
Arg::new("file1").required(true).index(1),
|
||||
Arg::new("file2").required(true).index(2),
|
||||
Arg::new("verbose")
|
||||
.short('v')
|
||||
.long("verbose")
|
||||
.action(ArgAction::SetTrue),
|
||||
])
|
||||
}
|
||||
|
||||
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let Some(matches) = matches else {
|
||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")));
|
||||
};
|
||||
let f1 = match matches.get_one::<String>("file1") {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
return Err(Box::new(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"missing file 1",
|
||||
)))
|
||||
}
|
||||
};
|
||||
let f2 = match matches.get_one::<String>("file2") {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
return Err(Box::new(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"missing file 2",
|
||||
)))
|
||||
}
|
||||
};
|
||||
fs::hard_link(f1, f2)?;
|
||||
if matches.get_flag("verbose") {
|
||||
println!("{f2} -> {f1}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn path(&self) -> Option<crate::Path> {
|
||||
Some(crate::Path::UsrBin)
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@ mod getty;
|
||||
pub mod groups;
|
||||
pub mod head;
|
||||
pub mod hostname;
|
||||
pub mod link;
|
||||
mod ln;
|
||||
mod ls;
|
||||
pub mod mountpoint;
|
||||
@ -40,9 +41,10 @@ pub mod yes;
|
||||
|
||||
pub use {
|
||||
self::hostname::Hostname, base32::Base32, base64::Base64, basename::Basename,
|
||||
bootstrap::Bootstrap, clear::Clear, dirname::Dirname, echo::Echo, factor::Factor, fold::Fold, groups::Groups,
|
||||
head::Head, mountpoint::Mountpoint, nologin::Nologin, nproc::Nproc, r#false::False,
|
||||
r#true::True, rev::Rev, shitbox::Shitbox, sleep::Sleep, which::Which, whoami::Whoami, yes::Yes,
|
||||
bootstrap::Bootstrap, clear::Clear, dirname::Dirname, echo::Echo, factor::Factor, fold::Fold,
|
||||
groups::Groups, head::Head, link::Link, mountpoint::Mountpoint, nologin::Nologin, nproc::Nproc,
|
||||
r#false::False, r#true::True, rev::Rev, shitbox::Shitbox, sleep::Sleep, which::Which,
|
||||
whoami::Whoami, yes::Yes,
|
||||
};
|
||||
|
||||
pub trait Cmd: fmt::Debug + Sync {
|
||||
@ -66,6 +68,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
|
||||
"fold" => Some(Box::new(Fold::default())),
|
||||
"groups" => Some(Box::new(Groups::default())),
|
||||
"head" => Some(Box::new(Head::default())),
|
||||
"link" => Some(Box::new(Link::default())),
|
||||
"mountpoint" => Some(Box::new(Mountpoint::default())),
|
||||
"nologin" => Some(Box::new(Nologin::default())),
|
||||
"nproc" => Some(Box::new(Nproc::default())),
|
||||
@ -80,7 +83,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
|
||||
}
|
||||
}
|
||||
|
||||
pub static COMMANDS: [&'static str; 23] = [
|
||||
pub static COMMANDS: [&'static str; 24] = [
|
||||
"base32",
|
||||
"base64",
|
||||
"basename",
|
||||
@ -94,6 +97,7 @@ pub static COMMANDS: [&'static str; 23] = [
|
||||
"groups",
|
||||
"head",
|
||||
"hostname",
|
||||
"link",
|
||||
"mountpoint",
|
||||
"nologin",
|
||||
"nproc",
|
||||
|
@ -1,4 +1,8 @@
|
||||
use std::{error::Error, ffi::{CStr, CString, c_int}, num, io};
|
||||
use std::{
|
||||
error::Error,
|
||||
ffi::{c_int, CStr, CString},
|
||||
io, num,
|
||||
};
|
||||
|
||||
pub fn get_username<'a>() -> Result<&'a str, std::str::Utf8Error> {
|
||||
let user = unsafe {
|
||||
@ -88,7 +92,12 @@ pub fn get_gids() -> Result<Vec<u32>, num::TryFromIntError> {
|
||||
pub fn get_gids_for_name(name: &str) -> Result<Vec<u32>, Box<dyn Error>> {
|
||||
let gid = match get_pgid_for_name(name) {
|
||||
Some(g) => g as libc::gid_t,
|
||||
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "cannot get primary group"))),
|
||||
None => {
|
||||
return Err(Box::new(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"cannot get primary group",
|
||||
)))
|
||||
}
|
||||
};
|
||||
let mut buf: Vec<libc::gid_t> = vec![0; 1024];
|
||||
let name = CString::new(name.as_bytes())?;
|
||||
@ -96,7 +105,10 @@ pub fn get_gids_for_name(name: &str) -> Result<Vec<u32>, Box<dyn Error>> {
|
||||
unsafe {
|
||||
let res = libc::getgrouplist(name.as_ptr(), gid, buf.as_mut_ptr(), &mut ct);
|
||||
if res < 0 {
|
||||
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "too many groups")))
|
||||
return Err(Box::new(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"too many groups",
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(buf[0..ct as usize].iter().map(|x| *x as u32).collect())
|
||||
|
Loading…
Reference in New Issue
Block a user