add command works ,fix warnings, proxy repaired, docker start arguments defined
This commit is contained in:
@ -1,27 +1,69 @@
|
||||
use clap::{ArgMatches, Command, arg};
|
||||
use crate::cli::env_settings;
|
||||
use crate::utils::validate_name;
|
||||
use crate::status::ServerStatus;
|
||||
use crate::utils::{ validate_name, extract_archive, move_all_elements};
|
||||
|
||||
use diesel::{insert_into, prelude::*};
|
||||
use crate::{DbPool, models};
|
||||
use crate::schema;
|
||||
|
||||
use tokio::{fs::{File, create_dir}, io::AsyncWriteExt};
|
||||
|
||||
use std::path::Path;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub fn create_subcommand() -> Command {
|
||||
let mut arguments = vec![
|
||||
arg!(<NAME> "Name of the server"),
|
||||
arg!([SERVER_ZIP] "initial server zip"),
|
||||
arg!([SERVER_ARCHIVE] "initial server archive (supported : prism zip)"),
|
||||
];
|
||||
|
||||
arguments.extend(env_settings::get_args());
|
||||
|
||||
Command::new("add")
|
||||
.about("add a server")
|
||||
.about("Add a server")
|
||||
.args(arguments.as_slice())
|
||||
}
|
||||
|
||||
|
||||
pub fn subcommand(arg: &ArgMatches) -> Result<(), String>{
|
||||
if !validate_name(arg.get_one::<String>("NAME").unwrap()) {
|
||||
pub async fn subcommand(arg: &ArgMatches, pool: DbPool) -> Result<(), String>{
|
||||
let config = crate::Config::load();
|
||||
let name = arg.get_one::<String>("NAME").unwrap();
|
||||
|
||||
|
||||
if !validate_name(name) {
|
||||
return Err("Invalid name provided".to_string());
|
||||
}
|
||||
|
||||
|
||||
println!("{}", env_settings::args_to_env(arg));
|
||||
let conn = &mut pool.get().unwrap();
|
||||
|
||||
if let Ok(_) = schema::servers::table.filter(schema::servers::name.eq(name)).select(models::Servers::as_select()).first(conn) {
|
||||
return Err("Server with this name already exists".to_string())
|
||||
}
|
||||
|
||||
let path = config.config_path + "/" + name + "/";
|
||||
|
||||
create_dir(&path).await.map_err(|e| format!("failed to create the config folder, reason : {}", e))?;
|
||||
|
||||
let mut file = File::create(path.to_string() + "env").await.map_err(|e| format!("failed to create .env file, reason {}", e))?;
|
||||
file.write_all(env_settings::args_to_env(arg).as_bytes()).await.map_err(|e| format!("Failed to write to env file, reason : {}", e))?;
|
||||
|
||||
create_dir(path.to_string() + "server").await.map_err(|e| format!("failed to create server subfolder, reason: {}", e))?;
|
||||
|
||||
if let Some(archive) = arg.get_one::<String>("SERVER_ARCHIVE") {
|
||||
extract_archive(archive.to_owned(), path.to_string() + "tmp").await?;
|
||||
move_all_elements(Path::new(&(path.to_string() + "tmp/minecraft")), Path::new(&(path.to_string() + "server"))).map_err(|e| format!("failed to move the elements, reason: {}", e))?;
|
||||
std::fs::remove_dir_all(path.to_string() + "tmp").map_err(|e| format!("failed to remove the tmp dir, reason : {}", e))?;
|
||||
}
|
||||
|
||||
let new_server = models::CreateServer {
|
||||
name: name,
|
||||
last_login: Some(SystemTime::now()),
|
||||
container_id: None,
|
||||
status: ServerStatus::Stopped,
|
||||
redirect_ip: None
|
||||
};
|
||||
|
||||
insert_into(schema::servers::dsl::servers).values(&new_server).execute(conn).map_err(|e| format!("Failed to insert in db, error : {:?}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user