Compare commits
No commits in common. "efb55e4b3bba61081d2641e244777d7389536fe2" and "4a4da2f8c0c8a60b1d4ecfa76cda05228f72f758" have entirely different histories.
efb55e4b3b
...
4a4da2f8c0
5 changed files with 5 additions and 112 deletions
|
@ -17,17 +17,9 @@ tracing-subscriber = "0.3"
|
||||||
# fs
|
# fs
|
||||||
tempfile = "3.10"
|
tempfile = "3.10"
|
||||||
|
|
||||||
# TOML parsing
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
|
||||||
toml = "0.8"
|
|
||||||
|
|
||||||
# error handling
|
|
||||||
terrors = "0.3"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
# Testing
|
# Testing
|
||||||
test-log = "0.2"
|
test-log = "0.2"
|
||||||
anyhow = "1.0"
|
|
||||||
|
|
||||||
[package.metadata.bin]
|
[package.metadata.bin]
|
||||||
# Conventional commits githook
|
# Conventional commits githook
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
[forge.default]
|
|
||||||
type = "forgejo"
|
|
||||||
url = "https://git.example.net"
|
|
||||||
user = "git-next" # the user to perform actions as
|
|
||||||
# API token for user?
|
|
||||||
# path to private SSH key for user?
|
|
||||||
|
|
||||||
[forge.default.repos]
|
|
||||||
hello = "user/hello" # maps to https://git.example.net/user/hello and git@git.example.net:user/hello.git
|
|
|
@ -7,7 +7,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use tempfile::{tempdir, TempDir};
|
use tempfile::{tempdir, TempDir};
|
||||||
use tracing::info;
|
use tracing::{event, Level};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum FileSystem {
|
pub enum FileSystem {
|
||||||
|
@ -46,7 +46,7 @@ pub trait FileSystemEnv: Sync + Send + std::fmt::Debug {
|
||||||
use std::io::{LineWriter, Write};
|
use std::io::{LineWriter, Write};
|
||||||
|
|
||||||
let path = self.in_cwd(file_name);
|
let path = self.in_cwd(file_name);
|
||||||
info!("writing to {:?}", path);
|
event!(Level::INFO, "writing to {:?}", path);
|
||||||
let file = File::create(path.clone())?;
|
let file = File::create(path.clone())?;
|
||||||
let mut file = LineWriter::new(file);
|
let mut file = LineWriter::new(file);
|
||||||
file.write_all(content.as_bytes())?;
|
file.write_all(content.as_bytes())?;
|
||||||
|
@ -57,18 +57,6 @@ pub trait FileSystemEnv: Sync + Send + std::fmt::Debug {
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
File::open(name).is_ok()
|
File::open(name).is_ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_file(&self, file_name: &str) -> std::io::Result<String> {
|
|
||||||
use std::fs::File;
|
|
||||||
use std::io::Read;
|
|
||||||
|
|
||||||
let path = self.in_cwd(file_name);
|
|
||||||
info!("reading from {:?}", path);
|
|
||||||
let mut file = File::open(path)?;
|
|
||||||
let mut content = String::new();
|
|
||||||
file.read_to_string(&mut content)?;
|
|
||||||
Ok(content)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
|
@ -103,7 +91,7 @@ impl RealFileSystemEnv {
|
||||||
impl TempFileSystemEnv {
|
impl TempFileSystemEnv {
|
||||||
fn new() -> std::io::Result<Self> {
|
fn new() -> std::io::Result<Self> {
|
||||||
let temp_dir = tempdir()?;
|
let temp_dir = tempdir()?;
|
||||||
info!("temp dir: {:?}", temp_dir.path());
|
event!(Level::INFO, "temp dir: {:?}", temp_dir.path());
|
||||||
let cwd = temp_dir.path().to_path_buf();
|
let cwd = temp_dir.path().to_path_buf();
|
||||||
let temp_dir = Arc::new(Mutex::new(temp_dir));
|
let temp_dir = Arc::new(Mutex::new(temp_dir));
|
||||||
Ok(Self { cwd, temp_dir })
|
Ok(Self { cwd, temp_dir })
|
||||||
|
@ -137,15 +125,4 @@ mod tests {
|
||||||
let env = RealFileSystemEnv::new(cwd.clone());
|
let env = RealFileSystemEnv::new(cwd.clone());
|
||||||
assert_eq!(env.cwd(), &cwd);
|
assert_eq!(env.cwd(), &cwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test_log::test]
|
|
||||||
fn test_write_and_read_file() -> std::io::Result<()> {
|
|
||||||
let env = TempFileSystemEnv::new()?;
|
|
||||||
let file_name = "test.txt";
|
|
||||||
let content = "Hello, World!";
|
|
||||||
let path = env.write_file(file_name, content)?;
|
|
||||||
assert_eq!(env.read_file(file_name)?, content);
|
|
||||||
assert!(path.exists());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
mod config;
|
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
@ -15,7 +13,7 @@ pub(crate) fn init(fs: FileSystem) {
|
||||||
file_name
|
file_name
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
match fs.write_file(file_name, include_str!("../../server-default.toml")) {
|
match fs.write_file(file_name, include_str!("../server-default.toml")) {
|
||||||
Ok(_) => println!("Created a default configuration file at {}", file_name),
|
Ok(_) => println!("Created a default configuration file at {}", file_name),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Failed to write to the configuration file: {}", e)
|
eprintln!("Failed to write to the configuration file: {}", e)
|
||||||
|
@ -24,13 +22,12 @@ pub(crate) fn init(fs: FileSystem) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn start(fs: FileSystem) {
|
pub(crate) fn start(_fs: FileSystem) {
|
||||||
let Ok(_) = init_logging() else {
|
let Ok(_) = init_logging() else {
|
||||||
eprintln!("Failed to initialize logging.");
|
eprintln!("Failed to initialize logging.");
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
info!("Starting Server...");
|
info!("Starting Server...");
|
||||||
let _config = config::Config::load(&fs);
|
|
||||||
// todo!()
|
// todo!()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
use serde::Deserialize;
|
|
||||||
use terrors::OneOf;
|
|
||||||
|
|
||||||
use crate::filesystem::FileSystem;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Deserialize)]
|
|
||||||
pub(crate) struct Config {
|
|
||||||
r#type: ForgeType,
|
|
||||||
url: String,
|
|
||||||
user: String,
|
|
||||||
// API Token
|
|
||||||
// Private SSH Key Path
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Deserialize)]
|
|
||||||
pub(crate) enum ForgeType {
|
|
||||||
ForgeJo,
|
|
||||||
// Gitea,
|
|
||||||
// GitHub,
|
|
||||||
// GitLab,
|
|
||||||
// BitBucket,
|
|
||||||
}
|
|
||||||
impl Config {
|
|
||||||
pub(crate) fn load(fs: &FileSystem) -> Result<Self, OneOf<(std::io::Error, toml::de::Error)>> {
|
|
||||||
let str = fs.read_file("git-next-server.toml").map_err(OneOf::new)?;
|
|
||||||
toml::from_str(&str).map_err(OneOf::new)
|
|
||||||
}
|
|
||||||
pub const fn r#type(&self) -> &ForgeType {
|
|
||||||
&self.r#type
|
|
||||||
}
|
|
||||||
pub fn url(&self) -> &str {
|
|
||||||
self.url.as_str()
|
|
||||||
}
|
|
||||||
pub fn user(&self) -> &str {
|
|
||||||
self.user.as_str()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use crate::filesystem::FileSystem;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_config_load() -> Result<(), OneOf<(std::io::Error, toml::de::Error)>> {
|
|
||||||
let fs = FileSystem::new_temp().map_err(OneOf::new)?;
|
|
||||||
fs.write_file(
|
|
||||||
"git-next-server.toml",
|
|
||||||
r#"
|
|
||||||
type = "ForgeJo"
|
|
||||||
url = "https://forge.jo"
|
|
||||||
user = "Bob"
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.map_err(OneOf::new)?;
|
|
||||||
let config = Config::load(&fs)?;
|
|
||||||
assert_eq!(config.r#type(), &ForgeType::ForgeJo);
|
|
||||||
assert_eq!(config.url(), "https://forge.jo".to_string());
|
|
||||||
assert_eq!(config.user(), "Bob".to_string());
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue