Derive repo name based on repo path

This commit is contained in:
Joscha 2023-08-09 00:24:34 +02:00
parent 6604cccf9e
commit a261bfdd26
2 changed files with 48 additions and 26 deletions

View file

@ -1,11 +1,21 @@
//! Configuration from a file. //! Configuration from a file.
use std::{fs, io::ErrorKind, net::SocketAddr, path::Path, time::Duration}; use std::{
fs,
io::ErrorKind,
net::SocketAddr,
path::{Path, PathBuf},
time::Duration,
};
use directories::ProjectDirs;
use serde::Deserialize; use serde::Deserialize;
use tracing::{debug, info}; use tracing::{debug, info};
use crate::somehow; use crate::{
args::{Args, Command, ServerCommand},
somehow,
};
mod default { mod default {
use std::{net::SocketAddr, time::Duration}; use std::{net::SocketAddr, time::Duration};
@ -19,10 +29,6 @@ mod default {
"[::1]:8221".parse().unwrap() "[::1]:8221".parse().unwrap()
} }
pub fn repo_name() -> String {
"local repo".to_string()
}
pub fn repo_update_delay() -> Duration { pub fn repo_update_delay() -> Duration {
Duration::from_secs(60) Duration::from_secs(60)
} }
@ -47,8 +53,7 @@ impl Default for Web {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Repo { pub struct Repo {
#[serde(default = "default::repo_name")] pub name: Option<String>,
pub name: String,
#[serde(default = "default::repo_update_delay", with = "humantime_serde")] #[serde(default = "default::repo_update_delay", with = "humantime_serde")]
pub update_delay: Duration, pub update_delay: Duration,
} }
@ -56,7 +61,7 @@ pub struct Repo {
impl Default for Repo { impl Default for Repo {
fn default() -> Self { fn default() -> Self {
Self { Self {
name: default::repo_name(), name: None,
update_delay: default::repo_update_delay(), update_delay: default::repo_update_delay(),
} }
} }
@ -91,6 +96,22 @@ impl ConfigFile {
.unwrap_or(&self.web.base) .unwrap_or(&self.web.base)
.to_string() .to_string()
} }
fn repo_name(&self, args: &Args) -> somehow::Result<String> {
if let Some(name) = &self.repo.name {
return Ok(name.clone());
}
if let Command::Server(ServerCommand { repo, .. }) = &args.command {
if let Some(name) = repo.canonicalize()?.file_name() {
let name = name.to_string_lossy();
let name = name.strip_suffix(".git").unwrap_or(&name).to_string();
return Ok(name);
}
}
Ok("unnamed repo".to_string())
}
} }
pub struct Config { pub struct Config {
@ -101,17 +122,30 @@ pub struct Config {
} }
impl Config { impl Config {
pub fn load(path: &Path) -> somehow::Result<Self> { fn path(args: &Args) -> PathBuf {
if let Some(path) = &args.config {
return path.clone();
}
ProjectDirs::from("de", "plugh", "tablejohn")
.expect("could not determine home directory")
.config_dir()
.join("config.toml")
}
pub fn load(args: &Args) -> somehow::Result<Self> {
let path = Self::path(args);
info!(path = %path.display(), "Loading config"); info!(path = %path.display(), "Loading config");
let config_file = ConfigFile::load(path)?; let config_file = ConfigFile::load(&path)?;
debug!("Loaded config file:\n{config_file:#?}"); debug!("Loaded config file:\n{config_file:#?}");
let web_base = config_file.web_base(); let web_base = config_file.web_base();
let repo_name = config_file.repo_name(args)?;
Ok(Self { Ok(Self {
web_base, web_base,
web_address: config_file.web.address, web_address: config_file.web.address,
repo_name: config_file.repo.name, repo_name,
repo_update_delay: config_file.repo.update_delay, repo_update_delay: config_file.repo.update_delay,
}) })
} }

View file

@ -4,10 +4,9 @@ mod runner;
mod server; mod server;
mod somehow; mod somehow;
use std::{io, path::PathBuf, process}; use std::{io, process};
use clap::Parser; use clap::Parser;
use directories::ProjectDirs;
use tokio::{select, signal::unix::SignalKind}; use tokio::{select, signal::unix::SignalKind};
use tracing::{debug, error, info, Level}; use tracing::{debug, error, info, Level};
use tracing_subscriber::{ use tracing_subscriber::{
@ -51,17 +50,6 @@ fn set_up_logging(verbose: u8) {
} }
} }
fn load_config(path: Option<PathBuf>) -> somehow::Result<&'static Config> {
let config_path = path.unwrap_or_else(|| {
ProjectDirs::from("de", "plugh", "tablejohn")
.expect("could not determine home directory")
.config_dir()
.join("config.toml")
});
Ok(Box::leak(Box::new(Config::load(&config_path)?)))
}
async fn wait_for_signal() -> io::Result<()> { async fn wait_for_signal() -> io::Result<()> {
debug!("Listening to signals"); debug!("Listening to signals");
@ -98,7 +86,7 @@ async fn run() -> somehow::Result<()> {
set_up_logging(args.verbose); set_up_logging(args.verbose);
info!("You are running {NAME} {VERSION}"); info!("You are running {NAME} {VERSION}");
let config = load_config(args.config)?; let config = Box::leak(Box::new(Config::load(&args)?));
match args.command { match args.command {
Command::Server(command) => { Command::Server(command) => {