feat: cargo dependencies

This commit is contained in:
newt 2024-09-07 02:08:05 +01:00
parent b6e1390957
commit 06307071d4
4 changed files with 1627 additions and 2 deletions

1549
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,3 +5,9 @@ version = "0.1.0"
edition = "2021"
[dependencies]
cargo_toml = "0.20.4"
color-eyre = "0.6.3"
forgejo-api = "0.4.1"
futures = "0.3.30"
tokio = { version = "1.40.0", features = ["full"] }
url = "2.5.2"

View file

@ -5,4 +5,11 @@
*Todo: Write readme.*
## Environment Variables
- `FORGEJO_TOKEN` - A forgejo repository token that requires the `read:organization` and `read:repository` permissions.
- `FORGEJO_URL` (optional, default: `https://git.newty.dev/`) - The URL of the forgejo instance
- `SSH_PATH` (optional, default: `~/.ssh/id_rsa`) -
- `SSH_PASSPHRASE` (optional, default: N/A) -
<sub>This project is licensed with the Opinionated Queer License v1.2 - you can view it <a href="https://git.newty.dev/amethyst/drinkable-xp/src/branch/main/license.md">here</a>.</sub>

View file

@ -1,3 +1,66 @@
fn main() {
println!("Hello, world!");
use cargo_toml::{Dependency, Manifest};
use color_eyre::{eyre::eyre, Result};
use forgejo_api::{
structs::{OrgListReposQuery, RepoGetRawFileQuery},
Forgejo,
};
use futures::future::join_all;
use std::env;
use url::Url;
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
// authenticate with forgejo
let forgejo = {
let token = env::var("FORGEJO_TOKEN")
.map_err(|_| eyre!("Expected Forgejo PAT in environment variable FORGEJO_TOKEN."))?;
let url: Url = env::var("FORGEJO_URL")
.unwrap_or("https://git.newty.dev/".to_string())
.parse()?;
Forgejo::new(forgejo_api::Auth::Token(&token), url)?
};
// find repositories
let repo_names = forgejo
.org_list_repos("amethyst", OrgListReposQuery::default())
.await?
.iter()
.map(|repo| repo.name.clone())
.collect::<Option<Vec<_>>>()
.expect("All repos should have a name");
// find all cargo dependencies
let crates: Vec<_> = join_all(repo_names.iter().map(|name| {
forgejo.repo_get_raw_file(
"amethyst",
&name,
"Cargo.toml",
RepoGetRawFileQuery::default(),
)
}))
.await
.iter()
.filter_map(|file| file.as_ref().ok())
.map(|file| Manifest::from_slice(file))
.filter_map(|manifest| manifest.ok())
.map(|manifest| manifest.dependencies)
.flatten()
.map(|(name, metadata)| {
(
name,
match metadata {
Dependency::Simple(version) => Some(version),
Dependency::Detailed(details) => details.version,
_ => None,
},
)
})
.filter_map(|(name, version)| version.map(|version| (name, version)))
.collect();
println!("{:?}", crates);
Ok(())
}