use crate::model::{
ActionKind, ExecutionPlan, InstalledPackage, PackageCandidate, PackageTarget,
ProviderCapabilities, ProviderId, ProviderStatus,
};
use crate::providers::Provider;
use async_trait::async_trait;
pub struct GithubProvider;
#[async_trait]
impl Provider for GithubProvider {
fn id(&self) -> ProviderId {
ProviderId::Github
}
fn display_name(&self) -> &'static str {
"GitHub Releases (owner/repo)"
}
fn bin_candidates(&self) -> &'static [&'static str] {
if cfg!(windows) {
&["powershell"]
} else {
&["curl"]
}
}
fn capabilities(&self) -> ProviderCapabilities {
ProviderCapabilities {
supports_search: false,
supports_list: false,
supports_update_all: false,
supports_json: false,
may_require_elevation: false,
}
}
async fn detect(&self) -> ProviderStatus {
self.detect_fast()
}
async fn search(&self, _query: &str, _timeout_secs: u64) -> Result<Vec<PackageCandidate>, String> {
Ok(Vec::new())
}
async fn list(&self, _timeout_secs: u64) -> Result<Vec<InstalledPackage>, String> {
Ok(Vec::new())
}
fn plan(&self, action: ActionKind, target: &PackageTarget) -> Result<ExecutionPlan, String> {
if !target.name.contains('/') {
return Err(format!(
"github target '{}' must be in `owner/repo` format (e.g. `github:sharkdp/fd`)",
target.name
));
}
match action {
ActionKind::Add | ActionKind::Update => {
let repo = &target.name;
let tag_path = match &target.version {
Some(v) => format!("tags/{v}"),
None => "latest".to_string(),
};
if cfg!(windows) {
let script = format!(
"$rel = Invoke-RestMethod 'https://api.github.com/repos/{repo}/releases/{tag_path}'; \
$asset = $rel.assets | Where-Object {{ $_.name -match 'windows|win64|x86_64.*\\.zip$' }} | Select-Object -First 1; \
if (-not $asset) {{ throw 'No matching Windows release asset found' }}; \
$dest = Join-Path $env:USERPROFILE '.local\\bin'; New-Item -ItemType Directory -Force -Path $dest | Out-Null; \
$tmp = Join-Path $env:TEMP $asset.name; Invoke-WebRequest $asset.browser_download_url -OutFile $tmp; \
$sumAsset = $rel.assets | Where-Object {{ $_.name -match 'SHA256SUMS|sha256' }} | Select-Object -First 1; \
if ($sumAsset) {{ \
$sumFile = Join-Path $env:TEMP $sumAsset.name; Invoke-WebRequest $sumAsset.browser_download_url -OutFile $sumFile; \
$line = Get-Content $sumFile | Where-Object {{ $_ -match [regex]::Escape($asset.name) }} | Select-Object -First 1; \
if ($line) {{ $exp = ($line -split '\\s+')[0].ToLowerInvariant(); $act = (Get-FileHash $tmp -Algorithm SHA256).Hash.ToLowerInvariant(); if ($act -ne $exp) {{ throw \"SHA256 mismatch: expected $exp, got $act\" }}; Write-Output \"Verified SHA256: $act\" }} \
}}; \
if ($tmp -like '*.zip') {{ Expand-Archive $tmp -DestinationPath $dest -Force }} else {{ Copy-Item $tmp $dest -Force }}; \
Write-Output \"Installed $($asset.name) to $dest\""
);
Ok(ExecutionPlan {
action,
target: repo.clone(),
provider: ProviderId::Github,
command: "powershell.exe".to_string(),
args: vec!["-NoProfile".into(), "-Command".into(), script],
requires_elevation: false,
reversible: false,
notes: vec![format!(
"Downloads release asset from https://github.com/{repo}/releases/{tag_path} into ~/.local/bin, verifies SHA256 if published (unmanaged, non-reversible)"
)],
})
} else {
let script = format!(
"url=$(curl -fsSL 'https://api.github.com/repos/{repo}/releases/{tag_path}' | grep -oE 'https://[^\"]+linux[^\"]+tar\\.gz' | head -n1); \
test -n \"$url\" && mkdir -p \"$HOME/.local/bin\" && curl -fsSL \"$url\" | tar -xz -C \"$HOME/.local/bin\""
);
Ok(ExecutionPlan {
action,
target: repo.clone(),
provider: ProviderId::Github,
command: "sh".to_string(),
args: vec!["-c".into(), script],
requires_elevation: false,
reversible: false,
notes: vec![format!(
"Downloads release tarball from https://github.com/{repo}/releases/{tag_path} into ~/.local/bin (unmanaged, non-reversible)"
)],
})
}
}
ActionKind::Remove => Err(
"github release installs are unmanaged binaries in ~/.local/bin; remove the binary manually".to_string(),
),
}
}
}