diff --git a/CHANGELOG.md b/CHANGELOG.md index 553fd69..ebf8a14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Extract command for working with the output of export * Arch packages are now published to my bur repo -* Allow passing configuration variables from TOML file ### Changed diff --git a/Cargo.lock b/Cargo.lock index 1854dec..16ec2ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,7 +14,6 @@ version = "0.3.1" dependencies = [ "chrono", "clap", - "figment", "flate2", "serde", "serde_json", @@ -86,12 +85,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "atomic" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c59bdb34bc650a32731b31bd8f0829cc15d24a708ee31559e0bb34f2bc320cba" - [[package]] name = "autocfg" version = "1.1.0" @@ -201,12 +194,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "equivalent" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" - [[package]] name = "errno" version = "0.3.1" @@ -228,20 +215,6 @@ dependencies = [ "libc", ] -[[package]] -name = "figment" -version = "0.10.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4547e226f4c9ab860571e070a9034192b3175580ecea38da34fcdb53a018c9a5" -dependencies = [ - "atomic", - "pear", - "serde", - "toml", - "uncased", - "version_check", -] - [[package]] name = "filetime" version = "0.2.21" @@ -264,12 +237,6 @@ dependencies = [ "miniz_oxide", ] -[[package]] -name = "hashbrown" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" - [[package]] name = "heck" version = "0.4.1" @@ -305,22 +272,6 @@ dependencies = [ "cc", ] -[[package]] -name = "indexmap" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" -dependencies = [ - "equivalent", - "hashbrown", -] - -[[package]] -name = "inlinable_string" -version = "0.1.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb" - [[package]] name = "io-lifetimes" version = "1.0.11" @@ -377,12 +328,6 @@ version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de" -[[package]] -name = "memchr" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" - [[package]] name = "miniz_oxide" version = "0.7.1" @@ -407,29 +352,6 @@ version = "1.17.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9670a07f94779e00908f3e686eab508878ebb390ba6e604d3a284c00e8d0487b" -[[package]] -name = "pear" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61a386cd715229d399604b50d1361683fe687066f42d56f54be995bc6868f71c" -dependencies = [ - "inlinable_string", - "pear_codegen", - "yansi", -] - -[[package]] -name = "pear_codegen" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f0f13dac8069c139e8300a6510e3f4143ecf5259c60b116a9b271b4ca0d54" -dependencies = [ - "proc-macro2", - "proc-macro2-diagnostics", - "quote", - "syn", -] - [[package]] name = "proc-macro2" version = "1.0.59" @@ -439,19 +361,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "proc-macro2-diagnostics" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "version_check", - "yansi", -] - [[package]] name = "quote" version = "1.0.28" @@ -521,15 +430,6 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_spanned" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" -dependencies = [ - "serde", -] - [[package]] name = "signal-hook" version = "0.3.15" @@ -588,49 +488,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "toml" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17e963a819c331dcacd7ab957d80bc2b9a9c1e71c804826d2f283dd65306542" -dependencies = [ - "serde", - "serde_spanned", - "toml_datetime", - "toml_edit", -] - -[[package]] -name = "toml_datetime" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" -dependencies = [ - "serde", -] - -[[package]] -name = "toml_edit" -version = "0.19.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a" -dependencies = [ - "indexmap", - "serde", - "serde_spanned", - "toml_datetime", - "winnow", -] - -[[package]] -name = "uncased" -version = "0.9.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b9bc53168a4be7402ab86c3aad243a84dd7381d09be0eddc81280c1da95ca68" -dependencies = [ - "version_check", -] - [[package]] name = "unicode-ident" version = "1.0.9" @@ -643,12 +500,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" @@ -806,15 +657,6 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" -[[package]] -name = "winnow" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19f495880723d0999eb3500a9064d8dbcf836460b24c17df80ea7b5794053aac" -dependencies = [ - "memchr", -] - [[package]] name = "xattr" version = "0.2.3" @@ -823,9 +665,3 @@ checksum = "6d1526bbe5aaeb5eb06885f4d987bcdfa5e23187055de9b83fe00156a821fabc" dependencies = [ "libc", ] - -[[package]] -name = "yansi" -version = "1.0.0-rc" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ee746ad3851dd3bc40e4a028ab3b00b99278d929e48957bcb2d111874a7e43e" diff --git a/Cargo.toml b/Cargo.toml index 09a8b1b..01d0593 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,8 @@ flate2 = "1.0.26" chrono = { version = "0.4.26", features = ["serde"] } clap = { version = "4.3.1", features = ["derive", "env"] } signal-hook = "0.3.15" -serde = { version = "1.0.164", features = ["derive"] } +serde = { version = "1.0.164", features = ["derive", "rc"] } serde_json = "1.0.96" -figment = { version = "0.10.10", features = ["env", "toml"] } [profile.release] lto = "fat" diff --git a/alex-example.toml b/alex-example.toml deleted file mode 100644 index 234a7bf..0000000 --- a/alex-example.toml +++ /dev/null @@ -1,16 +0,0 @@ -config = "data/config" -world = "data/worlds" -backup = "data/backups" -server = "Paper" - -# [[layers]] -# name = "2min" -# frequency = 2 -# chains = 4 -# chain_len = 4 - -# [[layers]] -# name = "3min" -# frequency = 3 -# chains = 2 -# chain_len = 2 diff --git a/src/backup/delta.rs b/src/backup/delta.rs index 1260b43..21f626c 100644 --- a/src/backup/delta.rs +++ b/src/backup/delta.rs @@ -1,8 +1,7 @@ -use std::{borrow::Borrow, fmt}; - -use serde::{Deserialize, Serialize}; - use super::State; +use serde::{Deserialize, Serialize}; +use std::borrow::Borrow; +use std::fmt; /// Represents the changes relative to the previous backup #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/src/backup/manager/config.rs b/src/backup/manager/config.rs index a07be56..8adbd5a 100644 --- a/src/backup/manager/config.rs +++ b/src/backup/manager/config.rs @@ -1,8 +1,8 @@ -use std::{error::Error, fmt, str::FromStr}; +use std::error::Error; +use std::fmt; +use std::str::FromStr; -use serde::{Deserialize, Serialize}; - -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug)] pub struct ManagerConfig { pub name: String, pub frequency: u32, diff --git a/src/backup/manager/meta.rs b/src/backup/manager/meta.rs index a649764..4831f3f 100644 --- a/src/backup/manager/meta.rs +++ b/src/backup/manager/meta.rs @@ -1,13 +1,10 @@ -use std::{ - collections::HashMap, - io, - path::{Path, PathBuf}, -}; - -use chrono::Utc; -use serde::{Deserialize, Serialize}; - use super::{Manager, ManagerConfig}; +use chrono::Utc; +use serde::Deserialize; +use serde::Serialize; +use std::collections::HashMap; +use std::io; +use std::path::{Path, PathBuf}; /// Manages a collection of backup layers, allowing them to be utilized as a single object. pub struct MetaManager diff --git a/src/backup/manager/mod.rs b/src/backup/manager/mod.rs index ec91a3d..7cbd43c 100644 --- a/src/backup/manager/mod.rs +++ b/src/backup/manager/mod.rs @@ -1,20 +1,20 @@ mod config; mod meta; -use std::{ - fs::{File, OpenOptions}, - io, - path::{Path, PathBuf}, -}; - -use chrono::{SubsecRound, Utc}; -use flate2::{write::GzEncoder, Compression}; -use serde::{Deserialize, Serialize}; +pub use config::ManagerConfig; +pub use meta::MetaManager; use super::{Backup, BackupType, Delta, State}; use crate::other; -pub use config::ManagerConfig; -pub use meta::MetaManager; +use chrono::SubsecRound; +use chrono::Utc; +use flate2::write::GzEncoder; +use flate2::Compression; +use serde::Deserialize; +use serde::Serialize; +use std::fs::{File, OpenOptions}; +use std::io; +use std::path::{Path, PathBuf}; /// Manages a single backup layer consisting of one or more chains of backups. pub struct Manager diff --git a/src/backup/mod.rs b/src/backup/mod.rs index c833b55..6bd798f 100644 --- a/src/backup/mod.rs +++ b/src/backup/mod.rs @@ -4,25 +4,24 @@ pub mod manager; mod path; mod state; -use std::{ - collections::HashSet, - fmt, - fs::File, - io, - path::{Path, PathBuf}, -}; - -use chrono::Utc; -use flate2::{read::GzDecoder, write::GzEncoder, Compression}; -use serde::{Deserialize, Serialize}; - use delta::Delta; pub use manager::Manager; pub use manager::ManagerConfig; pub use manager::MetaManager; -use path::PathExt; pub use state::State; +use chrono::Utc; +use flate2::read::GzDecoder; +use flate2::write::GzEncoder; +use flate2::Compression; +use path::PathExt; +use serde::{Deserialize, Serialize}; +use std::collections::HashSet; +use std::fmt; +use std::fs::File; +use std::io; +use std::path::{Path, PathBuf}; + const BYTE_SUFFIXES: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"]; #[derive(Debug, PartialEq, Serialize, Deserialize)] diff --git a/src/backup/path.rs b/src/backup/path.rs index d8c2cec..77c6883 100644 --- a/src/backup/path.rs +++ b/src/backup/path.rs @@ -1,12 +1,9 @@ -use std::{ - collections::HashSet, - ffi::OsString, - fs::{self, DirEntry}, - io, - path::{Path, PathBuf}, -}; - use chrono::{Local, Utc}; +use std::collections::HashSet; +use std::ffi::OsString; +use std::fs::DirEntry; +use std::path::{Path, PathBuf}; +use std::{fs, io}; pub struct ReadDirRecursive { ignored: HashSet, diff --git a/src/backup/state.rs b/src/backup/state.rs index 1f81abb..de4b2c0 100644 --- a/src/backup/state.rs +++ b/src/backup/state.rs @@ -1,13 +1,9 @@ -use std::{ - borrow::Borrow, - collections::{HashMap, HashSet}, - ops::{Deref, DerefMut}, - path::{Path, PathBuf}, -}; - -use serde::{Deserialize, Serialize}; - use crate::backup::Delta; +use serde::{Deserialize, Serialize}; +use std::borrow::Borrow; +use std::collections::{HashMap, HashSet}; +use std::ops::{Deref, DerefMut}; +use std::path::{Path, PathBuf}; /// Struct that represents a current state for a backup. This struct acts as a smart pointer around /// a HashMap. diff --git a/src/cli/backup.rs b/src/cli/backup.rs index 548facc..6810e42 100644 --- a/src/cli/backup.rs +++ b/src/cli/backup.rs @@ -1,10 +1,10 @@ -use std::io; -use std::path::{Path, PathBuf}; - +use crate::backup::Backup; +use crate::cli::Cli; +use crate::other; use chrono::{TimeZone, Utc}; use clap::{Args, Subcommand}; - -use crate::{backup::Backup, other}; +use std::io; +use std::path::{Path, PathBuf}; #[derive(Subcommand)] pub enum BackupCommands { @@ -103,7 +103,7 @@ pub struct BackupExtractArgs { } impl BackupArgs { - pub fn run(&self, cli: &super::Config) -> io::Result<()> { + pub fn run(&self, cli: &Cli) -> io::Result<()> { match &self.command { BackupCommands::Create(args) => args.run(cli), BackupCommands::List(args) => args.run(cli), @@ -115,7 +115,7 @@ impl BackupArgs { } impl BackupCreateArgs { - pub fn run(&self, cli: &super::Config) -> io::Result<()> { + pub fn run(&self, cli: &Cli) -> io::Result<()> { let mut meta = cli.meta()?; if let Some(res) = meta.create_backup(&self.layer) { @@ -127,7 +127,7 @@ impl BackupCreateArgs { } impl BackupListArgs { - pub fn run(&self, cli: &super::Config) -> io::Result<()> { + pub fn run(&self, cli: &Cli) -> io::Result<()> { let meta = cli.meta()?; // A bit scuffed? Sure @@ -184,7 +184,7 @@ fn parse_backup_path( } impl BackupRestoreArgs { - pub fn run(&self, cli: &super::Config) -> io::Result<()> { + pub fn run(&self, cli: &Cli) -> io::Result<()> { let backup_dir = cli.backup.canonicalize()?; // Create directories if needed @@ -237,7 +237,7 @@ impl BackupRestoreArgs { } impl BackupExportArgs { - pub fn run(&self, cli: &super::Config) -> io::Result<()> { + pub fn run(&self, cli: &Cli) -> io::Result<()> { let backup_dir = cli.backup.canonicalize()?; if self.make { @@ -261,7 +261,7 @@ impl BackupExportArgs { } impl BackupExtractArgs { - pub fn run(&self, _cli: &super::Config) -> io::Result<()> { + pub fn run(&self, _cli: &Cli) -> io::Result<()> { // Create directories if needed if self.make { std::fs::create_dir_all(&self.output_config)?; diff --git a/src/cli/config.rs b/src/cli/config.rs deleted file mode 100644 index da80269..0000000 --- a/src/cli/config.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::{io, path::PathBuf}; - -use serde::{Deserialize, Serialize}; - -use crate::{ - backup::{ManagerConfig, MetaManager}, - server::{Metadata, ServerType}, -}; - -#[derive(Serialize, Deserialize, Debug)] -pub struct Config { - pub config: PathBuf, - pub world: PathBuf, - pub backup: PathBuf, - pub layers: Vec, - pub server: ServerType, - pub server_version: String, -} - -impl Default for Config { - fn default() -> Self { - Self { - config: PathBuf::from("."), - world: PathBuf::from("../worlds"), - backup: PathBuf::from("../backups"), - layers: Vec::new(), - server: ServerType::Unknown, - server_version: String::from(""), - } - } -} - -impl Config { - /// Convenience method to initialize backup manager from the cli arguments - pub fn meta(&self) -> io::Result> { - let metadata = Metadata { - server_type: self.server, - server_version: self.server_version.clone(), - }; - let dirs = vec![ - (PathBuf::from("config"), self.config.canonicalize()?), - (PathBuf::from("worlds"), self.world.canonicalize()?), - ]; - let mut meta = MetaManager::new(self.backup.canonicalize()?, dirs, metadata); - meta.add_all(&self.layers)?; - - Ok(meta) - } -} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 4eaf6f8..0594e8e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,102 +1,92 @@ mod backup; -mod config; mod run; +pub use crate::backup::MetaManager; +pub use crate::server::Metadata; +pub use backup::{BackupArgs, BackupCommands}; +pub use run::RunArgs; + +use crate::backup::ManagerConfig; +use crate::server::ServerType; +use clap::{Parser, Subcommand}; +use std::io; use std::path::PathBuf; -use clap::{Args, Parser, Subcommand}; -use figment::{ - providers::{Env, Format, Serialized, Toml}, - Figment, -}; -use serde::{Deserialize, Serialize}; - -use crate::{backup::ManagerConfig, server::ServerType}; -use backup::BackupArgs; -use config::Config; -use run::RunCli; - -#[derive(Parser, Serialize)] -#[command(author, version, about, long_about = None)] -pub struct Cli { - #[command(subcommand)] - #[serde(skip)] - pub command: Commands, - - /// Path to a TOML configuration file - #[arg(long = "config-file", global = true)] - pub config_file: Option, - - #[command(flatten)] - pub args: CliArgs, -} - -#[derive(Args, Serialize, Deserialize, Clone)] -pub struct CliArgs { - /// Directory where configs are stored, and where the server will run - #[arg(long, value_name = "CONFIG_DIR", global = true)] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub config: Option, - - /// Directory where world files will be saved - #[arg(long, value_name = "WORLD_DIR", global = true)] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub world: Option, - - /// Directory where backups will be stored - #[arg(long, value_name = "BACKUP_DIR", global = true)] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub backup: Option, - - /// What backup layers to employ, provided as a list of tuples name,frequency,chains,chain_len - /// delimited by semicolons (;). - #[arg(long, global = true, value_delimiter = ';')] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub layers: Option>, - - /// Type of server - #[arg(long, global = true)] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub server: Option, - - /// Version string for the server, e.g. 1.19.4-545 - #[arg(long, global = true)] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub server_version: Option, -} - #[derive(Subcommand)] pub enum Commands { /// Run the server - Run(RunCli), + Run(RunArgs), /// Interact with the backup system without starting a server Backup(BackupArgs), } -impl Cli { - pub fn run(&self) -> crate::Result<()> { - let config = self.config(&self.args)?; +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +pub struct Cli { + #[command(subcommand)] + pub command: Commands, + /// Directory where configs are stored, and where the server will run + #[arg( + long, + value_name = "CONFIG_DIR", + default_value = ".", + env = "ALEX_CONFIG_DIR", + global = true + )] + pub config: PathBuf, + /// Directory where world files will be saved + #[arg( + long, + value_name = "WORLD_DIR", + default_value = "../worlds", + env = "ALEX_WORLD_DIR", + global = true + )] + pub world: PathBuf, + /// Directory where backups will be stored + #[arg( + long, + value_name = "BACKUP_DIR", + default_value = "../backups", + env = "ALEX_BACKUP_DIR", + global = true + )] + pub backup: PathBuf, + /// What backup layers to employ, provided as a list of tuples name,frequency,chains,chain_len + /// delimited by semicolons (;). + #[arg(long, env = "ALEX_LAYERS", global = true, value_delimiter = ';')] + pub layers: Vec, + + /// Type of server + #[arg(long, default_value = "unknown", env = "ALEX_SERVER", global = true)] + pub server: ServerType, + /// Version string for the server, e.g. 1.19.4-545 + #[arg(long, default_value = "", env = "ALEX_SERVER_VERSION", global = true)] + pub server_version: String, +} + +impl Cli { + pub fn run(&self) -> io::Result<()> { match &self.command { - Commands::Run(args) => args.run(self, &config), - Commands::Backup(args) => Ok(args.run(&config)?), + Commands::Run(args) => args.run(self), + Commands::Backup(args) => args.run(self), } } - pub fn config(&self, args: &U) -> Result - where - T: Default + Serialize + for<'de> Deserialize<'de>, - U: Serialize, - { - let toml_file = self - .config_file - .clone() - .unwrap_or(PathBuf::from(Env::var_or("ALEX_CONFIG_FILE", ""))); - Figment::new() - .merge(Serialized::defaults(T::default())) - .merge(Toml::file(toml_file)) - .merge(Env::prefixed("ALEX_")) - .merge(Serialized::defaults(args)) - .extract() + /// Convenience method to initialize backup manager from the cli arguments + pub fn meta(&self) -> io::Result> { + let metadata = Metadata { + server_type: self.server, + server_version: self.server_version.clone(), + }; + let dirs = vec![ + (PathBuf::from("config"), self.config.canonicalize()?), + (PathBuf::from("worlds"), self.world.canonicalize()?), + ]; + let mut meta = MetaManager::new(self.backup.canonicalize()?, dirs, metadata); + meta.add_all(&self.layers)?; + + Ok(meta) } } diff --git a/src/cli/run/mod.rs b/src/cli/run.rs similarity index 52% rename from src/cli/run/mod.rs rename to src/cli/run.rs index 21c6a0a..986fe8a 100644 --- a/src/cli/run/mod.rs +++ b/src/cli/run.rs @@ -1,20 +1,33 @@ -mod config; - -use std::{ - path::PathBuf, - sync::{Arc, Mutex}, -}; - +use crate::cli::Cli; +use crate::server; +use crate::signals; +use crate::stdin; use clap::Args; -use serde::{Deserialize, Serialize}; - -use crate::{server, signals, stdin}; -use config::Config; +use std::io; +use std::path::PathBuf; +use std::sync::{Arc, Mutex}; #[derive(Args)] -pub struct RunCli { - #[command(flatten)] - pub args: RunArgs, +pub struct RunArgs { + /// Server jar to execute + #[arg( + long, + value_name = "JAR_PATH", + default_value = "server.jar", + env = "ALEX_JAR" + )] + pub jar: PathBuf, + + /// Java command to run the server jar with + #[arg(long, value_name = "JAVA_CMD", default_value_t = String::from("java"), env = "ALEX_JAVA")] + pub java: String, + + /// XMS value in megabytes for the server instance + #[arg(long, default_value_t = 1024, env = "ALEX_XMS")] + pub xms: u64, + /// XMX value in megabytes for the server instance + #[arg(long, default_value_t = 2048, env = "ALEX_XMX")] + pub xmx: u64, /// Don't actually run the server, but simply output the server configuration that would have /// been ran @@ -22,29 +35,6 @@ pub struct RunCli { pub dry: bool, } -#[derive(Args, Serialize, Deserialize, Clone)] -pub struct RunArgs { - /// Server jar to execute - #[arg(long, value_name = "JAR_PATH")] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub jar: Option, - - /// Java command to run the server jar with - #[arg(long, value_name = "JAVA_CMD")] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub java: Option, - - /// XMS value in megabytes for the server instance - #[arg(long)] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub xms: Option, - - /// XMX value in megabytes for the server instance - #[arg(long)] - #[serde(skip_serializing_if = "::std::option::Option::is_none")] - pub xmx: Option, -} - fn backups_thread(counter: Arc>) { loop { let next_scheduled_time = { @@ -66,22 +56,19 @@ fn backups_thread(counter: Arc>) { } } -impl RunCli { - pub fn run(&self, cli: &super::Cli, global: &super::Config) -> crate::Result<()> { - let config: Config = cli.config(&self.args)?; - println!("{:?}", config); - +impl RunArgs { + pub fn run(&self, cli: &Cli) -> io::Result<()> { let (_, mut signals) = signals::install_signal_handlers()?; - let mut cmd = server::ServerCommand::new(global.server, &global.server_version) - .java(&config.java) - .jar(config.jar.clone()) - .config(global.config.clone()) - .world(global.world.clone()) - .backup(global.backup.clone()) - .managers(global.layers.clone()) - .xms(config.xms) - .xmx(config.xmx); + let mut cmd = server::ServerCommand::new(cli.server, &cli.server_version) + .java(&self.java) + .jar(self.jar.clone()) + .config(cli.config.clone()) + .world(cli.world.clone()) + .backup(cli.backup.clone()) + .managers(cli.layers.clone()) + .xms(self.xms) + .xmx(self.xmx); cmd.canonicalize()?; if self.dry { @@ -92,7 +79,7 @@ impl RunCli { let counter = Arc::new(Mutex::new(cmd.spawn()?)); - if !global.layers.is_empty() { + if !cli.layers.is_empty() { let clone = Arc::clone(&counter); std::thread::spawn(move || backups_thread(clone)); } @@ -102,6 +89,6 @@ impl RunCli { std::thread::spawn(move || stdin::handle_stdin(clone)); // Signal handler loop exits the process when necessary - Ok(signals::handle_signals(&mut signals, counter)?) + signals::handle_signals(&mut signals, counter) } } diff --git a/src/cli/run/config.rs b/src/cli/run/config.rs deleted file mode 100644 index 5783451..0000000 --- a/src/cli/run/config.rs +++ /dev/null @@ -1,22 +0,0 @@ -use std::path::PathBuf; - -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize, Debug)] -pub struct Config { - pub jar: PathBuf, - pub java: String, - pub xms: u64, - pub xmx: u64, -} - -impl Default for Config { - fn default() -> Self { - Self { - jar: PathBuf::from("server.jar"), - java: String::from("java"), - xms: 1024, - xmx: 2048, - } - } -} diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index 0ea7532..0000000 --- a/src/error.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::{fmt, io}; - -pub type Result = std::result::Result; - -#[derive(Debug)] -pub enum Error { - IO(io::Error), - Figment(figment::Error), -} - -impl fmt::Display for Error { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::IO(err) => write!(fmt, "{}", err), - Error::Figment(err) => write!(fmt, "{}", err), - } - } -} - -impl std::error::Error for Error {} - -impl From for Error { - fn from(err: io::Error) -> Self { - Error::IO(err) - } -} - -impl From for Error { - fn from(err: figment::Error) -> Self { - Error::Figment(err) - } -} diff --git a/src/main.rs b/src/main.rs index 245a796..f357d1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,12 @@ mod backup; mod cli; -mod error; mod server; mod signals; mod stdin; -use std::io; - -use clap::Parser; - use crate::cli::Cli; -pub use error::{Error, Result}; +use clap::Parser; +use std::io; pub fn other(msg: &str) -> io::Error { io::Error::new(io::ErrorKind::Other, msg) @@ -36,8 +32,7 @@ pub fn other(msg: &str) -> io::Error { // // manager.remove_old_backups() // } -fn main() -> crate::Result<()> { +fn main() -> io::Result<()> { let cli = Cli::parse(); - cli.run() } diff --git a/src/signals.rs b/src/signals.rs index 5462a75..61c38f9 100644 --- a/src/signals.rs +++ b/src/signals.rs @@ -1,13 +1,10 @@ -use std::{ - io, - sync::{atomic::AtomicBool, Arc, Mutex}, -}; +use std::io; +use std::sync::atomic::AtomicBool; +use std::sync::{Arc, Mutex}; -use signal_hook::{ - consts::TERM_SIGNALS, - flag, - iterator::{Signals, SignalsInfo}, -}; +use signal_hook::consts::TERM_SIGNALS; +use signal_hook::flag; +use signal_hook::iterator::{Signals, SignalsInfo}; use crate::server; diff --git a/src/stdin.rs b/src/stdin.rs index 4a7f761..f9a22e8 100644 --- a/src/stdin.rs +++ b/src/stdin.rs @@ -1,7 +1,5 @@ -use std::{ - io, - sync::{Arc, Mutex}, -}; +use std::io; +use std::sync::{Arc, Mutex}; use crate::server;