Skip to content

Commit ae5ac8f

Browse files
committed
skill: implement no-action
1 parent fb962d0 commit ae5ac8f

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

src/uu/skill/src/skill.rs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use clap::{arg, crate_version, value_parser, Arg, Command};
6+
use clap::{crate_version, Arg, Command};
77
#[cfg(unix)]
88
use nix::{sys::signal, sys::signal::Signal, unistd::Pid};
99
use uu_snice::{
@@ -29,6 +29,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2929
}
3030

3131
// Case1: Send signal
32+
let take_action = !matches.get_flag("no-action");
3233
if let Some(targets) = settings.expressions {
3334
let pids = collect_pids(&targets);
3435

@@ -43,7 +44,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4344
};
4445

4546
#[cfg(unix)]
46-
let results = perform_action(&pids, &signal);
47+
let results = perform_action(&pids, &signal, take_action);
4748
#[cfg(not(unix))]
4849
let results: Vec<Option<ActionResult>> = Vec::new();
4950

@@ -54,23 +55,23 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5455
if settings.verbose {
5556
let output = construct_verbose_result(&pids, &results).trim().to_owned();
5657
println!("{output}");
58+
} else if !take_action {
59+
pids.iter().for_each(|pid| println!("{pid}"));
5760
}
5861
}
5962

6063
Ok(())
6164
}
6265

6366
#[cfg(unix)]
64-
fn perform_action(pids: &[u32], signal: &Signal) -> Vec<Option<ActionResult>> {
67+
fn perform_action(pids: &[u32], signal: &Signal, take_action: bool) -> Vec<Option<ActionResult>> {
68+
let sig = if take_action { Some(*signal) } else { None };
6569
pids.iter()
6670
.map(|pid| {
67-
{
68-
Some(match signal::kill(Pid::from_raw(*pid as i32), *signal) {
69-
Ok(_) => ActionResult::Success,
70-
71-
Err(_) => ActionResult::PermissionDenied,
72-
})
73-
}
71+
Some(match signal::kill(Pid::from_raw(*pid as i32), sig) {
72+
Ok(_) => ActionResult::Success,
73+
Err(_) => ActionResult::PermissionDenied,
74+
})
7475
})
7576
.collect()
7677
}
@@ -84,23 +85,5 @@ pub fn uu_app() -> Command {
8485
.infer_long_args(true)
8586
.arg_required_else_help(true)
8687
.arg(Arg::new("signal"))
87-
.args([
88-
// arg!(-f --fast "fast mode (not implemented)"),
89-
// arg!(-i --interactive "interactive"),
90-
arg!(-l --list "list all signal names"),
91-
arg!(-L --table "list all signal names in a nice table"),
92-
// arg!(-n --"no-action" "do not actually kill processes; just print what would happen"),
93-
arg!(-v --verbose "explain what is being done"),
94-
// arg!(-w --warnings "enable warnings (not implemented)"),
95-
// Expressions
96-
arg!(-c --command <command> ... "expression is a command name"),
97-
arg!(-p --pid <pid> ... "expression is a process id number")
98-
.value_parser(value_parser!(u32)),
99-
arg!(-t --tty <tty> ... "expression is a terminal"),
100-
arg!(-u --user <username> ... "expression is a username"),
101-
// arg!(--ns <PID> "match the processes that belong to the same namespace as <pid>"),
102-
// arg!(--nslist <ns> "list which namespaces will be considered for the --ns option.")
103-
// .value_delimiter(',')
104-
// .value_parser(["ipc", "mnt", "net", "pid", "user", "uts"]),
105-
])
88+
.args(uu_snice::clap_args())
10689
}

src/uu/snice/src/snice.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub use action::ActionResult;
1010
use action::{perform_action, process_snapshot, users, SelectedTarget};
1111
use clap::{crate_version, Arg, Command};
1212
use prettytable::{format::consts::FORMAT_CLEAN, row, Table};
13+
pub use process_matcher::clap_args;
1314
use process_matcher::*;
1415
use sysinfo::Pid;
1516
use uu_pgrep::process::ProcessInformation;

0 commit comments

Comments
 (0)