Use Utf8PathBuf

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
xmppftw 2024-08-07 11:35:47 +02:00 committed by Maxime “pep” Buquet
parent ad3bcc88de
commit 66e4131bd3
3 changed files with 11 additions and 14 deletions

View file

@ -25,6 +25,7 @@ xmpp = "0.5"
hmac = "0.12" hmac = "0.12"
sha2 = "0.10" sha2 = "0.10"
hex = "0.4" hex = "0.4"
camino = { version = "1.1", features = ["serde"] }
[patch.crates-io] [patch.crates-io]
forgejo-hooks = { path = "forgejo-hooks" } forgejo-hooks = { path = "forgejo-hooks" }

View file

@ -15,8 +15,8 @@
use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use std::net::{IpAddr, Ipv6Addr, SocketAddr}; use std::net::{IpAddr, Ipv6Addr, SocketAddr};
use std::path::{Path, PathBuf};
use camino::{Utf8Path, Utf8PathBuf};
use jid::BareJid; use jid::BareJid;
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -57,7 +57,7 @@ impl Config {
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 3000) SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 3000)
} }
pub fn from_file(file: PathBuf) -> Result<Config, Error> { pub fn from_file(file: Utf8PathBuf) -> Result<Config, Error> {
if file.try_exists().is_err() { if file.try_exists().is_err() {
let err = IoError::new(IoErrorKind::NotFound, format!("{:?} not found", file)); let err = IoError::new(IoErrorKind::NotFound, format!("{:?} not found", file));
return Err(Error::Io(err)); return Err(Error::Io(err));
@ -68,20 +68,16 @@ impl Config {
Ok(toml::from_str(&buf)?) Ok(toml::from_str(&buf)?)
} }
pub fn from_arg(file: Option<&PathBuf>) -> Result<Config, Error> { pub fn from_arg(file: Option<&Utf8PathBuf>) -> Result<Config, Error> {
let path = if let Some(path) = file { let path = if let Some(path) = file {
// Provided by --config flag // Provided by --config flag
if !path.starts_with("/") { path.canonicalize_utf8()?
std::env::current_dir()?.join(path)
} else {
path.to_path_buf()
}
} else { } else {
let confdir: PathBuf = match std::env::var("XDG_CONFIG_HOME") { let confdir: Utf8PathBuf = match std::env::var("XDG_CONFIG_HOME") {
Ok(ref dir) => Path::new(dir).to_path_buf(), Ok(ref dir) => Utf8Path::new(dir).to_path_buf(),
Err(_) => { Err(_) => {
let home = std::env::var("HOME")?; let home = std::env::var("HOME")?;
Path::new(home.as_str()).join(".config") Utf8Path::new(home.as_str()).join(".config")
} }
}; };

View file

@ -28,9 +28,9 @@ use crate::error::Error;
use crate::hook::Hook; use crate::hook::Hook;
use crate::web::hooks; use crate::web::hooks;
use std::path::PathBuf;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use camino::Utf8PathBuf;
use clap::{command, value_parser, Arg}; use clap::{command, value_parser, Arg};
use hyper::{server::conn::http1, service::service_fn}; use hyper::{server::conn::http1, service::service_fn};
use hyper_util::rt::tokio::{TokioIo, TokioTimer}; use hyper_util::rt::tokio::{TokioIo, TokioTimer};
@ -46,11 +46,11 @@ async fn main() -> Result<!, Error> {
.short('c') .short('c')
.long("config") .long("config")
.required(false) .required(false)
.value_parser(value_parser!(PathBuf)), .value_parser(value_parser!(Utf8PathBuf)),
) )
.get_matches(); .get_matches();
let config = Config::from_arg(matches.get_one::<PathBuf>("config"))?; let config = Config::from_arg(matches.get_one::<Utf8PathBuf>("config"))?;
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<Hook>(); let (value_tx, mut value_rx) = mpsc::unbounded_channel::<Hook>();