#![allow(
clippy::disallowed_types,
reason = "Pre-runtime sync `Command` invocations for version probing"
)]
use std::process::Command;
#[derive(Debug, Clone)]
#[allow(dead_code, reason = "Fields reserved for future use")]
pub struct CliRequirement {
pub binary: &'static str,
pub min_version: &'static str,
pub install_url: &'static str,
pub install_hint: &'static str,
pub install_cmd: &'static str,
pub login_cmd: &'static str,
pub login_with_token: &'static str,
pub doc_link: &'static str,
}
#[must_use]
pub fn requirement_for(platform: &str) -> Option<CliRequirement> {
match platform {
"github" => Some(CliRequirement {
binary: "gh",
min_version: "2.0.0",
install_url: "https://github.com/cli/cli#installation",
install_hint: "brew install gh # macOS/Linux\n\
choco install gh # Windows\n\
sudo apt install gh # Debian/Ubuntu",
install_cmd: "brew install gh",
login_cmd: "gh auth login",
login_with_token: "echo TOKEN | gh auth login --with-token",
doc_link: "https://cli.github.com/manual/",
}),
"gitlab" => Some(CliRequirement {
binary: "glab",
min_version: "1.30.0",
install_url: "https://gitlab.com/gitlab-org/cli#installation",
install_hint: "brew install glab # macOS/Linux\n\
sudo apt install glab # Debian/Ubuntu",
install_cmd: "brew install glab",
login_cmd: "glab auth login",
login_with_token: "glab auth login --token TOKEN",
doc_link: "https://gitlab.com/gitlab-org/cli/-/blob/main/docs/",
}),
"gitcode" => Some(CliRequirement {
binary: "gitcode",
min_version: "0.6.0",
install_url: "https://gitcode.com/gitcode-cli/cli",
install_hint: "# 方式 1 — Wheel 包(推荐,内置全平台二进制):\n\
pip install https://gitcode.com/gitcode-cli/cli/releases/download/v0.6.1/gitcode_cli-0.6.1-py3-none-any.whl\n\n\
# 方式 2 — PyPI:\n\
pip install gitcode-cli\n\n\
# 方式 3 — Linux DEB:\n\
sudo dpkg -i gitcode_0.6.1_amd64.deb\n\n\
# 方式 4 — 源码构建(Go 1.22+):\n\
git clone https://gitcode.com/gitcode-cli/cli.git && cd cli\n\
make build && mkdir -p ~/.local/bin && mv bin/gitcode ~/.local/bin/",
install_cmd: "pip install gitcode-cli",
login_cmd: "gitcode auth login",
login_with_token: "echo TOKEN | gitcode auth login --with-token",
doc_link: "https://gitcode.com/gitcode-cli/cli/blob/main/README.md",
}),
_ => None,
}
}
fn format_platform(platform: &str) -> String {
match platform {
"github" => "[GitHub]".to_string(),
"gitlab" => "[GitLab]".to_string(),
"gitcode" => "[GitCode]".to_string(),
other => format!("[{other}]"),
}
}
#[derive(Debug)]
pub enum PrerequisiteError {
NotFound {
binary: String,
platform: String,
install_hint: String,
install_cmd: String,
doc_link: String,
},
VersionTooLow {
binary: String,
platform: String,
found: String,
required: String,
install_cmd: String,
doc_link: String,
},
VersionParseFailed {
binary: String,
platform: String,
install_cmd: String,
doc_link: String,
},
NotAuthenticated {
binary: String,
platform: String,
reason: String,
hint: String,
},
UnsupportedPlatform {
platform: String,
},
}
impl std::fmt::Display for PrerequisiteError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotFound {
binary,
platform,
install_cmd,
doc_link,
install_hint,
..
} => write!(
f,
"{} 未检测到 {binary}。\n\n📦 安装:{install_cmd}\n📖 \
文档:{doc_link}\n\n其他安装方式:\n{install_hint}",
format_platform(platform)
),
Self::VersionTooLow {
binary,
platform,
found,
required,
install_cmd,
doc_link,
} => write!(
f,
"{} {binary} 版本过低:当前 v{found},需要 v{required}+。\n\n📦 \
升级:{install_cmd}\n📖 文档:{doc_link}",
format_platform(platform)
),
Self::VersionParseFailed {
binary,
platform,
install_cmd,
doc_link,
} => write!(
f,
"{} {binary} 版本信息解析失败。\n\n📦 重新安装:{install_cmd}\n📖 文档:{doc_link}",
format_platform(platform)
),
Self::NotAuthenticated {
binary,
platform,
reason,
hint,
} => write!(
f,
"{} {binary} 未认证。\n\n🔍 原因:{reason}\n🔧 修复:运行 `{hint}` 完成登录",
format_platform(platform)
),
Self::UnsupportedPlatform { platform } => {
write!(
f,
"不支持的平台:{platform}。支持的平台:github、gitlab、gitcode"
)
}
}
}
}
impl std::error::Error for PrerequisiteError {}
#[allow(
clippy::result_large_err,
reason = "PrerequisiteError carries structured install hints; boxing would lose ergonomic \
matching"
)]
pub fn check(platform: &str) -> Result<(), PrerequisiteError> {
let req = requirement_for(platform).ok_or_else(|| PrerequisiteError::UnsupportedPlatform {
platform: platform.into(),
})?;
let (binary, path, version) = if platform == "gitcode" {
find_gitcode_cli(platform)?
} else {
let path = which::which(req.binary).map_err(|_| PrerequisiteError::NotFound {
binary: req.binary.into(),
platform: platform.into(),
install_hint: req.install_hint.into(),
install_cmd: req.install_cmd.into(),
doc_link: req.doc_link.into(),
})?;
let version = get_version(req.binary, platform)?;
(req.binary, path, version)
};
tracing::debug!(binary, path = %path.display(), "Found native CLI");
if !version_meets_minimum(&version, req.min_version) {
return Err(PrerequisiteError::VersionTooLow {
binary: binary.into(),
platform: platform.into(),
found: version,
required: req.min_version.into(),
install_cmd: req.install_cmd.into(),
doc_link: req.doc_link.into(),
});
}
tracing::debug!(
binary,
found = version,
minimum = req.min_version,
"Version OK"
);
let auth_checker = create_auth_checker(platform);
if !auth_checker.is_authenticated() {
let result = auth_checker.check_status();
return Err(PrerequisiteError::NotAuthenticated {
binary: binary.into(),
platform: platform.into(),
reason: result.reason.unwrap_or_else(|| "未知原因".into()),
hint: result.hint.unwrap_or_else(|| req.login_cmd.into()),
});
}
tracing::debug!(binary, "Authenticated");
Ok(())
}
fn create_auth_checker(platform: &str) -> Box<dyn gitflow_core::AuthChecker> {
match platform {
"github" => Box::new(gitflow_github::GitHubAuthProvider::new()),
"gitlab" => Box::new(gitflow_gitlab::GitLabAuthProvider::new()),
"gitcode" => Box::new(gitflow_gitcode::GitCodeAuthProvider::new()),
_ => unreachable!("Platform already validated by requirement_for"),
}
}
#[allow(
clippy::disallowed_methods,
reason = "binary discovery runs at startup before async runtime is ready"
)]
#[allow(
clippy::result_large_err,
reason = "Same PrerequisiteError size as check()"
)]
fn find_gitcode_cli(
platform: &str,
) -> Result<(&'static str, std::path::PathBuf, String), PrerequisiteError> {
let install_cmd = requirement_for(platform).map_or("", |r| r.install_cmd);
for &binary in &["gitcode"] {
if let Ok(path) = which::which(binary)
&& let Ok(v) = get_version(binary, platform)
{
return Ok((binary, path, v));
}
if let Ok(home) = std::env::var("HOME") {
let lib = std::path::PathBuf::from(&home).join("Library/Python");
if let Ok(entries) = std::fs::read_dir(&lib) {
for entry in entries.flatten() {
let p = entry.path().join("bin").join(binary);
if p.exists()
&& let Ok(v) = get_version(&p.to_string_lossy(), platform)
{
return Ok((binary, p, v));
}
}
}
}
}
Err(PrerequisiteError::NotFound {
binary: "gitcode".into(),
platform: platform.into(),
install_hint: requirement_for(platform)
.map_or("", |r| r.install_hint)
.into(),
install_cmd: install_cmd.into(),
doc_link: requirement_for(platform).map_or("", |r| r.doc_link).into(),
})
}
#[allow(
clippy::result_large_err,
reason = "Same PrerequisiteError size as check()"
)]
fn get_version(binary: &str, platform: &str) -> Result<String, PrerequisiteError> {
let install_cmd = requirement_for(platform).map_or("", |r| r.install_cmd);
for version_arg in ["--version", "version"] {
let output = match Command::new(binary).arg(version_arg).output() {
Ok(o) if o.status.success() => o,
_ => continue,
};
let stdout = String::from_utf8_lossy(&output.stdout);
if let Some(v) = extract_semver(&stdout) {
return Ok(v);
}
}
Err(PrerequisiteError::VersionParseFailed {
binary: binary.into(),
platform: platform.into(),
install_cmd: install_cmd.into(),
doc_link: requirement_for(platform).map_or("", |r| r.doc_link).into(),
})
}
#[must_use]
pub fn extract_semver(s: &str) -> Option<String> {
let re = regex::Regex::new(r"\d+\.\d+\.\d+").ok()?;
re.find(s).map(|m| m.as_str().to_owned())
}
#[must_use]
pub fn version_meets_minimum(found: &str, minimum: &str) -> bool {
let parse = |v: &str| -> Vec<u32> { v.split('.').filter_map(|s| s.parse().ok()).collect() };
parse(found).cmp(&parse(minimum)) != std::cmp::Ordering::Less
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, reason = "test code")]
mod tests {
use super::*;
#[test]
fn test_should_return_requirement_for_github() {
let req = requirement_for("github").expect("github requirement");
assert_eq!(req.binary, "gh");
assert_eq!(req.min_version, "2.0.0");
assert_eq!(req.install_cmd, "brew install gh");
assert_eq!(req.login_cmd, "gh auth login");
assert_eq!(
req.login_with_token,
"echo TOKEN | gh auth login --with-token"
);
}
#[test]
fn test_should_return_requirement_for_gitcode() {
let req = requirement_for("gitcode").expect("gitcode requirement");
assert_eq!(req.binary, "gitcode");
assert_eq!(req.min_version, "0.6.0");
assert_eq!(req.install_cmd, "pip install gitcode-cli");
assert_eq!(req.login_cmd, "gitcode auth login");
assert_eq!(
req.login_with_token,
"echo TOKEN | gitcode auth login --with-token"
);
}
#[test]
fn test_should_extract_semver_from_gh_version_output() {
assert_eq!(
extract_semver("gh version 2.50.0 (2024-01-01)").as_deref(),
Some("2.50.0")
);
}
#[test]
fn test_should_extract_semver_from_glab_version_output() {
assert_eq!(
extract_semver("glab version 1.35.0 (2024-01-01)").as_deref(),
Some("1.35.0")
);
}
#[test]
fn test_should_version_meets_minimum() {
assert!(version_meets_minimum("2.50.0", "2.0.0"));
assert!(version_meets_minimum("2.0.0", "2.0.0"));
assert!(!version_meets_minimum("1.9.0", "2.0.0"));
}
#[test]
fn test_should_display_github_in_not_authenticated_error() {
let err = PrerequisiteError::NotAuthenticated {
binary: "gh".into(),
platform: "github".into(),
reason: "token expired".into(),
hint: "gh auth login".into(),
};
let msg = err.to_string();
assert!(msg.contains("[GitHub]"), "Expected [GitHub] in: {msg}");
assert!(
!msg.contains("[[PLATFORM]]"),
"Found literal [[PLATFORM]] in: {msg}"
);
}
#[test]
fn test_should_display_gitlab_in_not_found_error() {
let err = PrerequisiteError::NotFound {
binary: "glab".into(),
platform: "gitlab".into(),
install_hint: "brew install glab".into(),
install_cmd: "brew install glab".into(),
doc_link: "https://docs.gitlab.com".into(),
};
let msg = err.to_string();
assert!(msg.contains("[GitLab]"), "Expected [GitLab] in: {msg}");
assert!(
!msg.contains("[[PLATFORM]]"),
"Found literal [[PLATFORM]] in: {msg}"
);
}
#[test]
fn test_should_display_gitcode_in_version_too_low_error() {
let err = PrerequisiteError::VersionTooLow {
binary: "gitcode".into(),
platform: "gitcode".into(),
found: "0.5.0".into(),
required: "0.6.0".into(),
install_cmd: "pip install gitcode-cli".into(),
doc_link: "https://gitcode.com/cli".into(),
};
let msg = err.to_string();
assert!(msg.contains("[GitCode]"), "Expected [GitCode] in: {msg}");
assert!(
!msg.contains("[[PLATFORM]]"),
"Found literal [[PLATFORM]] in: {msg}"
);
}
}