Skip to content

Commit

Permalink
server: Add help texts to all subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsayan committed May 20, 2024
1 parent 193fbaf commit 315182b
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 21 deletions.
30 changes: 27 additions & 3 deletions libsky/src/build_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,39 @@ use std::{

pub fn format_help_txt(
binary_name: &str,
help_text_path: &str,
arguments: HashMap<&'static str, &'static str>,
help_text_path: impl AsRef<Path>,
arguments: HashMap<&str, &str>,
) -> io::Result<()> {
fmt_help_text(binary_name, &arguments, help_text_path)
}

fn fmt_help_text<'a>(
binary_name: &str,
arguments: &HashMap<&str, &str>,
help_text_path: impl AsRef<Path>,
) -> Result<(), io::Error> {
let help_msg = fs::read_to_string(help_text_path)?;
let content = super::utils::format(&help_msg, arguments, true);
let content = super::utils::format(&help_msg, &arguments, true);
// write
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join(binary_name);
let mut f = File::create(&dest_path)?;
f.write_all(content.as_bytes())?;
Ok(())
}

pub fn format_all_help_txt(
binary_name: &str,
directory: &str,
arguments: HashMap<&str, &str>,
) -> io::Result<()> {
let dir = fs::read_dir(directory)?;
for item in dir {
let item = item?;
let item_path = item.path();
let file_name = &item.file_name();
let item_name = file_name.to_str().unwrap();
fmt_help_text(&format!("{binary_name}-{item_name}"), &arguments, item_path)?;
}
Ok(())
}
5 changes: 3 additions & 2 deletions libsky/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ use {
std::{collections::HashMap, env, path::PathBuf},
};

pub fn format(body: &str, arguments: HashMap<&'static str, &'static str>, auto: bool) -> String {
pub fn format(body: &str, arguments: &HashMap<&str, &str>, auto: bool) -> String {
use regex::Regex;
let pattern = r"\{[a-zA-Z_][a-zA-Z_0-9]*\}|\{\}";
let re = Regex::new(pattern).unwrap();
re.replace_all(body, |caps: &regex::Captures| {
re.replace_all(body.as_ref(), |caps: &regex::Captures| {
let capture: &str = &caps[0];
let capture = &capture[1..capture.len() - 1];
match capture {
Expand All @@ -43,6 +43,7 @@ pub fn format(body: &str, arguments: HashMap<&'static str, &'static str>, auto:
"default_tls_endpoint" if auto => "tls@127.0.0.1:2004".to_owned(),
"password_env_var" if auto => variables::env_vars::SKYDB_PASSWORD.into(),
"version" if auto => format!("v{}", variables::VERSION),
"further_assistance" if auto => "For further assistance, refer to the official documentation here: https://docs.skytable.org".to_owned(),
arbitrary => arguments
.get(arbitrary)
.expect(&format!("could not find value for argument {}", arbitrary))
Expand Down
2 changes: 1 addition & 1 deletion server/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() -> std::io::Result<()> {
libsky::build_scripts::format_help_txt("skyd", "help_text/help", Default::default())
libsky::build_scripts::format_all_help_txt("skyd", "help_text", Default::default())
}
22 changes: 22 additions & 0 deletions server/help_text/compact
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
███████ ██  ██ ██  ██ ████████  █████  ██████  ██  ███████
██      ██  ██   ██  ██     ██    ██   ██ ██   ██ ██  ██
███████ █████    ████   ██  ███████ ██████  ██  █████
     ██ ██  ██   ██   ██  ██   ██ ██   ██ ██  ██
███████ ██  ██  ██  ██  ██  ██ ██████  ███████ ███████

Skytable {version} | https://github.com/skytable/skytable
Copyright (c) Sayan N. <ohsayan@outlook.com>

Skytable database server daemon (skyd): database file optimization utility

Usage: skyd compact

Examples:
skyd compact

Notes:
- This command will force a compaction even if it is not required
- All data is backed up prior to compaction in the 'backups/' directory in a folder
named using the following template 'YYYYMMDD_HHMMSS-before-compaction'

{further_assistance}
6 changes: 3 additions & 3 deletions server/help_text/help
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
███████ ██  ██  ██  ██  ██  ██ ██████  ███████ ███████

Skytable {version} | https://github.com/skytable/skytable
Skytable database server daemon (skyd)

Copyright (c) Sayan N. <ohsayan@outlook.com>

Skytable database server daemon (skyd)

Usage: skyd [FLAGS] [OPTIONS]
skyd [FLAGS] [OPTIONS] [COMMAND] [OPTIONS]

Expand Down Expand Up @@ -42,4 +42,4 @@ Notes:
- When you run `repair`, your previous data is backed up in the `backups/` folder.
Restore if needed.

For further assistance, refer to the official documentation here: https://docs.skytable.org
{further_assistance}
23 changes: 23 additions & 0 deletions server/help_text/repair
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
███████ ██  ██ ██  ██ ████████  █████  ██████  ██  ███████
██      ██  ██   ██  ██     ██    ██   ██ ██   ██ ██  ██
███████ █████    ████   ██  ███████ ██████  ██  █████
     ██ ██  ██   ██   ██  ██   ██ ██   ██ ██  ██
███████ ██  ██  ██  ██  ██  ██ ██████  ███████ ███████

Skytable {version} | https://github.com/skytable/skytable
Copyright (c) Sayan N. <ohsayan@outlook.com>

Skytable database server daemon (skyd): database repair utility

Usage: skyd repair

Examples:
skyd repair

Notes:
- This command will check the integrity of all database files and attempt to repair
any files that are found to have any issues
- All data is backed up prior to compaction in the 'backups/' directory in a folder
named using the following template 'YYYYMMDD_HHMMSS-before-recovery-process'

{further_assistance}
33 changes: 33 additions & 0 deletions server/help_text/restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
███████ ██  ██ ██  ██ ████████  █████  ██████  ██  ███████
██      ██  ██   ██  ██     ██    ██   ██ ██   ██ ██  ██
███████ █████    ████   ██  ███████ ██████  ██  █████
     ██ ██  ██   ██   ██  ██   ██ ██   ██ ██  ██
███████ ██  ██  ██  ██  ██  ██ ██████  ███████ ███████

Skytable {version} | https://github.com/skytable/skytable
Copyright (c) Sayan N. <ohsayan@outlook.com>

Skytable database server daemon (skyd): data restore utility

Usage: skyd restore [FLAGS] [OPTIONS]
skyd restore [FLAGS] [OPTIONS]

Required options:
--path <path> (required) Path to the backup that is to be restored

Flags:
--verify-integrity Must verify integrity before backing up
--unsafe-allow-other-host (unsafe) Enable restoring backup from a different host

Examples:
skyd restore --verify-integrity --path /mnt/nfsbackups/mybackup-DDMMYYYY

Notes:
- The backup pointed to must be a manifest based backup. Otherwise it cannot be
restored using this tool and needs manual intervention
- If you use the `--unsafe-allow-other-host` flag you must guarantee that it is ok
to restore a backup from a potentially different node
- `--verify-integrity` is almost always unnecessary as on startup `skyd` will always
verify the integrity of the on-disk data.

{further_assistance}
38 changes: 27 additions & 11 deletions server/src/engine/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,15 +655,18 @@ fn arg_decode_rs_window<CS: ConfigurationSource>(
*/

/// CLI help message
pub(super) const TXT_HELP: &str = include_str!(concat!(env!("OUT_DIR"), "/skyd"));
pub(super) const TXT_HELP: &str = include_str!(concat!(env!("OUT_DIR"), "/skyd-help"));
pub(super) const TXT_HELP_REPAIR: &str = include_str!(concat!(env!("OUT_DIR"), "/skyd-repair"));
pub(super) const TXT_HELP_COMPACT: &str = include_str!(concat!(env!("OUT_DIR"), "/skyd-compact"));
pub(super) const TXT_HELP_RESTORE: &str = include_str!(concat!(env!("OUT_DIR"), "/skyd-restore"));

#[derive(Debug, PartialEq)]
/// Return from parsing CLI configuration
pub enum CLIConfigParseReturn<T> {
/// No changes
Default,
/// Output help menu
Help,
Help(String),
/// Output version
Version,
/// We yielded a config
Expand Down Expand Up @@ -697,24 +700,37 @@ pub fn parse_cli_args<'a, T: ArgItem>(
CLIConfigParseReturn::YieldedConfig(opts)
}
}
CliMultiCommand::Help(_) | CliMultiCommand::SubcommandHelp(_, _) => {
CLIConfigParseReturn::Help
}
CliMultiCommand::Help(_) => CLIConfigParseReturn::Help(TXT_HELP.to_string()),
CliMultiCommand::SubcommandHelp(_, subcommand) => match subcommand.name() {
"repair" => CLIConfigParseReturn::Help(TXT_HELP_REPAIR.to_owned()),
"compact" => CLIConfigParseReturn::Help(TXT_HELP_COMPACT.to_owned()),
"restore" => CLIConfigParseReturn::Help(TXT_HELP_RESTORE.to_owned()),
_ => {
return Err(ConfigError::with_src(
ConfigSource::Cli,
ConfigErrorKind::ErrorString(format!(
"unknown subcommand {}",
subcommand.name()
)),
)
.into())
}
},
CliMultiCommand::Version(_) | CliMultiCommand::SubcommandVersion(_, _) => {
CLIConfigParseReturn::Version
}
CliMultiCommand::Subcommand(base_settings, subcommand_settings) => {
base_settings.ensure_empty()?;
subcommand_settings.settings().ensure_empty()?;
match subcommand_settings.name() {
CliMultiCommand::Subcommand(command, subcommand) => {
command.ensure_empty()?;
subcommand.settings().ensure_empty()?;
match subcommand.name() {
"repair" => CLIConfigParseReturn::Repair,
"compact" => CLIConfigParseReturn::Compact,
_ => {
return Err(ConfigError::with_src(
ConfigSource::Cli,
ConfigErrorKind::ErrorString(format!(
"unknown subcommand {}",
subcommand_settings.name()
subcommand.name()
)),
)
.into())
Expand Down Expand Up @@ -1094,7 +1110,7 @@ pub fn check_configuration() -> RuntimeResult<ConfigReturn> {
None
}
CLIConfigParseReturn::Compact => return Ok(ConfigReturn::Compact),
CLIConfigParseReturn::Help => return Ok(ConfigReturn::HelpMessage(TXT_HELP.to_string())),
CLIConfigParseReturn::Help(txt) => return Ok(ConfigReturn::HelpMessage(txt)),
CLIConfigParseReturn::Version => {
// just output the version
return Ok(ConfigReturn::HelpMessage(format!(
Expand Down
3 changes: 2 additions & 1 deletion server/src/engine/tests/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
engine::config::{
self, AuthDriver, CLIConfigParseReturn, ConfigAuth, ConfigEndpoint, ConfigEndpointTcp,
ConfigEndpointTls, ConfigMode, ConfigReturn, ConfigSystem, Configuration, ParsedRawArgs,
TXT_HELP,
},
util::test_utils::with_files,
};
Expand Down Expand Up @@ -136,7 +137,7 @@ fn parse_validate_cli_args_help_and_version() {
let pl2 = "skyd --version";
let ret1 = extract_cli_args_raw(pl1);
let ret2 = extract_cli_args_raw(pl2);
assert_eq!(ret1, CLIConfigParseReturn::Help);
assert_eq!(ret1, CLIConfigParseReturn::Help(TXT_HELP.to_owned()));
assert_eq!(ret2, CLIConfigParseReturn::Version);
config::set_cli_src(vec!["skyd".into(), "--help".into()]);
let ret3 = config::check_configuration().unwrap();
Expand Down

0 comments on commit 315182b

Please sign in to comment.