Skip to content

Commit

Permalink
add password option
Browse files Browse the repository at this point in the history
  • Loading branch information
sysx23 committed Jan 19, 2021
1 parent b3e9bc5 commit 408645c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/argument_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum OutputFormat {
pub struct Config {
pub hostname: String,
pub port: u16,
pub password: Option<String>,
pub follow: bool,
pub interval: u64,
pub verbosity: usize,
Expand Down Expand Up @@ -41,6 +42,11 @@ pub fn get_config() -> Result<Config, clap::Error> {
.default_value("6379")
.validator(is_parsable!(u16, "Port mast be a in range 0-65535")),
)
.arg(
Arg::from("-a --password 'Password to use when connecting to the server'")
.takes_value(true)
.required(false),
)
.arg(
Arg::from("-f --follow 'Checks for new records in slowlog and prints if any'")
.takes_value(false),
Expand Down Expand Up @@ -70,12 +76,17 @@ pub fn get_config() -> Result<Config, clap::Error> {
let config = Config {
hostname: args.value_of("hostname").unwrap().to_owned(),
port: args.value_of("port").unwrap().parse().unwrap(),
password: args.value_of("password").map(|p| p.to_owned()),
interval: args.value_of("interval").unwrap().parse().unwrap(),
follow: args.is_present("follow") || args.occurrences_of("interval") > 0,
verbosity: args.occurrences_of("verbosity") as usize,
quiet: args.is_present("quiet"),
timeout: args.value_of("timeout").unwrap().parse().unwrap(),
output_format: if args.is_present("json") {OutputFormat::Json} else {OutputFormat::Text},
output_format: if args.is_present("json") {
OutputFormat::Json
} else {
OutputFormat::Text
},
};
Ok(config)
}
24 changes: 20 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::convert::TryFrom;
use std::thread::sleep;
use std::time::Duration;
use std::convert::TryFrom;

mod argument_parsing;
use argument_parsing::OutputFormat;

use rsloglib::{SlowlogRecord,SlowlogReader, RedisConnectionProvider};

use rsloglib::{RedisConnectionProvider, SlowlogReader, SlowlogRecord};

fn print_rec(r: &SlowlogRecord, format: &OutputFormat) {
match format {
Expand All @@ -27,6 +26,14 @@ fn error_handler(e: redis::RedisError) {
redis::ErrorKind::IoError => {
log::error!("Can't establish connection to redis cluster: {}", e)
}
redis::ErrorKind::AuthenticationFailed => {
log::error!("{:?}: {}", e.kind(), e);
std::process::exit(1);
}
redis::ErrorKind::ExtensionError => {
log::error!("{:?}: {}", e.kind(), e);
std::process::exit(1);
}
_ => unimplemented!("Error not handled: {}({:?})", e, e.kind()),
}
}
Expand Down Expand Up @@ -89,7 +96,16 @@ pub fn main() {
.quiet(config.quiet)
.init()
.unwrap();
let redis_client = redis::Client::open((&config.hostname, config.port)).unwrap();
let redis_client = redis::Client::open(redis::ConnectionInfo {
addr: Box::new(redis::ConnectionAddr::Tcp(
config.hostname.clone(),
config.port,
)),
db: 0,
username: None,
passwd: config.password.clone(),
})
.unwrap();
let connection_provider = RedisConnectionProvider::from((redis_client, config.interval));
if config.follow {
read_continiously(connection_provider, &config)
Expand Down

0 comments on commit 408645c

Please sign in to comment.