add reading config, add and update docs

This commit is contained in:
askiiart 2024-12-26 12:18:16 -06:00
parent e9af17baf0
commit bd880f4f5f
Signed by untrusted user who does not match committer: askiiart
GPG key ID: EA85979611654C30
9 changed files with 265 additions and 89 deletions

View file

@ -8,6 +8,8 @@ This is Gregory. Gregory controls repos. Gregory keeps track of updating repos,
- Add support for loading scripts rather than listing commands
- Add multithreading
- Add better/custom grouping for when to run jobs (dependency system?)
- Add dependency system (automatic detection?)
- Add hook system
## Other stuff

35
src/benchmark.rs Normal file
View file

@ -0,0 +1,35 @@
use std::time::Instant;
fn log_thing(total_threads: u32) -> u32 {
return total_threads - (f64::log(total_threads.into(), 5.0).round() as u32);
}
fn my_thing(total_threads: u32) -> u32 {
if total_threads >= 32 {
return total_threads - 4;
} else if total_threads >= 12 {
return total_threads - 2;
} else if total_threads >= 3 {
return total_threads - 1;
} else {
return total_threads;
}
//println!("{}", max_threads)
}
// let mut total_threads = thread::available_parallelism().unwrap().get() as u32;
fn main() {
/*
let now = Instant::now();
for _ in 0..100000000 {
}
let elapsed = now.elapsed();
*/
let total_threads: u32 = 128;
println!("{}", log_thing(total_threads));
println!("{}", my_thing(total_threads));
//println!("{}", elapsed.as_nanos());
}

91
src/data.rs Normal file
View file

@ -0,0 +1,91 @@
//! Datasets used by gregory and stuff for handling them
use serde::Deserialize;
use std::{collections::HashMap, fs, thread};
/// The config for gregory
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct Config {
/// What level to log at
///
/// - 0: Error
/// - 1: Warning
/// - 2: Info
/// - 3: Debug
#[serde(default = "log_level", rename = "log-level")]
log_level: u8,
/// Maximum number of jobs to run simultaneously
#[serde(default = "max_jobs", rename = "max-jobs")]
max_jobs: u32,
/// Maximum number of threads to use
#[serde(default = "max_threads", rename = "max-threads")]
max_threads: u32,
/// Holds the packages, including their compilation and packaging
///
/// Format: { "librewolf": Package { compilation, packaging } }
///
/// See [`Package`] for details
packages: HashMap<String, Package>,
#[serde(rename = "update-repo")]
update_repo: HashMap<String, Job>,
#[serde(default = "volumes")]
volumes: HashMap<String, String>,
}
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct Job {
#[serde(default = "job_threads")]
/// How many threads to limit this job to; recommended to set it to the max threads the job will use
///
/// If `threads` isn't specified, it will fall back to `max_threads` (from [`Config`]); the same behavior applies if `threads` is greater than `max_threads`
threads: u32,
image: String,
commands: Vec<String>,
volumes: Option<Vec<String>>,
}
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct Package {
compilation: Option<Job>,
packaging: HashMap<String, Job>,
}
pub(crate) fn config_from_file(filename: String) -> Config {
let yaml: Config = serde_yml::from_str(fs::read_to_string(filename).unwrap().as_str()).unwrap();
return yaml;
}
// ==========================
// === ===
// === ↓ DEFAULTS ↓ ===
// === ===
// ==========================
pub(crate) fn log_level() -> u8 {
return 1;
}
pub(crate) fn max_threads() -> u32 {
let total_threads = thread::available_parallelism().unwrap().get() as u32;
if total_threads >= 32 {
return total_threads - 4;
} else if total_threads >= 12 {
return total_threads - 2;
} else if total_threads >= 3 {
return total_threads - 1;
} else {
return total_threads;
}
}
pub(crate) fn max_jobs() -> u32 {
return 1;
}
pub(crate) fn volumes() -> HashMap<String, String> {
return HashMap::new();
}
pub(crate) fn job_threads() -> u32 {
return max_threads();
}

View file

@ -1,11 +1,12 @@
use crate::cli::*;
use crate::data::*;
use alphanumeric_sort::sort_str_slice;
use clap::{CommandFactory, Parser};
use clap_complete::aot::{generate, Bash, Elvish, Fish, PowerShell, Zsh};
use std::fs;
use std::io::stdout;
use yaml_rust2::YamlLoader;
mod cli;
mod data;
mod tests;
fn main() {
@ -29,14 +30,15 @@ fn main() {
generate(PowerShell, &mut Cli::command(), binary_name, &mut stdout());
}
},
Commands::Run { config} => {
println!("{}", config);
Commands::Run { config } => {
println!("Config path: {}", config);
run(config);
}
}
}
fn run(config_path: String) {
let tmp = fs::read_to_string(config_path.as_str()).unwrap();
let config = YamlLoader::load_from_str(tmp.as_str()).unwrap()[0].clone();
println!("{:?}", config)
let config = config_from_file(config_path);
println!("{:?}", config);
}

View file

@ -1,7 +1,7 @@
use alphanumeric_sort::sort_str_slice;
#[test]
/// This isn't to test the program, more to test the crate works how I want, especially if I switch crates
/// There sorting tests aren't to test the program, more to test the crate works how I want, especially if I switch crates
fn test_semver_sorting() {
// copied from https://pkgs.org/download/xorg-x11-xauth
let mut versions = [