feat: add --dry flag

incremental-backups
Jef Roosens 2023-06-13 12:49:31 +02:00
parent acb3cfd8e6
commit b08ba3853f
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 69 additions and 15 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://git.rustybever.be/Chewing_Bever/alex/src/branch/dev) ## [Unreleased](https://git.rustybever.be/Chewing_Bever/alex/src/branch/dev)
### Added
* `--dry` flag to inspect command that will be run
## [0.2.0](https://git.rustybever.be/Chewing_Bever/alex/src/tag/0.2.0) ## [0.2.0](https://git.rustybever.be/Chewing_Bever/alex/src/tag/0.2.0)
### Added ### Added

View File

@ -18,7 +18,7 @@ RUN cargo build && \
# We use ${:-} instead of a default value because the argument is always passed # We use ${:-} instead of a default value because the argument is always passed
# to the build, it'll just be blank most likely # to the build, it'll just be blank most likely
FROM eclipse-temurin:17-jre-alpine FROM eclipse-temurin:18-jre-alpine
# Build arguments # Build arguments
ARG MC_VERSION=1.19.4 ARG MC_VERSION=1.19.4

View File

@ -66,6 +66,11 @@ struct Cli {
/// How frequently to perform a backup, in minutes; 0 to disable. /// How frequently to perform a backup, in minutes; 0 to disable.
#[arg(short = 't', long, default_value_t = 0, env = "ALEX_FREQUENCY")] #[arg(short = 't', long, default_value_t = 0, env = "ALEX_FREQUENCY")]
frequency: u64, frequency: u64,
/// Don't actually run the server, but simply output the server configuration that would have
/// been ran
#[arg(short, long, default_value_t = false)]
dry: bool,
} }
fn backups_thread(counter: Arc<Mutex<server::ServerProcess>>, frequency: u64) { fn backups_thread(counter: Arc<Mutex<server::ServerProcess>>, frequency: u64) {
@ -85,7 +90,7 @@ fn main() -> io::Result<()> {
let (_, mut signals) = signals::install_signal_handlers()?; let (_, mut signals) = signals::install_signal_handlers()?;
let cli = Cli::parse(); let cli = Cli::parse();
let cmd = server::ServerCommand::new(cli.type_, &cli.server_version) let mut cmd = server::ServerCommand::new(cli.type_, &cli.server_version)
.java(&cli.java) .java(&cli.java)
.jar(cli.jar) .jar(cli.jar)
.config(cli.config) .config(cli.config)
@ -94,6 +99,14 @@ fn main() -> io::Result<()> {
.xms(cli.xms) .xms(cli.xms)
.xmx(cli.xmx) .xmx(cli.xmx)
.max_backups(cli.max_backups); .max_backups(cli.max_backups);
cmd.canonicalize()?;
if cli.dry {
print!("{}", cmd);
return Ok(());
}
let counter = Arc::new(Mutex::new(cmd.spawn()?)); let counter = Arc::new(Mutex::new(cmd.spawn()?));
if cli.frequency > 0 { if cli.frequency > 0 {

View File

@ -105,14 +105,19 @@ impl ServerCommand {
Ok(()) Ok(())
} }
pub fn spawn(self) -> std::io::Result<ServerProcess> { /// Canonicalize all paths to absolute paths. Without this command, all paths will be
/// interpreted relatively from the config directory.
pub fn canonicalize(&mut self) -> std::io::Result<()> {
// To avoid any issues, we use absolute paths for everything when spawning the process // To avoid any issues, we use absolute paths for everything when spawning the process
let jar = self.jar.canonicalize()?; self.jar = self.jar.canonicalize()?;
let config_dir = self.config_dir.canonicalize()?; self.config_dir = self.config_dir.canonicalize()?;
let world_dir = self.world_dir.canonicalize()?; self.world_dir = self.world_dir.canonicalize()?;
let backup_dir = self.backup_dir.canonicalize()?; self.backup_dir = self.backup_dir.canonicalize()?;
self.accept_eula()?; Ok(())
}
fn create_cmd(&self) -> std::process::Command {
let mut cmd = Command::new(&self.java); let mut cmd = Command::new(&self.java);
// Apply JVM optimisation flags // Apply JVM optimisation flags
@ -155,24 +160,56 @@ impl ServerCommand {
]); ]);
} }
cmd.current_dir(&config_dir) cmd.current_dir(&self.config_dir)
.arg("-jar") .arg("-jar")
.arg(&jar) .arg(&self.jar)
.arg("--universe") .arg("--universe")
.arg(&world_dir) .arg(&self.world_dir)
.arg("--nogui") .arg("--nogui")
.stdin(Stdio::piped()); .stdin(Stdio::piped());
cmd
}
pub fn spawn(&mut self) -> std::io::Result<ServerProcess> {
let mut cmd = self.create_cmd();
self.accept_eula()?;
let child = cmd.spawn()?; let child = cmd.spawn()?;
Ok(ServerProcess::new( Ok(ServerProcess::new(
self.type_, self.type_,
self.version, self.version.clone(),
config_dir, self.config_dir.clone(),
world_dir, self.world_dir.clone(),
backup_dir, self.backup_dir.clone(),
self.max_backups, self.max_backups,
child, child,
)) ))
} }
} }
impl fmt::Display for ServerCommand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let cmd = self.create_cmd();
write!(f, "Command: {}\n", self.java)?;
write!(f, "Working dir: {}\n", self.config_dir.as_path().display())?;
// Print command env vars
write!(f, "Environment:\n")?;
for (key, val) in cmd.get_envs().filter(|(_, v)| v.is_some()) {
let val = val.unwrap();
write!(f, " {}={}\n", key.to_string_lossy(), val.to_string_lossy())?;
}
// Print command arguments
write!(f, "Arguments:\n")?;
for arg in cmd.get_args() {
write!(f, " {}\n", arg.to_string_lossy())?;
}
Ok(())
}
}