diff options
author | Lars Wirzenius <liw@sequoia-pgp.org> | 2022-10-15 17:34:57 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@sequoia-pgp.org> | 2022-10-15 17:39:08 +0300 |
commit | 25a92de0113e0697e30bddf703bb5db21f03af6c (patch) | |
tree | ec27fe6a233bb28ee8b069120c0d9f822db6006e | |
parent | ed415b5fa1f0d1c85ab1cef5b8f5ee58951f9453 (diff) | |
download | unixtool-25a92de0113e0697e30bddf703bb5db21f03af6c.tar.gz |
add cat command
Sponsored-by: author
-rw-r--r-- | Cargo.lock | 14 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/bin/unixtool.rs | 7 | ||||
-rw-r--r-- | src/cmd/cat.rs | 28 | ||||
-rw-r--r-- | src/cmd/mod.rs | 3 |
5 files changed, 52 insertions, 2 deletions
@@ -3,6 +3,12 @@ version = 3 [[package]] +name = "anyhow" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602" + +[[package]] name = "atty" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -20,6 +26,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] +name = "check" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89beada3dfaf43501b20ab69f1d1dd2d43b4a600eb16ca05ea0e093a100634b1" + +[[package]] name = "clap" version = "4.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -167,6 +179,8 @@ checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" name = "unixtool" version = "0.1.0" dependencies = [ + "anyhow", + "check", "clap", ] @@ -6,4 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.65" +check = "1.0.0" clap = { version = "4.0.15", features = ["derive"] } diff --git a/src/bin/unixtool.rs b/src/bin/unixtool.rs index 59feac1..80c69b1 100644 --- a/src/bin/unixtool.rs +++ b/src/bin/unixtool.rs @@ -1,11 +1,13 @@ use clap::{Parser, Subcommand}; -use unixtool::cmd::Echo; +use unixtool::cmd::*; -fn main() { +fn main() -> anyhow::Result<()> { let args = Args::parse(); match args.command { + Command::Cat(x) => x.run()?, Command::Echo(x) => x.run(), } + Ok(()) } #[derive(Parser)] @@ -16,5 +18,6 @@ struct Args { #[derive(Subcommand)] enum Command { + Cat(Cat), Echo(Echo), } diff --git a/src/cmd/cat.rs b/src/cmd/cat.rs new file mode 100644 index 0000000..31fdbdf --- /dev/null +++ b/src/cmd/cat.rs @@ -0,0 +1,28 @@ +use clap::Parser; +use std::fs::read; +use std::io::{stdin, stdout, Read, Write}; +use std::path::PathBuf; + +/// Read files named on the command line and write them to stdout. If +/// no files are named, read stdin instead. +#[derive(Parser)] +pub struct Cat { + /// Files to read. If none given, read stdin. + filenames: Vec<PathBuf>, +} + +impl Cat { + pub fn run(&self) -> anyhow::Result<()> { + if self.filenames.is_empty() { + let mut data = vec![]; + stdin().read_to_end(&mut data)?; + stdout().write_all(&data)?; + } else { + for filename in self.filenames.iter() { + let data = read(filename)?; + stdout().write_all(&data)?; + } + } + Ok(()) + } +} diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index d1744e5..5315fc1 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,2 +1,5 @@ mod echo; pub use echo::Echo; + +mod cat; +pub use cat::Cat; |