watch: add initial version

This commit is contained in:
Sylvestre Ledru
2024-01-31 23:22:46 +01:00
parent 4b4fd96b75
commit c8debd01cd
7 changed files with 114 additions and 0 deletions
Generated
+9
View File
@@ -442,6 +442,7 @@ dependencies = [
"uu_free",
"uu_pwdx",
"uu_w",
"uu_watch",
"uucore",
"xattr",
]
@@ -794,6 +795,14 @@ dependencies = [
"uucore",
]
[[package]]
name = "uu_watch"
version = "0.0.1"
dependencies = [
"clap",
"uucore",
]
[[package]]
name = "uucore"
version = "0.0.24"
+2
View File
@@ -28,6 +28,7 @@ feat_common_core = [
"pwdx",
"free",
"w",
"watch",
]
[workspace.dependencies]
@@ -59,6 +60,7 @@ textwrap = { workspace = true }
pwdx = { optional = true, version = "0.0.1", package = "uu_pwdx", path = "src/uu/pwdx" }
free = { optional = true, version = "0.0.1", package = "uu_free", path = "src/uu/free" }
w = { optional = true, version = "0.0.1", package = "uu_w", path = "src/uu/w" }
watch = { optional = true, version = "0.0.1", package = "uu_watch", path = "src/uu/watch" }
[dev-dependencies]
pretty_assertions = "1"
+17
View File
@@ -0,0 +1,17 @@
[package]
name = "uu_watch"
version = "0.0.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
uucore = { workspace = true }
clap = { workspace = true }
[lib]
path = "src/watch.rs"
[[bin]]
name = "watch"
path = "src/main.rs"
+61
View File
@@ -0,0 +1,61 @@
// This file is part of the uutils procps package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use clap::{Arg, ArgAction, Command};
use clap::{crate_version};
use std::process::{Command as SystemCommand, Stdio};
use std::thread::sleep;
use std::time::Duration;
use uucore::{error::UResult, format_usage, help_about, help_usage};
const ABOUT: &str = help_about!("watch.md");
const USAGE: &str = help_usage!("watch.md");
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let command_to_watch = matches.get_one::<String>("command").expect("required argument");
let interval = 2; // TODO matches.get_one::<u64>("interval").map_or(2, |&v| v);
loop {
let output = SystemCommand::new("sh")
.arg("-c")
.arg(command_to_watch)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;
if !output.status.success() {
eprintln!("watch: command failed: {:?}", output.status);
break;
}
sleep(Duration::from_secs(interval));
}
Ok(())
}
pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.version(crate_version!())
.about(ABOUT)
.override_usage(format_usage(USAGE))
.infer_long_args(true)
.arg(
Arg::new("command")
.required(true)
.help("Command to be executed"),
)
.arg(
Arg::new("interval")
.short('n')
.long("interval")
.default_value("2")
.help("Seconds to wait between updates")
.action(ArgAction::Set),
)
}
+7
View File
@@ -0,0 +1,7 @@
# watch
```
watch [options] command
```
Execute a program periodically, showing output fullscreen
+14
View File
@@ -0,0 +1,14 @@
// This file is part of the uutils procps package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (words) symdir somefakedir
use std::path::PathBuf;
use crate::common::util::{TestScenario, UCommand};
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
+4
View File
@@ -16,3 +16,7 @@ mod test_free;
#[cfg(feature = "w")]
#[path = "by-util/test_w.rs"]
mod test_w;
#[cfg(feature = "watch")]
#[path = "by-util/test_watch.rs"]
mod test_watch;