Skip to content

Commit

Permalink
add manual select fallback
Browse files Browse the repository at this point in the history
resolves #6
  • Loading branch information
8LWXpg committed Oct 28, 2024
1 parent 1c314d4 commit c28c68b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Change Log

## [unreleased]

### Added

- Added manual select fallback if assets matching failed.

### Fixed

- Fixed extracting plugin zip file with backslashes in the path.
- Ignore version when importing plugin.

## [0.6.0]

### Added
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ cargo install --git https://github.com/8LWXpg/ptr.git

## Limitations

If you have any suggestions for these limitations, feel free to open an issue.

- This tool only supports plugins hosted on GitHub.
- The plugin release must be a zip file with either `x64` or `arm64` in the file name.
- The zip structure must be like this:
Expand Down
26 changes: 21 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, bail, Result};
use colored::Colorize;
use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, ACCEPT, USER_AGENT};
use serde::Deserialize;
Expand Down Expand Up @@ -88,11 +89,11 @@ pub fn gh_dl(
}
}

let asset = &res
.assets
.iter()
.find(|a| a.is_arch(arch))
.ok_or(anyhow!("No asset found that contains '{}'", arch))?;
let assets = res.assets;
let asset = match assets.iter().find(|a| a.is_arch(arch)) {
Some(asset) => asset,
None => manual_select(&assets)?,
};
let (url, name) = (&asset.browser_download_url, &asset.name);
let res = Client::new().get(url).send()?;

Expand All @@ -106,6 +107,21 @@ pub fn gh_dl(
Ok(tag)
}

fn manual_select(assets: &[Assets]) -> Result<&Assets> {
if assets.len() == 1 {
return Ok(&assets[0]);
}

println!("Fail to match assets, please select one:");
for (i, asset) in assets.iter().enumerate() {
println!("{}: {}", i.to_string().bright_yellow(), asset.name);
}
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let index = input.trim().parse::<usize>()?;
assets.get(index).ok_or(anyhow!("Invalid index"))
}

fn extract_zip(zip_path: &Path, output_dir: &Path, root_name: &str) -> Result<()> {
let file = File::open(zip_path)?;
let mut archive = ZipArchive::new(file)?;
Expand Down

0 comments on commit c28c68b

Please sign in to comment.