Skip to content

Commit b6dc9c1

Browse files
committed
skill: implement no-action
1 parent 8e136eb commit b6dc9c1

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

src/uu/skill/src/skill.rs

Lines changed: 9 additions & 24 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 = !settings.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,20 +55,22 @@ 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| {
6771
{
68-
Some(match signal::kill(Pid::from_raw(*pid as i32), *signal) {
72+
Some(match signal::kill(Pid::from_raw(*pid as i32), sig) {
6973
Ok(_) => ActionResult::Success,
70-
7174
Err(_) => ActionResult::PermissionDenied,
7275
})
7376
}
@@ -84,23 +87,5 @@ pub fn uu_app() -> Command {
8487
.infer_long_args(true)
8588
.arg_required_else_help(true)
8689
.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-
])
90+
.args(uu_snice::clap_args())
10691
}

src/uu/snice/src/process_matcher.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Settings {
1414
pub display: Option<SignalDisplay>,
1515
pub expressions: Option<Vec<SelectedTarget>>,
1616
pub verbose: bool,
17+
pub no_action: bool,
1718
}
1819

1920
impl Settings {
@@ -30,6 +31,7 @@ impl Settings {
3031
display,
3132
expressions: Self::targets(matches),
3233
verbose: matches.get_flag("verbose"),
34+
no_action: matches.get_flag("no-action"),
3335
})
3436
}
3537

src/uu/snice/src/snice.rs

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

6-
use std::{collections::HashSet, path::PathBuf, str::FromStr};
7-
86
use crate::priority::Priority;
97
pub use action::ActionResult;
108
use action::{perform_action, process_snapshot, users, SelectedTarget};
119
use clap::{crate_version, Arg, Command};
1210
use prettytable::{format::consts::FORMAT_CLEAN, row, Table};
11+
pub use process_matcher::clap_args;
1312
use process_matcher::*;
13+
use std::{collections::HashSet, path::PathBuf, str::FromStr};
1414
use sysinfo::Pid;
1515
use uu_pgrep::process::ProcessInformation;
1616
#[cfg(target_family = "unix")]
@@ -92,7 +92,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
9292
}
9393

9494
// Case1: Perform priority
95-
let take_action = !matches.get_flag("no-action");
95+
let take_action = !settings.no_action;
9696
if let Some(targets) = settings.expressions {
9797
let priority_str = matches.get_one::<String>("priority").cloned();
9898

0 commit comments

Comments
 (0)