mirror of
https://github.com/Noratrieb/captain.git
synced 2026-01-14 14:35:02 +01:00
Initial commit
This commit is contained in:
commit
f0ffa77500
24 changed files with 802 additions and 0 deletions
1
coreutils/.gitignore
vendored
Normal file
1
coreutils/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
8
coreutils/Cargo.toml
Normal file
8
coreutils/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "coreutils"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.17", features = ["derive"] }
|
||||
eyre.workspace = true
|
||||
3
coreutils/README.md
Normal file
3
coreutils/README.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# coreutils
|
||||
|
||||
Core utilities for the system. Not to be confused with GNU coreutils, these are quite different.
|
||||
25
coreutils/src/bin/ls.rs
Normal file
25
coreutils/src/bin/ls.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use clap::Parser;
|
||||
use eyre::{Context, Result};
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Cmd {
|
||||
directory: PathBuf,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = Cmd::parse();
|
||||
|
||||
let entries = std::fs::read_dir(&args.directory)
|
||||
.wrap_err_with(|| format!("opening {}", args.directory.display()))?;
|
||||
|
||||
for entry in entries {
|
||||
let entry =
|
||||
entry.wrap_err_with(|| format!("reading entry from {}", args.directory.display()))?;
|
||||
|
||||
println!("{}", entry.file_name().to_string_lossy());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
56
coreutils/src/bin/net.rs
Normal file
56
coreutils/src/bin/net.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
use std::net::{TcpStream, ToSocketAddrs};
|
||||
|
||||
use clap::Parser;
|
||||
use eyre::{Context, Result};
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Cmd {
|
||||
#[command(subcommand)]
|
||||
cmd: Subcommand,
|
||||
}
|
||||
|
||||
#[derive(clap::Subcommand)]
|
||||
enum Subcommand {
|
||||
/// Performs DNS resolution of a domain name
|
||||
Resolve { addr: String },
|
||||
Tcp {
|
||||
#[command(subcommand)]
|
||||
cmd: Tcp,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(clap::Subcommand)]
|
||||
enum Tcp {
|
||||
/// Connect via TCP and use stdin/stdout to communicate
|
||||
Connect { addr: String },
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = Cmd::parse();
|
||||
|
||||
match args.cmd {
|
||||
Subcommand::Resolve { addr } => {
|
||||
let addrs = (addr.as_str(), 0)
|
||||
.to_socket_addrs()
|
||||
.wrap_err_with(|| format!("resolving {addr}"))?;
|
||||
|
||||
for addr in addrs {
|
||||
println!("{}", addr.ip());
|
||||
}
|
||||
}
|
||||
Subcommand::Tcp {
|
||||
cmd: Tcp::Connect { addr },
|
||||
} => {
|
||||
let mut stream = TcpStream::connect(&addr).wrap_err_with(|| {
|
||||
format!("connecting to {addr}. note: use IP:PORT as the address format")
|
||||
})?;
|
||||
let mut reader = stream.try_clone().wrap_err("cloning stream")?;
|
||||
|
||||
std::thread::spawn(move || std::io::copy(&mut reader, &mut std::io::stdout().lock()));
|
||||
|
||||
std::io::copy(&mut std::io::stdin().lock(), &mut stream)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
0
coreutils/src/lib.rs
Normal file
0
coreutils/src/lib.rs
Normal file
Loading…
Add table
Add a link
Reference in a new issue