mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-20 06:56:17 -05:00
feat: improve platform integration and release packaging
This commit is contained in:
17 files changed
+357
-105
No files matched your search
@@ -1,17 +1,10 @@
|
||||
#[cfg(feature = "launcher")]
|
||||
use log::info;
|
||||
#[cfg(all(feature = "launcher", target_os = "macos"))]
|
||||
#[path = "platform/macos.rs"]
|
||||
mod platform;
|
||||
|
||||
#[cfg(any(feature = "launcher", test))]
|
||||
const CHROME_BROWSERS: &[&str] = &["google-chrome-stable", "google-chrome", "chromium"];
|
||||
#[cfg(any(feature = "launcher", test))]
|
||||
const EDGE_BROWSERS: &[&str] = &[
|
||||
"microsoft-edge-stable",
|
||||
"microsoft-edge",
|
||||
"microsoft-edge-beta",
|
||||
"microsoft-edge-dev",
|
||||
];
|
||||
#[cfg(any(feature = "launcher", test))]
|
||||
const FIREFOX_BROWSER: &str = "firefox";
|
||||
#[cfg(all(feature = "launcher", not(target_os = "macos")))]
|
||||
#[path = "platform/generic.rs"]
|
||||
mod platform;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Browser<'a> {
|
||||
@@ -20,6 +13,7 @@ pub enum Browser<'a> {
|
||||
Chrome,
|
||||
Edge,
|
||||
Firefox,
|
||||
Safari,
|
||||
Other(&'a str),
|
||||
}
|
||||
|
||||
@@ -39,18 +33,20 @@ impl<'a> Browser<'a> {
|
||||
"chrome" => Browser::Chrome,
|
||||
"edge" => Browser::Edge,
|
||||
"firefox" => Browser::Firefox,
|
||||
"safari" => Browser::Safari,
|
||||
_ => Browser::Other(value),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "launcher")]
|
||||
fn as_str(&self) -> &str {
|
||||
#[cfg(all(feature = "launcher", not(target_os = "macos")))]
|
||||
pub(crate) fn as_str(&self) -> &str {
|
||||
match self {
|
||||
Browser::Auto => "auto",
|
||||
Browser::Default => "default",
|
||||
Browser::Chrome => "chrome",
|
||||
Browser::Edge => "edge",
|
||||
Browser::Firefox => FIREFOX_BROWSER,
|
||||
Browser::Firefox => "firefox",
|
||||
Browser::Safari => "safari",
|
||||
Browser::Other(browser) => browser,
|
||||
}
|
||||
}
|
||||
@@ -58,62 +54,7 @@ impl<'a> Browser<'a> {
|
||||
|
||||
#[cfg(feature = "launcher")]
|
||||
pub fn open_url(url: &str, browser: Browser<'_>) -> anyhow::Result<()> {
|
||||
match browser {
|
||||
Browser::Auto => {
|
||||
if let Some(browser) = find_auto_browser() {
|
||||
open_with_browser(url, &browser).or_else(|_| open_default_browser(url))
|
||||
} else {
|
||||
open_default_browser(url)
|
||||
}
|
||||
}
|
||||
Browser::Default => open_default_browser(url),
|
||||
Browser::Chrome => {
|
||||
let browser = find_chrome_browser().unwrap_or_else(|| browser.as_str().to_string());
|
||||
open_with_browser(url, &browser)
|
||||
}
|
||||
Browser::Edge => {
|
||||
let browser = find_edge_browser().unwrap_or_else(|| browser.as_str().to_string());
|
||||
open_with_browser(url, &browser)
|
||||
}
|
||||
Browser::Firefox | Browser::Other(_) => open_with_browser(url, browser.as_str()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "launcher")]
|
||||
fn open_default_browser(url: &str) -> anyhow::Result<()> {
|
||||
if webbrowser::open(url).is_ok() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
open::that_detached(url).map_err(Into::into)
|
||||
}
|
||||
|
||||
#[cfg(feature = "launcher")]
|
||||
fn open_with_browser(url: &str, browser: &str) -> anyhow::Result<()> {
|
||||
info!("Launching browser: {}", browser);
|
||||
open::with_detached(url, browser).map_err(Into::into)
|
||||
}
|
||||
|
||||
#[cfg(feature = "launcher")]
|
||||
fn find_auto_browser() -> Option<String> {
|
||||
find_chrome_browser()
|
||||
.or_else(find_edge_browser)
|
||||
.or_else(|| find_program(FIREFOX_BROWSER))
|
||||
}
|
||||
|
||||
#[cfg(feature = "launcher")]
|
||||
fn find_chrome_browser() -> Option<String> {
|
||||
CHROME_BROWSERS.iter().find_map(|browser| find_program(browser))
|
||||
}
|
||||
|
||||
#[cfg(feature = "launcher")]
|
||||
fn find_edge_browser() -> Option<String> {
|
||||
EDGE_BROWSERS.iter().find_map(|browser| find_program(browser))
|
||||
}
|
||||
|
||||
#[cfg(feature = "launcher")]
|
||||
fn find_program(name: &str) -> Option<String> {
|
||||
which::which(name).ok().map(|path| path.to_string_lossy().to_string())
|
||||
platform::open_url(url, browser)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -134,6 +75,7 @@ mod tests {
|
||||
assert_eq!(Browser::from_name("Chrome"), Browser::Chrome);
|
||||
assert_eq!(Browser::from_name("EDGE"), Browser::Edge);
|
||||
assert_eq!(Browser::from_name("Firefox"), Browser::Firefox);
|
||||
assert_eq!(Browser::from_name("Safari"), Browser::Safari);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -144,28 +86,4 @@ mod tests {
|
||||
Browser::Other(" custom-browser ")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_candidates_try_chrome_then_edge_then_firefox() {
|
||||
let candidates = CHROME_BROWSERS
|
||||
.iter()
|
||||
.chain(EDGE_BROWSERS)
|
||||
.chain([FIREFOX_BROWSER].iter())
|
||||
.copied()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(
|
||||
candidates,
|
||||
vec![
|
||||
"google-chrome-stable",
|
||||
"google-chrome",
|
||||
"chromium",
|
||||
"microsoft-edge-stable",
|
||||
"microsoft-edge",
|
||||
"microsoft-edge-beta",
|
||||
"microsoft-edge-dev",
|
||||
"firefox",
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user