diff --git a/Cargo.toml b/Cargo.toml index f519ee4..aa12aa9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,10 +27,7 @@ gix = "0.62" async-trait = "0.1" # fs/network -kxio = { version = "1.0", features = [ - "fs", - "network", -] } # { git = "https://git.kemitix.net/kemitix/kxio.git", branch = "main" } +kxio = { version = "1.1" } # fs tempfile = "3.10" @@ -52,6 +49,7 @@ ulid = "1.1" warp = "0.3" # error handling +derive_more = { version = "1.0.0-beta.6", features = ["from", "display"] } terrors = "0.3" # Actors diff --git a/src/init.rs b/src/init.rs index a03e15d..1eda7c6 100644 --- a/src/init.rs +++ b/src/init.rs @@ -1,17 +1,21 @@ use std::path::PathBuf; -use crate::filesystem::FileSystem; +use kxio::fs::FileSystem; pub fn run(fs: FileSystem) { let file_name = ".git-next.toml"; - let path = PathBuf::from(file_name); - if fs.file_exists(&path) { + let pathbuf = PathBuf::from(file_name); + let Ok(exists) = fs.path_exists(&pathbuf) else { + eprintln!("Could not check if file exist: {}", file_name); + return; + }; + if exists { eprintln!( "The configuration file already exists at {} - not overwritting it.", file_name ); } else { - match fs.write_file(file_name, include_str!("../default.toml")) { + match fs.file_write(&pathbuf, include_str!("../default.toml")) { Ok(_) => { println!("Created a default configuration file at {}", file_name); } diff --git a/src/main.rs b/src/main.rs index 794dd5a..526d9d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ mod init; mod server; +use std::path::PathBuf; + use clap::Parser; -use kxio::{filesystem, network::Network}; +use kxio::{fs, network::Network}; #[derive(Parser, Debug)] #[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())] @@ -24,7 +26,7 @@ enum Server { #[actix_rt::main] async fn main() { - let fs = filesystem::FileSystem::new_real(None); + let fs = fs::new(PathBuf::default()); let net = Network::new_real(); let commands = Commands::parse(); diff --git a/src/server/config/mod.rs b/src/server/config/mod.rs index 225f6cf..e433fc8 100644 --- a/src/server/config/mod.rs +++ b/src/server/config/mod.rs @@ -8,9 +8,15 @@ use std::{ }; use serde::Deserialize; -use terrors::OneOf; -use crate::filesystem::FileSystem; +use kxio::fs::FileSystem; + +#[derive(Debug, derive_more::From, derive_more::Display)] +pub enum Error { + Io(std::io::Error), + KxIoFs(kxio::fs::Error), + TomlDe(toml::de::Error), +} /// Mapped from the `git-next-server.toml` file #[derive(Debug, PartialEq, Eq, Deserialize)] @@ -20,9 +26,9 @@ pub struct ServerConfig { forge: HashMap, } impl ServerConfig { - pub(crate) fn load(fs: &FileSystem) -> Result> { - let str = fs.read_file("git-next-server.toml").map_err(OneOf::new)?; - toml::from_str(&str).map_err(OneOf::new) + pub(crate) fn load(fs: &FileSystem) -> Result { + let str = fs.file_read_to_string(&fs.base().join("git-next-server.toml"))?; + toml::from_str(&str).map_err(Into::into) } pub(crate) fn forges(&self) -> impl Iterator { diff --git a/src/server/config/tests.rs b/src/server/config/tests.rs index 1cc7a62..1c03a95 100644 --- a/src/server/config/tests.rs +++ b/src/server/config/tests.rs @@ -1,18 +1,17 @@ use assert2::let_assert; use pretty_assertions::assert_eq; -use crate::{ - filesystem::FileSystem, - server::gitforge::tests::common, /* server::gitforge::tests::common */ -}; +use crate::{server::gitforge::tests::common /* server::gitforge::tests::common */}; + +use kxio::fs; use super::*; #[test] -fn load_should_parse_server_config() -> Result<(), OneOf<(std::io::Error, toml::de::Error)>> { - let fs = FileSystem::new_temp().map_err(OneOf::new)?; - fs.write_file( - "git-next-server.toml", +fn load_should_parse_server_config() -> Result<(), crate::server::config::Error> { + let fs = fs::temp()?; + fs.file_write( + &fs.base().join("git-next-server.toml"), r#" [webhook] url = "http://localhost:9909/webhook" @@ -38,8 +37,8 @@ fn load_should_parse_server_config() -> Result<(), OneOf<(std::io::Error, toml:: dev = "sam-dev" "#, ) - .map_err(OneOf::new)?; - let config = ServerConfig::load(&fs)?; + ?; + let_assert!(Ok(config) = ServerConfig::load(&fs)); let expected = ServerConfig { webhook: Webhook { url: "http://localhost:9909/webhook".to_string(), @@ -112,7 +111,7 @@ fn load_should_parse_server_config() -> Result<(), OneOf<(std::io::Error, toml:: } #[test] -fn test_repo_config_load() -> Result<(), OneOf<(toml::de::Error,)>> { +fn test_repo_config_load() -> Result<(), crate::server::config::Error> { let toml = r#" [branches] main = "main" @@ -191,8 +190,8 @@ fn gitdir_validate_should_pass_a_valid_git_repo() -> Result<(), RepoValidationEr #[test] fn gitdir_validate_should_fail_a_non_git_dir() { - let_assert!(Ok(fs) = kxio::filesystem::FileSystem::new_temp()); - let cwd = fs.cwd(); + let_assert!(Ok(fs) = kxio::fs::temp()); + let cwd = fs.base(); let repo_details = common::repo_details( 1, common::forge_details(1, ForgeType::MockForge), diff --git a/src/server/gitforge/tests/forgejo.rs b/src/server/gitforge/tests/forgejo.rs index 611cb71..4258e97 100644 --- a/src/server/gitforge/tests/forgejo.rs +++ b/src/server/gitforge/tests/forgejo.rs @@ -8,7 +8,7 @@ use super::*; #[test] fn test_name() { - let Ok(fs) = kxio::filesystem::FileSystem::new_temp() else { + let Ok(fs) = kxio::fs::temp() else { panic!("fs") }; let net = Network::new_mock(); @@ -16,7 +16,7 @@ fn test_name() { 1, common::forge_details(1, ForgeType::MockForge), Some(common::repo_config(1)), - GitDir::new(fs.cwd()), + GitDir::new(fs.base()), ); let forge = Forge::new_forgejo(repo_details, net); assert_eq!(forge.name(), "forgejo"); @@ -24,7 +24,7 @@ fn test_name() { #[test_log::test(tokio::test)] async fn test_branches_get() { - let Ok(fs) = kxio::filesystem::FileSystem::new_temp() else { + let Ok(fs) = kxio::fs::temp() else { panic!("fs") }; let mut net = MockNetwork::new(); @@ -42,7 +42,7 @@ async fn test_branches_get() { 1, common::forge_details(1, ForgeType::MockForge), Some(common::repo_config(1)), - GitDir::new(fs.cwd()), + GitDir::new(fs.base()), ); let forge = Forge::new_forgejo(repo_details, net.clone()); diff --git a/src/server/mod.rs b/src/server/mod.rs index fd50498..0f77f29 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -12,7 +12,7 @@ use std::path::PathBuf; use tracing::{error, info, level_filters::LevelFilter}; use crate::{ - filesystem::FileSystem, + fs::FileSystem, server::{ actors::webhook, config::{ForgeConfig, ForgeName, RepoAlias, ServerStorage, Webhook}, @@ -23,14 +23,18 @@ use self::{actors::repo::RepoActor, config::ServerRepoConfig}; pub fn init(fs: FileSystem) { let file_name = "git-next-server.toml"; - let path = PathBuf::from(file_name); - if fs.file_exists(&path) { + let pathbuf = PathBuf::from(file_name); + let Ok(exists) = fs.path_exists(&pathbuf) else { + eprintln!("Could not check if file exist: {}", file_name); + return; + }; + if exists { eprintln!( "The configuration file already exists at {} - not overwritting it.", file_name ); } else { - match fs.write_file(file_name, include_str!("../../server-default.toml")) { + match fs.file_write(&pathbuf, 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)