From 9a9c73d9293f639c80d6df3cf500b50fea1abc45 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 30 Jul 2024 10:52:05 +0100 Subject: [PATCH] feat: return better errors to the user on init --- Cargo.toml | 1 + crates/cli/Cargo.toml | 1 + crates/cli/src/init.rs | 37 +++++++++++++----------------------- crates/cli/src/main.rs | 8 +++++--- crates/cli/src/server/mod.rs | 27 +++++++++++--------------- crates/cli/src/tests.rs | 4 ++-- 6 files changed, 33 insertions(+), 45 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 84eda05f..2b1970ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,6 +80,7 @@ derive_more = { version = "1.0.0-beta.6", features = [ "from", ] } derive-with = "0.5" +anyhow = "1.0" thiserror = "1.0" pike = "0.1" diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index a1d6f6d0..40fbe9af 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -50,6 +50,7 @@ actix-rt = { workspace = true } # boilerplate derive_more = { workspace = true } derive-with = { workspace = true } +anyhow = { workspace = true } thiserror = { workspace = true } # Webhooks diff --git a/crates/cli/src/init.rs b/crates/cli/src/init.rs index 4dfe4b60..fde940b9 100644 --- a/crates/cli/src/init.rs +++ b/crates/cli/src/init.rs @@ -1,29 +1,18 @@ // +use anyhow::{Context, Result}; use kxio::fs::FileSystem; -pub fn run(fs: FileSystem) { - let file_name = ".git-next.toml"; - let pathbuf = fs.base().join(file_name); - match fs.path_exists(&pathbuf) { - Ok(exists) => { - if exists { - eprintln!( - "The configuration file already exists at {} - not overwritting it.", - file_name - ); - } else { - match fs.file_write(&pathbuf, include_str!("../default.toml")) { - Ok(_) => { - println!("Created a default configuration file at {}", file_name); - } - Err(e) => { - eprintln!("Failed to write to the configuration file: {}", e) - } - } - } - } - Err(err) => { - eprintln!("Could not check if file exist: {} - {err:?}", file_name); - } +pub fn run(fs: FileSystem) -> Result<()> { + let pathbuf = fs.base().join(".git-next.toml"); + if fs + .path_exists(&pathbuf) + .with_context(|| format!("Checking for existing file: {pathbuf:?}"))? + { + eprintln!("The configuration file already exists at {pathbuf:?} - not overwritting it.",); + } else { + fs.file_write(&pathbuf, include_str!("../default.toml")) + .with_context(|| format!("Writing file: {pathbuf:?}"))?; + println!("Created a default configuration file at {pathbuf:?}"); } + Ok(()) } diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index bd9de7e8..15295b05 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -13,6 +13,7 @@ use git_next_core::git; use std::path::PathBuf; +use anyhow::Result; use clap::Parser; use kxio::{fs, network::Network}; @@ -34,7 +35,7 @@ enum Server { Start, } -fn main() { +fn main() -> Result<()> { let fs = fs::new(PathBuf::default()); let net = Network::new_real(); let repository_factory = git::repository::factory::real(); @@ -42,11 +43,11 @@ fn main() { match commands.command { Command::Init => { - init::run(fs); + init::run(fs)?; } Command::Server(server) => match server { Server::Init => { - server::init(fs); + server::init(fs)?; } Server::Start => { let sleep_duration = std::time::Duration::from_secs(10); @@ -54,4 +55,5 @@ fn main() { } }, } + Ok(()) } diff --git a/crates/cli/src/server/mod.rs b/crates/cli/src/server/mod.rs index 24ef09f9..3d9f0f81 100644 --- a/crates/cli/src/server/mod.rs +++ b/crates/cli/src/server/mod.rs @@ -10,31 +10,26 @@ use crate::file_watcher::{watch_file, FileUpdated}; use actor::ServerActor; use git_next_core::git::RepositoryFactory; +use anyhow::{Context, Result}; use kxio::{fs::FileSystem, network::Network}; use tracing::info; use std::path::PathBuf; -pub fn init(fs: FileSystem) { +pub fn init(fs: FileSystem) -> Result<()> { let file_name = "git-next-server.toml"; 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 - ); + if fs + .path_exists(&pathbuf) + .with_context(|| format!("Checking for existing file: {pathbuf:?}"))? + { + eprintln!("The configuration file already exists at {pathbuf:?} - not overwritting it.",); } else { - 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) - } - } + fs.file_write(&pathbuf, include_str!("server-default.toml")) + .with_context(|| format!("Writing file: {pathbuf:?}"))?; + println!("Created a default configuration file at {pathbuf:?}",); } + Ok(()) } pub fn start( diff --git a/crates/cli/src/tests.rs b/crates/cli/src/tests.rs index e5ab069f..88c4f8b4 100644 --- a/crates/cli/src/tests.rs +++ b/crates/cli/src/tests.rs @@ -8,7 +8,7 @@ mod init { let file = fs.base().join(".git-next.toml"); fs.file_write(&file, "contents")?; - crate::init::run(fs.clone()); + crate::init::run(fs.clone())?; assert_eq!( fs.file_read_to_string(&file)?, @@ -23,7 +23,7 @@ mod init { fn should_create_default_file_if_not_exists() -> TestResult { let fs = kxio::fs::temp()?; - crate::init::run(fs.clone()); + crate::init::run(fs.clone())?; let file = fs.base().join(".git-next.toml");