#![allow(dead_code)]
mod connect_info;
mod ssh_config;
mod generate;
mod upload;
mod backup;
mod util;
use generate::generate_ssh_config;
use upload::{ upload_all, upload_patch, clear_cache };
use backup::{ backup, backup_db };
use util::{ load_json, get_session };
use std::env;
use clap::{ Parser, Subcommand };
#[derive(Debug, Parser)]
#[command(
name = "RESM - Remote Server Management tools",
version = env!("CARGO_PKG_VERSION"),
author = env!("CARGO_PKG_AUTHORS"),
)]
struct Cli
{
#[command(subcommand)]
subcommand: Subcommands,
#[clap(
short = 'e',
long,
default_value = "",
)]
env_path: String,
}
#[derive(Debug, Subcommand)]
enum Subcommands
{
Init,
List,
Show
{
#[clap(
required = true,
)]
project: String,
},
Replace
{
#[clap(
required = true,
)]
project: String,
#[clap(
short = 't',
long,
default_value = "",
)]
target_path: String,
#[clap(
long,
)]
zip: bool,
},
Patch
{
#[clap(
required = true,
)]
project: String,
#[clap(
short = 'f',
long,
default_value = "",
)]
patch_file: String,
},
Clear
{
#[clap(
required = true,
)]
project: String,
},
Backup
{
#[clap(
required = true,
)]
project: String,
#[clap(
short = 't',
long,
default_value = "",
)]
target_path: String,
},
BackupDb
{
#[clap(
required = true,
)]
project: String,
#[clap(
short = 't',
long,
value_parser,
num_args = 1..,
value_delimiter = ',',
)]
target_tables: Vec<String>,
},
}
#[tokio::main]
async fn main()
{
let cli = Cli::parse();
let env_path = if cli.env_path.len() > 0
{
cli.env_path
}
else
{
env::var("HOME").unwrap_or("".to_string()) + "/env"
};
let config_entries = load_json(&env_path);
match cli.subcommand
{
Subcommands::Init => generate_ssh_config(&config_entries),
Subcommands::List =>
{
let keys = config_entries.keys();
for key in keys
{
println!("{}", key);
}
},
Subcommands::Show { project } =>
{
if let Some(config) = config_entries.get(&project)
{
println!("{}", serde_json::to_string_pretty(&config).unwrap());
}
else
{
let mut found = false;
for (key, value) in config_entries.iter()
{
if key.starts_with(&project)
{
println!
(
"{}",
serde_json::to_string_pretty(&value).unwrap()
);
found = true;
}
}
if !found
{
println!("Project not found.");
}
return;
}
},
Subcommands::Replace { project, target_path, zip } =>
{
if let Some(config) = config_entries.get(&project)
{
upload_all(&project, config, target_path, zip).await;
}
else
{
let mut found = false;
for (key, value) in config_entries.iter()
{
if key.starts_with(&project)
{
upload_all(&key, value, target_path.clone(), zip).await;
found = true;
}
}
if !found
{
println!("Project not found.");
}
return;
}
},
Subcommands::Patch { project, patch_file } =>
{
if let Some(config) = config_entries.get(&project)
{
upload_patch(&project, config, patch_file).await;
}
else
{
let mut found = false;
for (key, value) in config_entries.iter()
{
if key.starts_with(&project)
{
upload_patch(&key, value, patch_file.clone()).await;
found = true;
}
}
if !found
{
println!("Project not found.");
}
return;
}
},
Subcommands::Clear { project } =>
{
if let Some(config) = config_entries.get(&project)
{
let session = get_session(&project).await;
clear_cache(&session, &config.remote_cache_path()).await;
session.close().await.unwrap();
}
else
{
let mut found = false;
for (key, value) in config_entries.iter()
{
if key.starts_with(&project)
{
let session = get_session(&key).await;
clear_cache(&session, &value.remote_cache_path()).await;
session.close().await.unwrap();
found = true;
}
}
if !found
{
println!("Project not found.");
}
return;
}
}
Subcommands::Backup { project, target_path } =>
{
if let Some(config) = config_entries.get(&project)
{
backup(&project, config, target_path).await;
}
else
{
let mut found = false;
for (key, value) in config_entries.iter()
{
if key.starts_with(&project)
{
backup(&key, value, target_path.clone()).await;
found = true;
}
}
if !found
{
println!("Project not found.");
}
return;
}
},
Subcommands::BackupDb { project, target_tables } =>
{
if let Some(config) = config_entries.get(&project)
{
backup_db(&project, config, target_tables).await;
}
else
{
let mut found = false;
for (key, value) in config_entries.iter()
{
if key.starts_with(&project)
{
backup_db(&key, value, target_tables.clone()).await;
found = true;
}
}
if !found
{
println!("Project not found.");
}
return;
}
},
}
}