mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
Merge pull request #2791 from jfinkels/csplit-uresult
csplit: return UResult from uumain() function
This commit is contained in:
commit
25c10ad540
2 changed files with 27 additions and 12 deletions
|
@ -2,15 +2,19 @@
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
use clap::{crate_version, App, Arg, ArgMatches};
|
|
||||||
use regex::Regex;
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::io::{self, BufReader};
|
use std::io::{self, BufReader};
|
||||||
use std::{
|
use std::{
|
||||||
fs::{remove_file, File},
|
fs::{remove_file, File},
|
||||||
io::{BufRead, BufWriter, Write},
|
io::{BufRead, BufWriter, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use clap::{crate_version, App, Arg, ArgMatches};
|
||||||
|
use regex::Regex;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
|
use uucore::error::{FromIo, UResult};
|
||||||
|
use uucore::InvalidEncodingHandling;
|
||||||
|
|
||||||
mod csplit_error;
|
mod csplit_error;
|
||||||
mod patterns;
|
mod patterns;
|
||||||
|
@ -18,7 +22,6 @@ mod split_name;
|
||||||
|
|
||||||
use crate::csplit_error::CsplitError;
|
use crate::csplit_error::CsplitError;
|
||||||
use crate::split_name::SplitName;
|
use crate::split_name::SplitName;
|
||||||
use uucore::InvalidEncodingHandling;
|
|
||||||
|
|
||||||
static SUMMARY: &str = "split a file into sections determined by context lines";
|
static SUMMARY: &str = "split a file into sections determined by context lines";
|
||||||
static LONG_HELP: &str = "Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ..., and output byte counts of each piece to standard output.";
|
static LONG_HELP: &str = "Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ..., and output byte counts of each piece to standard output.";
|
||||||
|
@ -712,7 +715,8 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
#[uucore_procs::gen_uumain]
|
||||||
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let usage = usage();
|
let usage = usage();
|
||||||
let args = args
|
let args = args
|
||||||
.collect_str(InvalidEncodingHandling::Ignore)
|
.collect_str(InvalidEncodingHandling::Ignore)
|
||||||
|
@ -729,20 +733,22 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(str::to_string)
|
.map(str::to_string)
|
||||||
.collect();
|
.collect();
|
||||||
let patterns = crash_if_err!(1, patterns::get_patterns(&patterns[..]));
|
let patterns = patterns::get_patterns(&patterns[..])?;
|
||||||
let options = CsplitOptions::new(&matches);
|
let options = CsplitOptions::new(&matches);
|
||||||
if file_name == "-" {
|
if file_name == "-" {
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
crash_if_err!(1, csplit(&options, patterns, stdin.lock()));
|
Ok(csplit(&options, patterns, stdin.lock())?)
|
||||||
} else {
|
} else {
|
||||||
let file = crash_if_err!(1, File::open(file_name));
|
let file = File::open(file_name)
|
||||||
let file_metadata = crash_if_err!(1, file.metadata());
|
.map_err_context(|| format!("cannot access {}", file_name.quote()))?;
|
||||||
|
let file_metadata = file
|
||||||
|
.metadata()
|
||||||
|
.map_err_context(|| format!("cannot access {}", file_name.quote()))?;
|
||||||
if !file_metadata.is_file() {
|
if !file_metadata.is_file() {
|
||||||
crash!(1, "{} is not a regular file", file_name.quote());
|
return Err(CsplitError::NotRegularFile(file_name.to_string()).into());
|
||||||
}
|
}
|
||||||
crash_if_err!(1, csplit(&options, patterns, BufReader::new(file)));
|
Ok(csplit(&options, patterns, BufReader::new(file))?)
|
||||||
};
|
}
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> App<'static, 'static> {
|
pub fn uu_app() -> App<'static, 'static> {
|
||||||
|
|
|
@ -2,6 +2,7 @@ use std::io;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
|
use uucore::error::UError;
|
||||||
|
|
||||||
/// Errors thrown by the csplit command
|
/// Errors thrown by the csplit command
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
|
@ -28,6 +29,8 @@ pub enum CsplitError {
|
||||||
SuffixFormatIncorrect,
|
SuffixFormatIncorrect,
|
||||||
#[error("too many % conversion specifications in suffix")]
|
#[error("too many % conversion specifications in suffix")]
|
||||||
SuffixFormatTooManyPercents,
|
SuffixFormatTooManyPercents,
|
||||||
|
#[error("{} is not a regular file", ._0.quote())]
|
||||||
|
NotRegularFile(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<io::Error> for CsplitError {
|
impl From<io::Error> for CsplitError {
|
||||||
|
@ -35,3 +38,9 @@ impl From<io::Error> for CsplitError {
|
||||||
CsplitError::IoError(error)
|
CsplitError::IoError(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl UError for CsplitError {
|
||||||
|
fn code(&self) -> i32 {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue