Skip to content

add cli subcommands #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 107 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ tokio-test = "0.4.2"
serde_json = "1"
itertools = "0.10"
clap = { version = "4.3.1", features = ["derive", "env"] }
prettytable-rs = "^0.10"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = [
"json",
81 changes: 81 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::cmd_args::{Args, Commands, ConfigPoolsSubcommands, ConfigSubcommands};
use crate::config;
use prettytable::{format, row, Cell, Row, Table};

pub fn init(args: &Args) {
match &args.command {
Some(Commands::Config(command)) => match command {
ConfigSubcommands::Pools(subcommand) => match subcommand {
ConfigPoolsSubcommands::List => cmd_config_pools_list(),
ConfigPoolsSubcommands::Add => cmd_config_pools_add(),
ConfigPoolsSubcommands::Remove => cmd_config_pools_remove(),
},
ConfigSubcommands::Shards => println!("Shard"),
},
None => {}
}
std::process::exit(exitcode::OK);
}

fn cmd_config_pools_list() {
println!("Pools list");

let cfg = config::get_config();

// Create the table
let mut table = Table::new();

// let format = format::FormatBuilder::new()
// .column_separator('|')
// .borders('|')
// .separators(
// &[format::LinePosition::Top, format::LinePosition::Bottom],
// format::LineSeparator::new('-', '+', '+', '+'),
// )
// .padding(1, 1)
// .build();
// table.set_format(format);
/* Pool { pool_mode: Session, load_balancing_mode: Random, default_role:
* "primary", query_parser_enabled: true, query_parser_max_length: None,
* query_parser_read_write_splitting: false, primary_reads_enabled: true,
* connect_timeout: None, idle_timeout: None, server_lifetime: None,
* sharding_function: PgBigintHash, automatic_sharding_key: None,
* sharding_key_regex: None, shard_id_regex: None, regex_search_limit: None,
* auth_query: None, auth_query_user: None, auth_query_password: None,
* cleanup_server_connections: true, plugins: None, shards: {"0": Shard {
* database: "some_db", mirrors: None, servers: [ServerConfig { host:
* "127.0.0.1", port: 5432, role: Primary }, ServerConfig { host:
* "localhost", port: 5432, role: Replica }] }}, users: {"0": User {
* username: "simple_user", password: Some("simple_user"), server_username:
* None, server_password: None, pool_size: 5, min_pool_size: Some(3),
* pool_mode: None, server_lifetime: Some(60000), statement_timeout: 0 }} }
*/
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);

table.set_titles(row![
"pool_name",
"pool_mode",
"load_balancing_mode",
"default_role"
]);

for (pool_name, pool) in cfg.pools {
table.add_row(Row::new(vec![
Cell::new(&pool_name),
Cell::new(&format!("{}", pool.pool_mode.to_string())),
Cell::new(&format!("{}", pool.load_balancing_mode.to_string())),
Cell::new(&format!("{}", pool.default_role.to_string())),
]));
}

// Print the table to stdout
table.printstd();
}

fn cmd_config_pools_add() {
todo!("Add pools")
}

fn cmd_config_pools_remove() {
todo!("Remove pools")
}
25 changes: 24 additions & 1 deletion src/cmd_args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Parser, ValueEnum};
use clap::{Parser, Subcommand, ValueEnum};
use tracing::Level;

/// PgCat: Nextgen PostgreSQL Pooler
@@ -22,6 +22,9 @@ pub struct Args {
help = "disable colors in the log output"
)]
pub no_color: bool,

#[command(subcommand)]
pub command: Option<Commands>,
}

pub fn parse() -> Args {
@@ -34,3 +37,23 @@ pub enum LogFormat {
Structured,
Debug,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
#[command(subcommand)]
Config(ConfigSubcommands),
}

#[derive(Subcommand, Debug)]
pub enum ConfigSubcommands {
#[command(subcommand)]
Pools(ConfigPoolsSubcommands),
Shards,
}

#[derive(Subcommand, Debug)]
pub enum ConfigPoolsSubcommands {
List,
Add,
Remove,
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod admin;
pub mod auth_passthrough;
pub mod cli;
pub mod client;
pub mod cmd_args;
pub mod config;
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ extern crate log;
extern crate md5;
extern crate num_cpus;
extern crate once_cell;
extern crate prettytable;
extern crate rustls_pemfile;
extern crate serde;
extern crate serde_derive;
@@ -60,6 +61,7 @@ use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::broadcast;

use pgcat::cli;
use pgcat::cmd_args;
use pgcat::config::{get_config, reload_config, VERSION};
use pgcat::dns_cache;
@@ -71,6 +73,7 @@ use pgcat::stats::{Collector, Reporter, REPORTER};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = cmd_args::parse();

logger::init(&args);

info!("Welcome to PgCat! Meow. (Version {})", VERSION);
@@ -94,6 +97,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};
});
}
cli::init(&args);

let config = get_config();