diff --git a/Cargo.lock b/Cargo.lock index 0d40ecc9..c61c5e69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -254,13 +254,50 @@ dependencies = [ "ansi_term", "atty", "bitflags", - "strsim", + "strsim 0.8.0", "textwrap", "unicode-width", "vec_map", "yaml-rust", ] +[[package]] +name = "clap" +version = "4.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06badb543e734a2d6568e19a40af66ed5364360b9226184926f89d229b4b4267" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "once_cell", + "strsim 0.10.0", + "termcolor", +] + +[[package]] +name = "clap_derive" +version = "4.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42f169caba89a7d512b5418b09864543eeb4d497416c917d7137863bd2076ad" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "clipboard-win" version = "4.4.2" @@ -594,6 +631,12 @@ dependencies = [ "ahash 0.7.6", ] +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -906,6 +949,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "os_str_bytes" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" + [[package]] name = "parking_lot" version = "0.12.1" @@ -982,6 +1031,30 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.46" @@ -1268,7 +1341,7 @@ dependencies = [ name = "sky-bench" version = "0.8.0" dependencies = [ - "clap", + "clap 2.34.0", "devtimer", "env_logger", "libstress", @@ -1284,7 +1357,7 @@ name = "sky-migrate" version = "0.8.0" dependencies = [ "bincode", - "clap", + "clap 2.34.0", "env_logger", "log", "skytable 0.8.0 (git+https://github.com/skytable/client-rust.git)", @@ -1310,7 +1383,7 @@ dependencies = [ "bytes", "cc", "chrono", - "clap", + "clap 2.34.0", "env_logger", "hashbrown", "jemallocator", @@ -1336,7 +1409,7 @@ dependencies = [ name = "skysh" version = "0.8.0" dependencies = [ - "clap", + "clap 4.0.17", "crossterm", "lazy_static", "libsky", @@ -1418,6 +1491,12 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "subtle" version = "2.4.1" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 5d348c4b..aa8f0562 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -3,6 +3,7 @@ name = "skysh" version = "0.8.0" authors = ["Sayan Nandan "] edition = "2021" +description = "The Skytable Shell (skysh)" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -15,7 +16,7 @@ skytable = { git = "https://github.com/skytable/client-rust", branch = "next", f ], default-features = false } # external deps tokio = { version = "1.21.2", features = ["full"] } -clap = { version = "2", features = ["yaml"] } +clap = { version = "4.0.15", features = ["derive"] } rustyline = "10.0.0" crossterm = "0.25.0" -lazy_static = "1.4.0" +lazy_static = "1.4.0" \ No newline at end of file diff --git a/cli/README.md b/cli/README.md index e17064ee..ffa84f39 100644 --- a/cli/README.md +++ b/cli/README.md @@ -1,9 +1,18 @@ # Skytable Shell -This directory contains the source code for the Skytable Shell. When compiled, it produces a single binary `skysh`, short for **Sky**table **s**hell. The Skytable shell can be used to connect to a Skytable instance and it provides some nice to have shell features like history, terminal interactions and more. +This directory contains the source code for the Skytable Shell. When compiled, it produces a single binary `skysh`, short for **Sky**table **s**hell. The Skytable shell can be used to connect to a Skytable instance, and it provides some nice to have shell features like history, terminal interactions and more. By default, `skysh` connects to the `default:default` entity for an instance running on `127.0.0.1` (localhost) on port `2003`. You can change these connection settings use command-line parameters. +# Usage + +The below terminal interactions show a few ways in which `skysh` can be used: + +```shell +# Use skysh to run one-off expressions +skysh -e "SET x 100" "GET x" +``` + ## License All files in this directory are distributed under the [AGPL-3.0 License](../LICENSE). diff --git a/cli/src/argparse.rs b/cli/src/argparse.rs index e8a3f0b3..bd2b307f 100644 --- a/cli/src/argparse.rs +++ b/cli/src/argparse.rs @@ -24,9 +24,10 @@ * */ +use crate::cli::Cli; use { crate::{runner::Runner, tokenizer}, - clap::{load_yaml, App}, + clap::Parser, crossterm::{ cursor, execute, terminal::{Clear, ClearType}, @@ -37,7 +38,6 @@ use { std::{io::stdout, process}, }; -const ADDR: &str = "127.0.0.1"; const SKYSH_HISTORY_FILE: &str = ".sky_history"; const HELP_TEXT: &str = r#" @@ -100,16 +100,7 @@ pub async fn start_repl() { }; } - let cfg_layout = load_yaml!("./cli.yml"); - let matches = App::from_yaml(cfg_layout).get_matches(); - let host = libsky::option_unwrap_or!(matches.value_of("host"), ADDR); - let port = match matches.value_of("port") { - Some(p) => match p.parse::() { - Ok(p) => p, - Err(_) => fatal!("Invalid port"), - }, - None => 2003, - }; + let cli = Cli::parse(); let mut editor = match Editor::<()>::new() { Ok(e) => e, Err(e) => fatal!("Editor init error: {}", e), @@ -123,9 +114,9 @@ pub async fn start_repl() { ), rustyline::Cmd::Noop, ); - let con = match matches.value_of("cert") { - Some(cert) => Runner::new_secure(host, port, cert).await, - None => Runner::new_insecure(host, port).await, + let con = match cli.ssl_cert { + Some(cert) => Runner::new_secure(&cli.host, cli.port, &cert).await, + None => Runner::new_insecure(&cli.host, cli.port).await, }; let mut runner = match con { Ok(c) => c, @@ -143,10 +134,10 @@ pub async fn start_repl() { }; } - if let Some(eval_expr) = matches.values_of("eval") { - for eval_expr in eval_expr { + if let Some(expressions) = cli.expressions { + for eval_expr in expressions { if !eval_expr.is_empty() { - runner.run_query(eval_expr).await; + runner.run_query(&eval_expr).await; } } process::exit(0x00); diff --git a/cli/src/cli.rs b/cli/src/cli.rs new file mode 100644 index 00000000..cd40d68c --- /dev/null +++ b/cli/src/cli.rs @@ -0,0 +1,29 @@ +use clap::Parser; +use clap::ArgAction; + +const HELP_TEMPLATE: &'static str = r#" +{before-help}{name} {version} +{author-with-newline}{about-with-newline} +{usage-heading} {usage} + +{all-args}{after-help} +"#; + +#[derive(Parser)] +#[command(author, version, about, long_about=None, disable_help_flag=true, help_template=HELP_TEMPLATE)] +pub struct Cli { + #[arg(short = 'C', long = "sslcert", help="Sets the PEM certificate to use for SSL connections", value_name = "CERT")] + pub ssl_cert: Option, + + #[arg(short = 'e', long = "eval", help = "Run one or more expressions without REPL", value_name = "EXPRESSION", num_args=0..)] + pub expressions: Option>, + + #[arg(short, long, help = "Sets the remote host to connect to", default_value = "127.0.0.1", value_name = "HOST")] + pub host: String, + + #[arg(short, long, help = "Sets the remote port to connect to", default_value_t = 2003, value_name = "PORT")] + pub port: u16, + + #[arg(long, help="Print help information", action=ArgAction::Help)] + pub help: Option +} diff --git a/cli/src/cli.yml b/cli/src/cli.yml deleted file mode 100644 index db562b18..00000000 --- a/cli/src/cli.yml +++ /dev/null @@ -1,55 +0,0 @@ -# -# Created on Tue Nov 03 2020 -# -# This file is a part of Skytable -# Copyright (c) 2020, Sayan Nandan -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -# - -name: Skytable Shell -version: 0.8.0 -author: Sayan N. -about: The Skytable Shell (skysh) -args: - - host: - short: h - required: false - long: host - value_name: host - help: Sets the remote host to connect to - takes_value: true - - port: - short: p - required: false - long: port - value_name: port - help: Sets the remote port to connect to - takes_value: true - - eval: - short: e - required: false - long: eval - multiple: true - value_name: expression - help: Run an expression without REPL - takes_value: true - - cert: - short: C - required: false - long: sslcert - value_name: cert - help: Sets the PEM certificate to use for SSL connections - takes_value: true diff --git a/cli/src/main.rs b/cli/src/main.rs index fd90672e..afa7c311 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -30,8 +30,10 @@ #[macro_use] mod macros; mod argparse; +mod cli; mod runner; mod tokenizer; + // tests #[cfg(test)] mod tests;