git-next/src/server/mod.rs
Paul Campbell 1de2bd2ecb
All checks were successful
ci/woodpecker/push/docker Pipeline was successful
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/todo-check Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
feat(server): log parsed config
2024-04-07 19:10:37 +01:00

72 lines
2.1 KiB
Rust

mod config;
use std::path::PathBuf;
use tracing::{error, info};
use crate::filesystem::FileSystem;
pub fn init(fs: FileSystem) {
let file_name = "git-next-server.toml";
let path = PathBuf::from(file_name);
if fs.file_exists(&path) {
eprintln!(
"The configuration file already exists at {} - not overwritting it.",
file_name
);
} else {
match fs.write_file(file_name, include_str!("../../server-default.toml")) {
Ok(_) => println!("Created a default configuration file at {}", file_name),
Err(e) => {
eprintln!("Failed to write to the configuration file: {}", e)
}
}
}
}
pub fn start(fs: FileSystem) {
let Ok(_) = init_logging() else {
eprintln!("Failed to initialize logging.");
return;
};
info!("Starting Server...");
let config = match config::Config::load(&fs) {
Ok(config) => config,
Err(err) => {
error!("Failed to load config file. Error: {}", err);
return;
}
};
info!("Config loaded");
info!("Loaded config");
config.forges().for_each(|(name, forge)| {
let span = tracing::info_span!("Forge", name);
let _guard = span.enter();
info!("Forge: {}", name);
info!("Forge Type: {}", forge.forge_type());
info!("Hostname: {}", forge.hostname());
info!("User: {}", forge.user());
forge.repos().for_each(|(name, path)| {
let span = tracing::info_span!("Repo", name);
let _guard = span.enter();
info!("Repo: {} - {}", name, path);
});
});
}
pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> {
use tracing_subscriber::prelude::*;
let console_layer = console_subscriber::ConsoleLayer::builder().spawn();
let subscriber = tracing_subscriber::fmt::layer()
// NOTE: set RUSTLOG in ${root}/.cargo/config
.with_target(false)
.with_file(true)
.with_line_number(true);
tracing_subscriber::registry()
.with(console_layer)
.with(subscriber)
.init();
Ok(())
}