Skip to content

Commit

Permalink
tail: Apply from review: VerificationResult returns better enums. Inc…
Browse files Browse the repository at this point in the history
…ludes some small improvements.
  • Loading branch information
Joining7943 committed Nov 15, 2022
1 parent ac185d0 commit 4d69b80
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
13 changes: 5 additions & 8 deletions src/uu/tail/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// spell-checker:ignore (ToDO) kqueue Signum

use crate::paths::Input;
use crate::{parse, platform, text, Quotable};
use crate::{parse, platform, Quotable};
use atty::Stream;
use clap::{parser::ValueSource, Arg, ArgAction, ArgMatches, Command};
use same_file::Handle;
Expand Down Expand Up @@ -117,8 +117,8 @@ pub enum FollowMode {
#[derive(Debug)]
pub enum VerificationResult {
Ok,
Fatal(String),
Exit(i32),
CannotFollowStdinByName,
NoOutput,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -290,10 +290,7 @@ impl Settings {
pub fn verify(&self) -> VerificationResult {
// Mimic GNU's tail for `tail -F`
if self.inputs.iter().any(|i| i.is_stdin()) && self.follow == Some(FollowMode::Name) {
return VerificationResult::Fatal(format!(
"cannot follow {} by name",
text::DASH.quote()
));
return VerificationResult::CannotFollowStdinByName;
}

// Mimic GNU's tail for -[nc]0 without -f and exit immediately
Expand All @@ -303,7 +300,7 @@ impl Settings {
FilterMode::Lines(Signum::MinusZero, _) | FilterMode::Bytes(Signum::MinusZero)
)
{
return VerificationResult::Exit(0);
return VerificationResult::NoOutput;
}

VerificationResult::Ok
Expand Down
11 changes: 9 additions & 2 deletions src/uu/tail/src/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
settings.check_warnings();

match settings.verify() {
args::VerificationResult::Fatal(message) => return Err(USimpleError::new(1, message)),
args::VerificationResult::Exit(code) => std::process::exit(code),
args::VerificationResult::CannotFollowStdinByName => {
return Err(USimpleError::new(
1,
format!("cannot follow {} by name", text::DASH.quote()),
))
}
// Exit early if we do not output anything. Note, that this may break a pipe
// when tail is on the receiving side.
args::VerificationResult::NoOutput => return Ok(()),
args::VerificationResult::Ok => {}
}

Expand Down

0 comments on commit 4d69b80

Please sign in to comment.