fix(macos): support bundled CLI through Homebrew and report auth failures

This commit is contained in:
Kevin Yue committed 2026-10-10 16:37:28 +08:00
1 parent 8724ed3b37
commit 29e8d9c822
10 files changed
+165 -22

No files matched your search

+3
View File
@@ -6,3 +6,6 @@ authors.workspace = true
homepage.workspace = true
edition.workspace = true
license = "MIT"
[dev-dependencies]
tempfile.workspace = true
+51 -13
View File
@@ -3,6 +3,9 @@ use std::{
path::{Path, PathBuf},
};
#[cfg(target_os = "macos")]
mod macos;
#[cfg(all(
not(debug_assertions),
any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")
@@ -80,13 +83,18 @@ fn resolve(env_key: &str, binary_name: &str, default_path: &str) -> PathBuf {
fn sibling_binary(binary_name: &str) -> Option<PathBuf> {
let current_exe = env::current_exe().ok()?;
let bin_dir = current_exe.parent()?;
sibling_binary_for_executable(&current_exe, binary_name)
}
fn sibling_binary_for_executable(executable: &Path, binary_name: &str) -> Option<PathBuf> {
let executable = executable.canonicalize().ok()?;
let bin_dir = executable.parent()?;
let binary = bin_dir.join(binary_name);
if is_file(&binary) {
return Some(binary);
}
#[cfg(target_os = "macos")]
if let Some(binary) = bundled_helper_path(&current_exe, binary_name)
if let Some(binary) = macos::helper_path(&executable, binary_name)
&& is_file(&binary)
{
return Some(binary);
@@ -95,14 +103,14 @@ fn sibling_binary(binary_name: &str) -> Option<PathBuf> {
}
#[cfg(target_os = "macos")]
fn bundled_helper_path(executable: &Path, binary_name: &str) -> Option<PathBuf> {
let bin_dir = executable.parent()?;
let contents = bin_dir.parent()?;
let bundle = contents.parent()?;
if bin_dir.file_name()? != "MacOS" || contents.file_name()? != "Contents" || bundle.extension()? != "app" {
return None;
}
Some(contents.join("Helpers").join(binary_name))
pub fn bundled_vpnc_script() -> Option<PathBuf> {
bundled_vpnc_script_for_executable(&env::current_exe().ok()?)
}
#[cfg(target_os = "macos")]
fn bundled_vpnc_script_for_executable(executable: &Path) -> Option<PathBuf> {
let executable = executable.canonicalize().ok()?;
macos::vpnc_script_path(&executable)
}
fn is_file(path: &Path) -> bool {
@@ -117,14 +125,44 @@ mod tests {
#[cfg(target_os = "macos")]
fn desktop_resolves_helpers_in_the_actual_app_bundle_layout() {
assert_eq!(
bundled_helper_path(
macos::helper_path(
Path::new("/Applications/GP Connect.app/Contents/MacOS/gpgui"),
"gpclient"
),
Some(PathBuf::from("/Applications/GP Connect.app/Contents/Helpers/gpclient"))
);
assert!(bundled_helper_path(Path::new("/usr/local/bin/gpgui"), "gpclient").is_none());
assert!(bundled_helper_path(Path::new("/tmp/Contents/MacOS/gpgui"), "gpclient").is_none());
assert!(macos::helper_path(Path::new("/usr/local/bin/gpgui"), "gpclient").is_none());
assert!(macos::helper_path(Path::new("/tmp/Contents/MacOS/gpgui"), "gpclient").is_none());
}
#[test]
#[cfg(unix)]
fn symlinked_executable_resolves_its_bundle_paths() {
let directory = tempfile::tempdir().unwrap();
let helpers = directory.path().join("GP Connect.app/Contents/Helpers");
let bin = directory.path().join("bin");
std::fs::create_dir_all(&helpers).unwrap();
std::fs::create_dir_all(&bin).unwrap();
std::fs::write(helpers.join("gpclient"), "").unwrap();
std::fs::write(helpers.join("gpauth"), "").unwrap();
std::os::unix::fs::symlink(helpers.join("gpclient"), bin.join("gpclient")).unwrap();
assert_eq!(
sibling_binary_for_executable(&bin.join("gpclient"), "gpauth"),
Some(helpers.canonicalize().unwrap().join("gpauth"))
);
#[cfg(target_os = "macos")]
assert_eq!(
bundled_vpnc_script_for_executable(&bin.join("gpclient")),
Some(
helpers
.canonicalize()
.unwrap()
.parent()
.unwrap()
.join("Resources/Scripts/vpnc-script")
)
);
}
#[test]
+45
View File
@@ -0,0 +1,45 @@
use std::path::{Path, PathBuf};
fn contents_dir(executable: &Path) -> Option<&Path> {
let bin_dir = executable.parent()?;
let contents = bin_dir.parent()?;
let bundle = contents.parent()?;
let directory_name = bin_dir.file_name()?;
if (directory_name != "MacOS" && directory_name != "Helpers")
|| contents.file_name()? != "Contents"
|| bundle.extension()? != "app"
{
return None;
}
Some(contents)
}
pub(super) fn helper_path(executable: &Path, binary_name: &str) -> Option<PathBuf> {
Some(contents_dir(executable)?.join("Helpers").join(binary_name))
}
pub(super) fn vpnc_script_path(executable: &Path) -> Option<PathBuf> {
Some(contents_dir(executable)?.join("Resources/Scripts/vpnc-script"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bundle_entry_points_use_their_own_vpnc_script() {
for executable in [
"/Applications/GP Connect.app/Contents/Helpers/gpclient",
"/Applications/GP Connect.app/Contents/MacOS/gpgui",
] {
assert_eq!(
vpnc_script_path(Path::new(executable)),
Some(PathBuf::from(
"/Applications/GP Connect.app/Contents/Resources/Scripts/vpnc-script"
))
);
}
assert!(vpnc_script_path(Path::new("/opt/homebrew/bin/gpclient")).is_none());
assert!(vpnc_script_path(Path::new("/tmp/Contents/Helpers/gpclient")).is_none());
}
}