From a261bfdd2668827114c019a06506cd88178a01d0 Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 9 Aug 2023 00:24:34 +0200 Subject: [PATCH] Derive repo name based on repo path --- src/config.rs | 58 ++++++++++++++++++++++++++++++++++++++++----------- src/main.rs | 16 ++------------ 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/config.rs b/src/config.rs index ebfa1eb..b86baae 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,21 @@ //! 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 tracing::{debug, info}; -use crate::somehow; +use crate::{ + args::{Args, Command, ServerCommand}, + somehow, +}; mod default { use std::{net::SocketAddr, time::Duration}; @@ -19,10 +29,6 @@ mod default { "[::1]:8221".parse().unwrap() } - pub fn repo_name() -> String { - "local repo".to_string() - } - pub fn repo_update_delay() -> Duration { Duration::from_secs(60) } @@ -47,8 +53,7 @@ impl Default for Web { #[derive(Debug, Deserialize)] pub struct Repo { - #[serde(default = "default::repo_name")] - pub name: String, + pub name: Option, #[serde(default = "default::repo_update_delay", with = "humantime_serde")] pub update_delay: Duration, } @@ -56,7 +61,7 @@ pub struct Repo { impl Default for Repo { fn default() -> Self { Self { - name: default::repo_name(), + name: None, update_delay: default::repo_update_delay(), } } @@ -91,6 +96,22 @@ impl ConfigFile { .unwrap_or(&self.web.base) .to_string() } + + fn repo_name(&self, args: &Args) -> somehow::Result { + 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 { @@ -101,17 +122,30 @@ pub struct Config { } impl Config { - pub fn load(path: &Path) -> somehow::Result { + 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 { + let path = Self::path(args); 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:#?}"); let web_base = config_file.web_base(); + let repo_name = config_file.repo_name(args)?; Ok(Self { web_base, web_address: config_file.web.address, - repo_name: config_file.repo.name, + repo_name, repo_update_delay: config_file.repo.update_delay, }) } diff --git a/src/main.rs b/src/main.rs index fa55dca..7fabdbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,9 @@ mod runner; mod server; mod somehow; -use std::{io, path::PathBuf, process}; +use std::{io, process}; use clap::Parser; -use directories::ProjectDirs; use tokio::{select, signal::unix::SignalKind}; use tracing::{debug, error, info, Level}; use tracing_subscriber::{ @@ -51,17 +50,6 @@ fn set_up_logging(verbose: u8) { } } -fn load_config(path: Option) -> 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<()> { debug!("Listening to signals"); @@ -98,7 +86,7 @@ async fn run() -> somehow::Result<()> { set_up_logging(args.verbose); info!("You are running {NAME} {VERSION}"); - let config = load_config(args.config)?; + let config = Box::leak(Box::new(Config::load(&args)?)); match args.command { Command::Server(command) => {