Skip to content

Commit

Permalink
feat(sources): add regex (#13)
Browse files Browse the repository at this point in the history
Add source type regex

Search through a webpage for the version string that fits a given regex.
The first match of the regex is returned.

---------

Co-authored-by: Adam Perkowski <[email protected]>
  • Loading branch information
pisquaredover6 and adamperkowski authored Dec 5, 2024
1 parent a8a42fd commit fa12ce9
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ include = [

[features]
nvrs_cli = ["clap", "colored", "futures"]
default = ["aur", "github", "gitlab"]
default = ["aur", "github", "gitlab", "regex"]
aur = []
github = []
gitlab = []
regex = ["dep:regex"]

[[bin]]
name = "nvrs"
Expand All @@ -40,6 +41,7 @@ required-features = ["nvrs_tui"]
clap = { version = "4.5.22", features = ["derive", "color", "error-context", "help", "std", "usage"], default-features = false , optional = true }
colored = { version = "2.1.0", optional = true }
futures = { version = "0.3.31", optional = true }
regex = { version = "1.11.1", optional = true }
reqwest = { version = "0.12.9", features = ["__tls", "charset", "default-tls", "h2", "http2", "json"], default-features = false }
serde = { version = "1.0.215", features = ["derive"], default-features = false }
serde_json = "1.0.132"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ check the [release notes](https://github.com/adamperkowski/nvrs/releases) for co
- `aur`
- `github`
- `gitlab` (with custom hosts)
- `website` (regex)

### QOL improvements
- `ALL` argument for the `--take` command
Expand Down Expand Up @@ -143,6 +144,8 @@ package entries are custom entries in the main config file. they contain values
| `host` | domain name the source is hosted on | string |||
| `prefix` | the prefix used in releases / tags<br>example: `v` for tags like `v0.1.0` | string |||
| `use_max_tag` | use max git tag instead of the latest release | bool |||
| `url` | url to check for source type `regex` | string |||
| `regex` | regex to search url for source type `regex` | bool |||

### Keyfile structure
this file contains API keys for various [sources](#sources). example can be found [here](/n_keyfile.toml).
Expand Down
12 changes: 12 additions & 0 deletions nvrs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ prefix = "v"
source = "github"
github = "rust-lang/rustup"
use_max_tag = true

[rustrover]
source = 'regex'
url = 'https://data.services.jetbrains.com/products?code=RR&release.type=release'
encoding = 'utf8'
regex = 'RustRover-([\d.]+).tar.gz'

[linux]
source = 'regex'
url = 'https://www.kernel.org/'
encoding = 'utf8'
regex = '<td><strong>([\d.]+)</strong></td>'
7 changes: 7 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mod aur;
mod github;
#[cfg(feature = "gitlab")]
mod gitlab;
#[cfg(feature = "regex")]
mod regex;

/// struct containing the API name & a pointer to API's `get_latest` function
pub struct Api {
Expand Down Expand Up @@ -78,6 +80,11 @@ pub const API_LIST: &[Api] = &[
name: "gitlab",
func: gitlab::get_latest,
},
#[cfg(feature = "regex")]
Api {
name: "regex",
func: regex::get_latest,
},
];

#[test]
Expand Down
47 changes: 47 additions & 0 deletions src/api/regex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::{api, error};
use regex::Regex;

/// get a version string from a webpage
pub fn get_latest(args: api::ApiArgs) -> api::ReleaseFuture {
Box::pin(async move {
let url = args.args[0].clone();
let client = args.request_client;

let result = client
.get(&url)
.headers(api::setup_headers())
.send()
.await?;
api::match_statuscode(&result.status(), args.package.clone())?;

let body = result.text().await?;

let re = Regex::new(&args.args[1]).unwrap();
if let Some(caps) = re.captures(&body) {
Ok(api::Release {
name: caps.get(1).unwrap().as_str().to_owned(),
tag: None,
url,
})
} else {
Err(error::Error::NoVersion(args.package))
}
})
}

#[tokio::test]
async fn request_test() {
let package = "rustrover".to_string();
let args = api::ApiArgs {
request_client: reqwest::Client::new(),
package: package.clone(),
use_max_tag: None,
args: vec![
"https://data.services.jetbrains.com/products?code=RR&release.type=release".to_string(),
r"RustRover-([\d.]+).tar.gz".to_string(),
],
api_key: String::new(),
};

assert!(crate::api::regex::get_latest(args).await.is_ok());
}
17 changes: 17 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ pub struct Package {
#[serde(default)]
#[serde(skip_serializing_if = "is_empty_string")]
gitlab: String,
#[cfg(feature = "regex")]
#[serde(default)]
url: String,
#[cfg(feature = "regex")]
#[serde(default)]
regex: String,

/// whether to use the latest tag instead of the latest release
#[serde(default)]
Expand Down Expand Up @@ -95,6 +101,11 @@ impl Package {
package.gitlab = target;
Ok(())
}
#[cfg(feature = "regex")]
"regex" => {
package.url = target;
Ok(())
}
_ => Err(error::Error::SourceNotFound(source.clone())),
}?;

Expand All @@ -115,6 +126,10 @@ impl Package {
github: String::new(),
#[cfg(feature = "gitlab")]
gitlab: String::new(),
#[cfg(feature = "regex")]
url: String::new(),
#[cfg(feature = "regex")]
regex: String::new(),
use_max_tag: None,
prefix: String::new(),
}
Expand All @@ -137,6 +152,8 @@ impl Package {
"github" => vec![self.github.clone()],
#[cfg(feature = "gitlab")]
"gitlab" => vec![self.gitlab.clone(), self.host.clone()],
#[cfg(feature = "regex")]
"regex" => vec![self.url.clone(), self.regex.clone()],
_ => vec![],
};

Expand Down
1 change: 1 addition & 0 deletions src/tui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit fa12ce9

Please sign in to comment.