set up config, discovering remotes

This commit is contained in:
Luca Bilke 2024-06-04 03:00:22 +02:00
commit 4611d251d5
No known key found for this signature in database
GPG Key ID: C9E851809C1A5BDE
8 changed files with 1515 additions and 0 deletions

11
.editorconfig Normal file
View File

@ -0,0 +1,11 @@
root = true
[*]
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
[*.{yaml,yml,json}]
indent_size = 2

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1394
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

23
Cargo.toml Normal file
View File

@ -0,0 +1,23 @@
[package]
name = "klados"
version = "0.1.0"
edition = "2021"
[dependencies.reqwest]
version = "0.12.4"
features = ["json", "blocking"]
[dependencies.git2]
version = "0.18.3"
[dependencies.serde_json]
version = "1.0.117"
[dependencies.serde]
version = "1.0.203"
features = ["derive"]
[dependencies.confy]
version = "0.6.1"
features = ["yaml_conf"]
default-features = false

11
TODO.md Normal file
View File

@ -0,0 +1,11 @@
# TODO
- [X] Define config schema
- [X] Parse config
- [ ] Discover local repositories in $KLADOS_DIR
- [X] Discover remote repositories
- [ ] Clone remote repositories
- [ ] List unclean local repositories
- [ ] Pull clean local repositories
- [ ] Execute hook scripts
- [ ] Inline TUI

34
src/config.rs Normal file
View File

@ -0,0 +1,34 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct Config {
blacklist: Option<Vec<String>>,
lookups: Option<Vec<Lookup>>,
map: Option<Vec<Map>>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Lookup {
name: String,
url: String,
field: String,
interpolate: Option<Vec<String>>,
token_cmd: Option<String>,
block_unmatched: Option<bool>,
auth_header: Option<String>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Map {
patterns: Option<Vec<String>>,
url: Option<String>,
directory: Option<String>,
rename: Option<Vec<Rename>>,
ignore: Option<bool>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Rename {
replace: String,
with: String,
}

6
src/main.rs Normal file
View File

@ -0,0 +1,6 @@
pub mod config;
pub mod remote;
fn main() {
let cfg: config::Config = confy::load("klados", "config").unwrap();
}

35
src/remote.rs Normal file
View File

@ -0,0 +1,35 @@
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
use serde_json::Value;
pub fn get_remote_urls(
endpoint: &str,
auth_header: &str,
field: &str,
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let handle = reqwest::blocking::Client::new();
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
headers.insert(
"Authorization",
HeaderValue::from_str(auth_header).expect("Malformed auth header"),
);
let res = handle
.get(endpoint)
.headers(headers)
.send()
.expect("Request to API Failed")
.json::<Value>()
.expect("Malformed JSON Response from API");
let urls = res
.as_array()
.unwrap_or(&Vec::new())
.iter()
.filter_map(|obj| obj[field].as_str())
.map(|url| url.to_string())
.collect();
Ok(urls)
}