kratactl: initial windows bringup

This commit is contained in:
Alex Zenla
2024-03-21 19:54:14 -07:00
parent 5fd10d6edf
commit a3a2148002
7 changed files with 78 additions and 17 deletions

View File

@ -13,13 +13,15 @@ env_logger = { workspace = true }
krata = { path = "../krata" }
log = { workspace = true }
serde = { workspace = true }
termion = { workspace = true }
tokio = { workspace = true }
tokio-stream = { workspace = true }
tonic = { workspace = true }
tower = { workspace = true }
url = { workspace = true }
[target.'cfg(unix)'.dependencies]
termion = { workspace = true }
[lib]
name = "kratactl"

View File

@ -1,7 +1,13 @@
#[cfg(not(unix))]
use anyhow::anyhow;
use anyhow::Result;
use krata::{control::control_service_client::ControlServiceClient, dial::ControlDialAddress};
#[cfg(unix)]
use tokio::net::UnixStream;
use tonic::transport::{Channel, ClientTlsConfig, Endpoint, Uri};
#[cfg(unix)]
use tonic::transport::Uri;
use tonic::transport::{Channel, ClientTlsConfig, Endpoint};
#[cfg(unix)]
use tower::service_fn;
pub struct ControlClientProvider {}
@ -10,13 +16,13 @@ impl ControlClientProvider {
pub async fn dial(addr: ControlDialAddress) -> Result<ControlServiceClient<Channel>> {
let channel = match addr {
ControlDialAddress::UnixSocket { path } => {
// This URL is not actually used but is required to be specified.
Endpoint::try_from(format!("unix://localhost/{}", path))?
.connect_with_connector(service_fn(|uri: Uri| {
let path = uri.path().to_string();
UnixStream::connect(path)
}))
.await?
#[cfg(not(unix))]
return Err(anyhow!(
"unix sockets are not supported on this platform (path {})",
path
));
#[cfg(unix)]
ControlClientProvider::dial_unix_socket(path).await?
}
ControlDialAddress::Tcp { host, port } => {
@ -41,4 +47,15 @@ impl ControlClientProvider {
Ok(ControlServiceClient::new(channel))
}
#[cfg(unix)]
async fn dial_unix_socket(path: String) -> Result<Channel> {
// This URL is not actually used but is required to be specified.
Ok(Endpoint::try_from(format!("unix://localhost/{}", path))?
.connect_with_connector(service_fn(|uri: Uri| {
let path = uri.path().to_string();
UnixStream::connect(path)
}))
.await?)
}
}

View File

@ -1,8 +1,3 @@
use std::{
io::stdout,
os::fd::{AsRawFd, FromRawFd},
};
use anyhow::Result;
use async_stream::stream;
use krata::{
@ -10,9 +5,13 @@ use krata::{
control::{watch_events_reply::Event, ConsoleDataReply, ConsoleDataRequest},
};
use log::debug;
#[cfg(unix)]
use std::os::fd::{AsRawFd, FromRawFd};
#[cfg(unix)]
use termion::raw::IntoRawMode;
#[cfg(unix)]
use tokio::fs::File;
use tokio::{
fs::File,
io::{stdin, AsyncReadExt, AsyncWriteExt},
task::JoinHandle,
};
@ -48,8 +47,14 @@ impl StdioConsoleStream {
}
pub async fn stdout(mut stream: Streaming<ConsoleDataReply>) -> Result<()> {
let terminal = stdout().into_raw_mode()?;
let mut stdout = unsafe { File::from_raw_fd(terminal.as_raw_fd()) };
#[cfg(unix)]
let terminal = std::io::stdout().into_raw_mode()?;
#[cfg(unix)]
let mut stdout =
unsafe { File::from_std(std::fs::File::from_raw_fd(terminal.as_raw_fd())) };
#[cfg(not(unix))]
let mut stdout = tokio::io::stdout();
while let Some(reply) = stream.next().await {
let reply = reply?;
if reply.data.is_empty() {