feat: return better errors to the user on init

This commit is contained in:
Paul Campbell 2024-07-30 10:52:05 +01:00
parent 03ae9153b4
commit 9a9c73d929
6 changed files with 33 additions and 45 deletions

View file

@ -80,6 +80,7 @@ derive_more = { version = "1.0.0-beta.6", features = [
"from", "from",
] } ] }
derive-with = "0.5" derive-with = "0.5"
anyhow = "1.0"
thiserror = "1.0" thiserror = "1.0"
pike = "0.1" pike = "0.1"

View file

@ -50,6 +50,7 @@ actix-rt = { workspace = true }
# boilerplate # boilerplate
derive_more = { workspace = true } derive_more = { workspace = true }
derive-with = { workspace = true } derive-with = { workspace = true }
anyhow = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
# Webhooks # Webhooks

View file

@ -1,29 +1,18 @@
// //
use anyhow::{Context, Result};
use kxio::fs::FileSystem; use kxio::fs::FileSystem;
pub fn run(fs: FileSystem) { pub fn run(fs: FileSystem) -> Result<()> {
let file_name = ".git-next.toml"; let pathbuf = fs.base().join(".git-next.toml");
let pathbuf = fs.base().join(file_name); if fs
match fs.path_exists(&pathbuf) { .path_exists(&pathbuf)
Ok(exists) => { .with_context(|| format!("Checking for existing file: {pathbuf:?}"))?
if exists { {
eprintln!( eprintln!("The configuration file already exists at {pathbuf:?} - not overwritting it.",);
"The configuration file already exists at {} - not overwritting it.", } else {
file_name fs.file_write(&pathbuf, include_str!("../default.toml"))
); .with_context(|| format!("Writing file: {pathbuf:?}"))?;
} else { println!("Created a default configuration file at {pathbuf:?}");
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);
}
} }
Ok(())
} }

View file

@ -13,6 +13,7 @@ use git_next_core::git;
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::Result;
use clap::Parser; use clap::Parser;
use kxio::{fs, network::Network}; use kxio::{fs, network::Network};
@ -34,7 +35,7 @@ enum Server {
Start, Start,
} }
fn main() { fn main() -> Result<()> {
let fs = fs::new(PathBuf::default()); let fs = fs::new(PathBuf::default());
let net = Network::new_real(); let net = Network::new_real();
let repository_factory = git::repository::factory::real(); let repository_factory = git::repository::factory::real();
@ -42,11 +43,11 @@ fn main() {
match commands.command { match commands.command {
Command::Init => { Command::Init => {
init::run(fs); init::run(fs)?;
} }
Command::Server(server) => match server { Command::Server(server) => match server {
Server::Init => { Server::Init => {
server::init(fs); server::init(fs)?;
} }
Server::Start => { Server::Start => {
let sleep_duration = std::time::Duration::from_secs(10); let sleep_duration = std::time::Duration::from_secs(10);
@ -54,4 +55,5 @@ fn main() {
} }
}, },
} }
Ok(())
} }

View file

@ -10,31 +10,26 @@ use crate::file_watcher::{watch_file, FileUpdated};
use actor::ServerActor; use actor::ServerActor;
use git_next_core::git::RepositoryFactory; use git_next_core::git::RepositoryFactory;
use anyhow::{Context, Result};
use kxio::{fs::FileSystem, network::Network}; use kxio::{fs::FileSystem, network::Network};
use tracing::info; use tracing::info;
use std::path::PathBuf; use std::path::PathBuf;
pub fn init(fs: FileSystem) { pub fn init(fs: FileSystem) -> Result<()> {
let file_name = "git-next-server.toml"; let file_name = "git-next-server.toml";
let pathbuf = PathBuf::from(file_name); let pathbuf = PathBuf::from(file_name);
let Ok(exists) = fs.path_exists(&pathbuf) else { if fs
eprintln!("Could not check if file exist: {}", file_name); .path_exists(&pathbuf)
return; .with_context(|| format!("Checking for existing file: {pathbuf:?}"))?
}; {
if exists { eprintln!("The configuration file already exists at {pathbuf:?} - not overwritting it.",);
eprintln!(
"The configuration file already exists at {} - not overwritting it.",
file_name
);
} else { } else {
match fs.file_write(&pathbuf, include_str!("server-default.toml")) { fs.file_write(&pathbuf, include_str!("server-default.toml"))
Ok(_) => println!("Created a default configuration file at {}", file_name), .with_context(|| format!("Writing file: {pathbuf:?}"))?;
Err(e) => { println!("Created a default configuration file at {pathbuf:?}",);
eprintln!("Failed to write to the configuration file: {}", e)
}
}
} }
Ok(())
} }
pub fn start( pub fn start(

View file

@ -8,7 +8,7 @@ mod init {
let file = fs.base().join(".git-next.toml"); let file = fs.base().join(".git-next.toml");
fs.file_write(&file, "contents")?; fs.file_write(&file, "contents")?;
crate::init::run(fs.clone()); crate::init::run(fs.clone())?;
assert_eq!( assert_eq!(
fs.file_read_to_string(&file)?, fs.file_read_to_string(&file)?,
@ -23,7 +23,7 @@ mod init {
fn should_create_default_file_if_not_exists() -> TestResult { fn should_create_default_file_if_not_exists() -> TestResult {
let fs = kxio::fs::temp()?; let fs = kxio::fs::temp()?;
crate::init::run(fs.clone()); crate::init::run(fs.clone())?;
let file = fs.base().join(".git-next.toml"); let file = fs.base().join(".git-next.toml");