add edit, ls and remove cli commands
This commit is contained in:
@ -1,6 +1,34 @@
|
||||
use clap::Command;
|
||||
use clap::{Arg, ArgMatches, Command};
|
||||
|
||||
use crate::{Config, DbPool};
|
||||
|
||||
use std::fs;
|
||||
|
||||
use diesel::prelude::*;
|
||||
|
||||
pub fn create_subcommand() -> Command {
|
||||
Command::new("remove")
|
||||
.about("remove a server")
|
||||
.arg(Arg::new("NAME").required(true))
|
||||
}
|
||||
|
||||
pub async fn subcommand(arguments: &ArgMatches, pool: DbPool) -> Result<(), String> {
|
||||
use crate::schema::servers::dsl::*;
|
||||
let server_name = arguments.get_one::<String>("NAME").unwrap();
|
||||
|
||||
let config = Config::load();
|
||||
|
||||
let conn = &mut pool.get().unwrap();
|
||||
|
||||
//todo stop the server if it's started
|
||||
|
||||
let deleted = diesel::delete(servers.filter(name.eq(server_name))).execute(conn).map_err(|_| "failed to delete from db".to_string())?;
|
||||
if deleted != 1 {
|
||||
return Err("servver was not found".to_string());
|
||||
}
|
||||
|
||||
let path = config.config_path + "/" + server_name;
|
||||
|
||||
fs::remove_dir_all(path).map_err(|e| format!("failed to remove the server directory, reason : {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user