Albatross/src/main.rs

132 lines
4.0 KiB
Rust

use std::path::PathBuf;
use structopt::StructOpt;
mod backup;
mod chunk_coordinate;
mod config;
mod discord;
mod error;
mod region;
mod remote;
mod restore;
use crate::backup::{convert_backup_to_sp, do_backup};
use crate::chunk_coordinate::ChunkCoordinate;
use crate::config::AlbatrossConfig;
use crate::restore::{restore_chunk_from_backup, restore_range_from_backup};
#[derive(Debug, StructOpt)]
#[structopt(about = "Backup your Minecraft Server!")]
struct Albatross {
/// Path to the Albatross config
#[structopt(short, long, env = "ALBATROSS_CONFIG", parse(from_os_str))]
config_path: PathBuf,
/// Subcommand
#[structopt(subcommand)]
sub_command: SubCommand,
}
#[derive(Debug, StructOpt)]
enum SubCommand {
/// Backup a server
Backup {
/// Output path
#[structopt(short = "o", long = "ouptut", parse(from_os_str))]
output: Option<PathBuf>,
},
/// Export a backup as a single player world
Export {
/// Backup to convert
#[structopt(parse(from_os_str))]
input_backup: PathBuf,
/// Output location override
#[structopt(parse(from_os_str))]
output: PathBuf,
},
/// Restore certain chunks from a backup
Restore {
/// Output server directory
#[structopt(short, long, parse(from_os_str))]
server_directory: Option<PathBuf>,
/// World to restore
world_name: String,
/// Backup to restore from
#[structopt(parse(from_os_str))]
backup_path: PathBuf,
/// Chunk to backup
chunk: ChunkCoordinate,
/// Upper chunk range bound
#[structopt(short, long)]
upper_bound: Option<ChunkCoordinate>,
},
}
fn main() {
let opt = Albatross::from_args();
let cfg = AlbatrossConfig::new(opt.config_path.into_os_string().to_str().unwrap())
.expect("Config error");
if cfg.world_config.is_some() {
match opt.sub_command {
SubCommand::Backup { output } => {
println!("Starting backup");
match do_backup(cfg, output) {
Ok(_) => println!("Backup complete!"),
Err(e) => println!("Error doing backup: {:?}", e),
};
}
SubCommand::Export {
input_backup,
output,
} => {
println!("Starting export");
match convert_backup_to_sp(&cfg, &input_backup, &output) {
Ok(_) => println!("Export complete!"),
Err(e) => println!("Error exporting backup: {:?}", e),
};
}
SubCommand::Restore {
server_directory,
world_name,
backup_path,
chunk,
upper_bound,
} => {
println!("Starting restore");
let server_directory = match server_directory {
Some(dir) => dir,
None => cfg.backup.minecraft_dir,
};
if let Some(upper_bound) = upper_bound {
match restore_range_from_backup(
world_name.as_str(),
chunk,
upper_bound,
&backup_path,
&server_directory,
) {
Ok(count) => println!("Restored {} chunks!", count),
Err(e) => println!("Error restoring backup: {:?}", e),
};
} else {
match restore_chunk_from_backup(
world_name.as_str(),
chunk,
&backup_path,
&server_directory,
) {
Ok(_) => println!("Restored chunk!"),
Err(e) => println!("Error restoring backup: {:?}", e),
};
}
}
}
} else {
println!("No worlds specified in config file!")
}
}