feat: return better errors to the user on init
All checks were successful
Rust / build (push) Successful in 1m15s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

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",
] }
derive-with = "0.5"
anyhow = "1.0"
thiserror = "1.0"
pike = "0.1"

View file

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

View file

@ -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
);
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 {
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);
}
fs.file_write(&pathbuf, include_str!("../default.toml"))
.with_context(|| format!("Writing file: {pathbuf:?}"))?;
println!("Created a default configuration file at {pathbuf:?}");
}
Ok(())
}

View file

@ -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(())
}

View file

@ -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(

View file

@ -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");