Skip to content

Commit

Permalink
feature: add list logs features
Browse files Browse the repository at this point in the history
  • Loading branch information
yorodm committed Jan 28, 2020
1 parent 1e4bf2a commit 9a64b88
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "awstail"
version = "0.3.0"
version = "0.4.0"
authors = ["Yoandy Rodriguez <[email protected]>"]
edition = '2018'
homepage = "https://github.com/yorodm/awstail"
Expand Down
68 changes: 58 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use clap::{App, Arg};
use cmd::run;
use clap::{App, Arg, ArgMatches};
use ctrlc;
mod cmd;
mod util;
use humantime::parse_duration;
use rusoto_core::Region;
use rusoto_logs::CloudWatchLogsClient;
use std::str::FromStr;
use util::{
client_with_profile, create_filter_from_timestamp, create_filter_request, fetch_logs,
list_log_groups, AWSResponse,
};

fn main() {
ctrlc::set_handler(move || std::process::exit(0))
.expect("Could not set Ctrl+C handler...bailing out");
let matches = App::new("awstail")
.version("0.3.0")
fn get_options<'a>() -> ArgMatches<'a> {
return App::new("awstail")
.version("0.4.0")
.author("Yoandy Rodriguez <[email protected]>")
.about("like tail -f for AWS Cloudwatch")
.arg(
Expand Down Expand Up @@ -56,7 +61,50 @@ fn main() {
.help("Keep watching for new logs every n seconds (defaults to 10)"),
)
.get_matches();
if let Err(_e) = run(matches) {
std::process::exit(0);
}

fn main() {
ctrlc::set_handler(move || std::process::exit(0))
.expect("Could not set Ctrl+C handler...bailing out");
let matches = get_options();
let region = match matches.value_of("region") {
Some(m) => Region::from_str(m),
None => Ok(Region::UsEast1),
};
let client = match matches.value_of("profile") {
Some(m) => client_with_profile(m, region.unwrap()),
None => CloudWatchLogsClient::new(region.unwrap()),
};
if matches.is_present("list") {
list_log_groups(&client).unwrap();
} else {
let group = matches.value_of("group").unwrap();
let mtime = match matches.value_of("since") {
Some(m) => parse_duration(m),
None => parse_duration("5m"),
};
let timeout = match matches.value_of("timeout") {
Some(m) => parse_duration(m),
None => parse_duration("30s"),
};
let sleep_for = match matches.value_of("watch") {
Some(m) => parse_duration(m),
None => parse_duration("10s"),
};
let mut token: Option<String> = None;
let mut req = create_filter_request(group, mtime.unwrap(), token);
loop {
match fetch_logs(&client, req, timeout.unwrap()) {
AWSResponse::Token(x) => {
token = Some(x);
req = create_filter_request(group, mtime.unwrap(), token);
}
AWSResponse::LastLog(t) => {
token = None;
req = create_filter_from_timestamp(group, t, token);
std::thread::sleep(sleep_for.unwrap());
}
};
}
}
}
61 changes: 6 additions & 55 deletions src/cmd.rs → src/util.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
use chrono::Duration as Delta;
use chrono::{DateTime, Local, NaiveDateTime, Utc};
use clap::ArgMatches;
use console::Style;
use humantime::parse_duration;
use rusoto_core::HttpClient;
use rusoto_core::Region;
use rusoto_core::{HttpClient, Region};
use rusoto_credential::{AutoRefreshingProvider, ChainProvider, ProfileProvider};
use rusoto_logs::{
CloudWatchLogs, CloudWatchLogsClient, DescribeLogGroupsRequest, DescribeLogGroupsResponse,
FilterLogEventsRequest,
CloudWatchLogs, CloudWatchLogsClient, DescribeLogGroupsRequest, FilterLogEventsRequest,
};
use std::result::Result;
use std::str::FromStr;
use std::time::Duration;

enum AWSResponse {
pub enum AWSResponse {
Token(String),
LastLog(Option<i64>),
}
Expand All @@ -27,7 +22,7 @@ fn calculate_start_time(from: DateTime<Local>, delta: Duration) -> Option<i64> {
return Some(utc_time.timestamp_millis());
}

fn create_filter_request(
pub fn create_filter_request(
group: &str,
start: Duration,
token: Option<String>,
Expand All @@ -41,7 +36,7 @@ fn create_filter_request(
return req;
}

fn create_filter_from_timestamp(
pub fn create_filter_from_timestamp(
group: &str,
start: Option<i64>,
token: Option<String>,
Expand All @@ -67,7 +62,7 @@ fn print_date(time: Option<i64>) -> String {
}
}

fn fetch_logs(
pub fn fetch_logs(
client: &CloudWatchLogsClient,
req: FilterLogEventsRequest,
timeout: Duration,
Expand Down Expand Up @@ -145,47 +140,3 @@ pub fn list_log_groups(c: &CloudWatchLogsClient) -> Result<(), String> {
}
Ok(())
}

pub fn run(matches: ArgMatches) -> Result<(), String> {
let region = match matches.value_of("region") {
Some(m) => Region::from_str(m),
None => Ok(Region::UsEast1),
};
let client = match matches.value_of("profile") {
Some(m) => client_with_profile(m, region.unwrap()),
None => CloudWatchLogsClient::new(region.unwrap()),
};
if matches.is_present("list") {
return list_log_groups(&client);
} else {
let group = matches.value_of("group").unwrap();
let mtime = match matches.value_of("since") {
Some(m) => parse_duration(m),
None => parse_duration("5m"),
};
let timeout = match matches.value_of("timeout") {
Some(m) => parse_duration(m),
None => parse_duration("30s"),
};
let sleep_for = match matches.value_of("watch") {
Some(m) => parse_duration(m),
None => parse_duration("10s"),
};
let mut token: Option<String> = None;
let mut req = create_filter_request(group, mtime.unwrap(), token);
loop {
match fetch_logs(&client, req, timeout.unwrap()) {
AWSResponse::Token(x) => {
token = Some(x);
req = create_filter_request(group, mtime.unwrap(), token);
}
AWSResponse::LastLog(t) => {
token = None;
req = create_filter_from_timestamp(group, t, token);
std::thread::sleep(sleep_for.unwrap());
}
};
}
}
Ok(())
}

0 comments on commit 9a64b88

Please sign in to comment.