aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
46
akode-rs/stdio-to-uds/src/lib.rs
Normal file
46
akode-rs/stdio-to-uds/src/lib.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
#![deny(clippy::print_stdout)]
|
||||
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Context;
|
||||
use akode_uds::UnixStream;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
/// Connects to the Unix Domain Socket at `socket_path` and relays data between
|
||||
/// standard input/output and the socket.
|
||||
pub async fn run(socket_path: &Path) -> anyhow::Result<()> {
|
||||
let stream = UnixStream::connect(socket_path)
|
||||
.await
|
||||
.with_context(|| format!("failed to connect to socket at {}", socket_path.display()))?;
|
||||
let (mut socket_reader, mut socket_writer) = tokio::io::split(stream);
|
||||
|
||||
let copy_socket_to_stdout = async {
|
||||
let mut stdout = tokio::io::stdout();
|
||||
tokio::io::copy(&mut socket_reader, &mut stdout).await?;
|
||||
stdout.flush().await?;
|
||||
Ok(())
|
||||
};
|
||||
let copy_stdin_to_socket = async {
|
||||
let mut stdin = tokio::io::stdin();
|
||||
tokio::io::copy(&mut stdin, &mut socket_writer)
|
||||
.await
|
||||
.context("failed to copy data from stdin to socket")?;
|
||||
|
||||
// The peer can close immediately after sending its response; in that
|
||||
// race, half-closing our write side can report NotConnected on some
|
||||
// platforms.
|
||||
if let Err(err) = socket_writer.shutdown().await
|
||||
&& err.kind() != io::ErrorKind::NotConnected
|
||||
{
|
||||
return Err(err).context("failed to shutdown socket writer");
|
||||
}
|
||||
|
||||
anyhow::Ok(())
|
||||
};
|
||||
|
||||
tokio::try_join!(copy_stdin_to_socket, copy_socket_to_stdout)
|
||||
.context("failed to relay data between stdio and socket")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
20
akode-rs/stdio-to-uds/src/main.rs
Normal file
20
akode-rs/stdio-to-uds/src/main.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let mut args = env::args_os().skip(1);
|
||||
let Some(socket_path) = args.next() else {
|
||||
eprintln!("Usage: akode-stdio-to-uds <socket-path>");
|
||||
process::exit(1);
|
||||
};
|
||||
|
||||
if args.next().is_some() {
|
||||
eprintln!("Expected exactly one argument: <socket-path>");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
let socket_path = PathBuf::from(socket_path);
|
||||
akode_stdio_to_uds::run(&socket_path).await
|
||||
}
|
||||
Reference in New Issue
Block a user