Skip to content

Commit

Permalink
Merge branch 'main' into tui
Browse files Browse the repository at this point in the history
  • Loading branch information
adamperkowski committed Dec 5, 2024
2 parents 6703b4a + a8f4963 commit 0761c25
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 5 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,4 @@ jobs:
- uses: actions/checkout@v4

- name: Test lib & docs
run: |
cargo test --lib --all-features
cargo test --doc --all-features
run: cargo test --all-features --no-fail-fast
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ All notable changes to nvrs will be documented in this file.

- `--list-sources` command ([442c06f](https://github.com/adamperkowski/nvrs/commit/442c06f0e56f4adcc0c2ad44d042997cb088a930))

### 🚀 Features

- `--list-sources` command ([442c06f](https://github.com/adamperkowski/nvrs/commit/442c06f0e56f4adcc0c2ad44d042997cb088a930))
- (*sources*) add regex ([#13](https://github.com/adamperkowski/nvrs/issues/13)) ([fa12ce9](https://github.com/adamperkowski/nvrs/commit/fa12ce9691adbdcf51990eb8416aedf4fdc7d36b))

### 🐛 Bug Fixes

- incorrect `--compare` colors ([ef1f78f](https://github.com/adamperkowski/nvrs/commit/ef1f78fed76f883986734b7e3220b2f56508a5f0))
- (*io*) not shutting down file streams after writing ([a8a42fd](https://github.com/adamperkowski/nvrs/commit/a8a42fdf03bfde7aeee563fbd6f9d7af832bc70e))

### ⚙️ Refactoring

- (*verfiles, config*) saving & loading improvements ([81d7efd](https://github.com/adamperkowski/nvrs/commit/81d7efd24b9b425f59bec1cdbb588bc25ed433cb))
Expand All @@ -16,6 +26,11 @@ All notable changes to nvrs will be documented in this file.
### 📚 Documentation

- more details & improvements ([9f02405](https://github.com/adamperkowski/nvrs/commit/9f02405339c3520340899313365f0de2fb3d65c5))
- (*README*) update cargo install instructions ([88f0fdc](https://github.com/adamperkowski/nvrs/commit/88f0fdce435c50df44c3ae2cfd5d1087df4376fc))

### ⚙️ Miscellaneous Tasks

- (*repo*) fix dependabot ([432b10f](https://github.com/adamperkowski/nvrs/commit/432b10f32199ecd7a33c2d9643a5e1f512db862c))

### Other (unconventional)

Expand Down
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 @@ -22,10 +22,11 @@ include = [
[features]
nvrs_cli = ["clap", "colored", "futures"]
nvrs_tui = ["ratatui", "crossterm", "anyhow", "tachyonfx", "futures"]
default = ["aur", "github", "gitlab"]
default = ["aur", "github", "gitlab", "regex"]
aur = []
github = []
gitlab = []
regex = ["dep:regex"]

[[bin]]
name = "nvrs"
Expand All @@ -44,6 +45,7 @@ colored = { version = "2.1.0", optional = true }
crossterm = { version = "0.28.1", features = ["events", "event-stream"], default-features = false, optional = true }
futures = { version = "0.3.31", optional = true }
ratatui = { version = "0.29.0", 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 @@ -30,6 +30,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 @@ -145,6 +146,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());
}
19 changes: 19 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 Expand Up @@ -187,6 +204,8 @@ pub async fn save(config_content: Config, path: PathBuf) -> error::Result<()> {
let mut file = fs::File::create(path).await?;
let content = format!("{}\n", toml::to_string(&config_content)?);
file.write_all(content.as_bytes()).await?;
file.shutdown().await?;

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn compare(core: Core) -> error::Result<()> {
"*".white().on_black(),
new_pkg.0.blue(),
old_pkg.1.version.red(),
new_pkg.1.version.blue()
new_pkg.1.version.green()
);
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/verfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ pub async fn save(
let content = format!("{}\n", serde_json::to_string_pretty(&verfile)?);

file.write_all(content.as_bytes()).await?;
file.shutdown().await?;

Ok(())
}

async fn load_file(path: &Path) -> error::Result<Verfile> {
if !path.exists() {
let mut file = fs::File::create(path).await?;
file.write_all(TEMPLATE.as_bytes()).await?;
file.shutdown().await?;
}

let content = fs::read_to_string(path).await?;
Expand Down

0 comments on commit 0761c25

Please sign in to comment.