make windows work again

This commit is contained in:
2025-01-02 13:15:49 -05:00
parent cd4c613758
commit bf62054c2b
13 changed files with 2493 additions and 88 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "creddy_cli"
version = "0.6.0"
version = "0.6.4"
edition = "2021"
[dependencies]
@ -10,3 +10,6 @@ dirs = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
[target.'cfg(windows)'.dependencies]
windows = { workspace = true }

View File

@ -2,8 +2,6 @@ use std::path::PathBuf;
use std::process::Command as ChildCommand;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
#[cfg(windows)]
use std::time::Duration;
use anyhow::{bail, Context};
use clap::{
@ -184,17 +182,10 @@ pub fn exec(args: ExecArgs, global: GlobalArgs) -> anyhow::Result<()> {
#[cfg(windows)]
{
let mut child = match cmd.spawn() {
Ok(c) => c,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
let name: OsString = cmd_name.into();
return Err(ExecError::NotFound(name).into());
}
Err(e) => return Err(ExecError::ExecutionFailed(e).into()),
};
let mut child = cmd.spawn()
.with_context(|| format!("Failed to execute command: {}", args.command.join(" ")))?;
let status = child.wait()
.map_err(|e| ExecError::ExecutionFailed(e))?;
.with_context(|| format!("Failed to execute command: {}", args.command.join(" ")))?;
std::process::exit(status.code().unwrap_or(1));
};
}

View File

@ -47,12 +47,31 @@ mod platform {
#[cfg(windows)]
mod platform {
pub fn server_addr(sock_name: &str) -> String {
use std::path::PathBuf;
use std::time::Duration;
use tokio::net::windows::named_pipe::{NamedPipeClient, ClientOptions};
use windows::Win32::Foundation::ERROR_PIPE_BUSY;
pub async fn connect(addr: Option<PathBuf>) -> std::io::Result<NamedPipeClient> {
let opts = ClientOptions::new();
let pipe_name = addr.unwrap_or_else(|| server_addr("creddy-server"));
loop {
match opts.open(&pipe_name) {
Ok(client) => return Ok(client),
Err(e) if e.raw_os_error() == Some(ERROR_PIPE_BUSY.0 as i32) => {
tokio::time::sleep(Duration::from_millis(50)).await;
},
Err(e) => return Err(e),
}
}
}
pub fn server_addr(sock_name: &str) -> PathBuf {
if cfg!(debug_assertions) {
format!(r"\\.\pipe\{sock_name}.dev")
format!(r"\\.\pipe\{sock_name}.dev").into()
}
else {
format!(r"\\.\pipe\{sock_name}")
format!(r"\\.\pipe\{sock_name}").into()
}
}
}