From 40f5c69a13e8165bff02a26c848aeeebe378628b Mon Sep 17 00:00:00 2001 From: Takayuki Maeda <41065217+TaKO8Ki@users.noreply.github.com> Date: Sat, 28 Aug 2021 15:42:44 +0900 Subject: [PATCH] Pass config file path as a command line argument (#45) * pass config file as an argument * update keymap --- Cargo.lock | 138 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 + README.md | 16 +++-- sample.toml | 14 ----- src/cli.rs | 14 +++++ src/config.rs | 30 ++++++++- src/database/mysql.rs | 2 +- src/main.rs | 14 ++--- 8 files changed, 199 insertions(+), 32 deletions(-) create mode 100644 src/cli.rs diff --git a/Cargo.lock b/Cargo.lock index 90d2885..ba08ce3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "anyhow" version = "1.0.41" @@ -60,6 +69,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi 0.3.9", +] + [[package]] name = "autocfg" version = "0.1.7" @@ -200,6 +220,21 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "clipboard-win" version = "3.1.1" @@ -349,6 +384,27 @@ dependencies = [ "generic-array", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if 1.0.0", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi 0.3.9", +] + [[package]] name = "dotenv" version = "0.15.0" @@ -538,9 +594,11 @@ dependencies = [ "anyhow", "async-trait", "chrono", + "clap", "copypasta", "crossterm 0.19.0", "database-tree", + "dirs-next", "easy-cast", "futures", "itertools", @@ -549,6 +607,7 @@ dependencies = [ "serde", "serde_json", "sqlx", + "structopt", "strum", "strum_macros", "tokio", @@ -1091,6 +1150,30 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[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-macro-hack" version = "0.5.19" @@ -1177,6 +1260,16 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" +dependencies = [ + "getrandom 0.2.3", + "redox_syscall", +] + [[package]] name = "regex" version = "1.5.4" @@ -1529,6 +1622,36 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "structopt" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69b041cdcb67226aca307e6e7be44c8806423d83e018bd662360a93dabce4d71" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7813934aecf5f51a54775e00068c237de98489463968231a51746bbbc03f9c10" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "strum" version = "0.21.0" @@ -1582,6 +1705,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.25" @@ -1768,6 +1900,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.3" diff --git a/Cargo.toml b/Cargo.toml index 796ad1b..f56aaa5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,9 @@ copypasta = { version = "0.7.0", default-features = false } async-trait = "0.1.50" itertools = "0.10.0" rust_decimal = "1.15" +dirs-next = "2.0" +clap = "2.33.3" +structopt = "0.3.22" [target.'cfg(all(target_family="unix",not(target_os="macos")))'.dependencies] which = "4.1" diff --git a/README.md b/README.md index c38f5c4..b283fc6 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,13 @@ $ cargo install --version 0.1.0-alpha.0 gobang | Key | Description | | ---- | ---- | -| h | scroll left | -| j | scroll down | -| k | scroll up | -| l | scroll right | -| Ctrl + d | scroll down multiple lines | -| Ctrl + u | scroll up multiple lines | +| h | Scroll left | +| j | Scroll down | +| k | Scroll up | +| l | Scroll right | +| Ctrl + d | Scroll down multiple lines | +| Ctrl + u | Scroll up multiple lines | +| y | Copy a cell value | +| | Move focus to right | +| | Move focus to left | +| / | Filter | diff --git a/sample.toml b/sample.toml index 038e106..b9e071f 100644 --- a/sample.toml +++ b/sample.toml @@ -5,20 +5,6 @@ user = "root" host = "localhost" port = 3306 -[[conn]] -type = "mysql" -user = "root" -host = "localhost" -port = 3306 -database = "world" - -[[conn]] -type = "mysql" -user = "root" -host = "localhost" -port = 3306 -database = "employees" - [[conn]] type = "postgres" user = "postgres" diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..5c1fc7d --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,14 @@ +use crate::config::CliConfig; +use structopt::StructOpt; + +/// A cross-platform terminal database tool written in Rust +#[derive(StructOpt, Debug)] +#[structopt(name = "gobang")] +pub struct Cli { + #[structopt(flatten)] + pub config: CliConfig, +} + +pub fn parse() -> Cli { + Cli::from_args() +} diff --git a/src/config.rs b/src/config.rs index 862969d..631cf0a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,6 +3,14 @@ use serde::Deserialize; use std::fmt; use std::fs::File; use std::io::{BufReader, Read}; +use structopt::StructOpt; + +#[derive(StructOpt, Debug)] +pub struct CliConfig { + /// Set the config file + #[structopt(long, short, global = true)] + config_path: Option, +} #[derive(Debug, Deserialize, Clone)] pub struct Config { @@ -118,8 +126,13 @@ impl Default for KeyConfig { } impl Config { - pub fn new(path: &str) -> anyhow::Result { - if let Ok(file) = File::open(path) { + pub fn new(config: &CliConfig) -> anyhow::Result { + let config_path = if let Some(config_path) = &config.config_path { + config_path.clone() + } else { + get_app_config_path()?.join("config.yml") + }; + if let Ok(file) = File::open(config_path) { let mut buf_reader = BufReader::new(file); let mut contents = String::new(); buf_reader.read_to_string(&mut contents)?; @@ -176,3 +189,16 @@ impl Connection { matches!(self.r#type, DatabaseType::MySql) } } + +pub fn get_app_config_path() -> anyhow::Result { + let mut path = if cfg!(target_os = "macos") { + dirs_next::home_dir().map(|h| h.join(".config")) + } else { + dirs_next::config_dir() + } + .ok_or_else(|| anyhow::anyhow!("failed to find os config dir."))?; + + path.push("gobang"); + std::fs::create_dir_all(&path)?; + Ok(path) +} diff --git a/src/database/mysql.rs b/src/database/mysql.rs index a21899c..664a661 100644 --- a/src/database/mysql.rs +++ b/src/database/mysql.rs @@ -216,7 +216,7 @@ impl Pool for MySqlPool { WHERE REFERENCED_TABLE_SCHEMA IS NULL AND REFERENCED_TABLE_NAME IS NULL - TABLE_SCHEMA = ? + AND TABLE_SCHEMA = ? AND TABLE_NAME = ? ", ) diff --git a/src/main.rs b/src/main.rs index 643dd9f..5cd8ef2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod app; +mod cli; mod clipboard; mod components; mod config; @@ -16,24 +17,19 @@ use crossterm::{ terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, ExecutableCommand, }; -use std::{ - io::{self, stdout}, - panic, -}; +use std::{io, panic}; use tui::{backend::CrosstermBackend, Terminal}; #[tokio::main] async fn main() -> anyhow::Result<()> { - enable_raw_mode()?; - outln!("gobang logger"); - let config = config::Config::new("sample.toml")?; + let value = crate::cli::parse(); + let config = config::Config::new(&value.config)?; - let stdout = stdout(); setup_terminal()?; - let backend = CrosstermBackend::new(stdout); + let backend = CrosstermBackend::new(io::stdout()); let mut terminal = Terminal::new(backend)?; let events = event::Events::new(250); let mut app = App::new(config);