From 010850408c8262fa573c1240e59b8d92cb3acea6 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Thu, 21 Oct 2021 17:49:54 +0300 Subject: [PATCH] cli to struct --- README.md | 1 - src/cli.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 61 ++++++-------------------------------------- 3 files changed, 81 insertions(+), 54 deletions(-) create mode 100644 src/cli.rs diff --git a/README.md b/README.md index e1fccb7..7401f3c 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,4 @@ subbeat ``` subbeat http://localhost:3000 eyJrIjoiWnRRMTNmcGpvTHNPb3UzNzdUNUphRm53Rk9tMTNzOTQiLCJuIjoic3ViYmVhdC10ZXN0IiwiaWQiOjF9 "/api/datasources/proxy/1/api/v1/query_range" "rate(go_memstats_alloc_bytes_total[5m])" 1634672070 1634672970 - ``` \ No newline at end of file diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..352f3e8 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,73 @@ +use clap::{App, Arg, SubCommand}; + +pub struct CLI { + pub url: String, + pub key: String, + pub datasource_url: String, + pub query: String, + pub from: u64, + pub to: u64 +} + +impl CLI { + + pub fn new() -> CLI { + + let matches = App::new("subbeat") + .version("0.0.2") + .about("Timeseries toolkit") + .arg( + Arg::with_name("GRAFANA_URL") + .help("URL to your Grafana instance") + .required(true) + .index(1), + ) + .arg( + Arg::with_name("GRAFANA_API_KEY") + .help("Grafna API Key. Go to http:///org/apikeys to get one") + .required(true) + .index(2), + ) + .arg( + Arg::with_name("datasource_url") + .help("relative path to datasource") + .required(true) + .index(3), + ) + .arg( + Arg::with_name("query") + .help("your query to datasource") + .required(true) + .index(4), + ) + .arg( + Arg::with_name("from") + .help("timestamp") + .required(true) + .index(5), + ) + .arg( + Arg::with_name("to") + .help("timestampt") + .required(true) + .index(6), + ) + .get_matches(); + + let url = matches.value_of("GRAFANA_URL").unwrap(); + let key = matches.value_of("GRAFANA_API_KEY").unwrap(); + let datasource_url = matches.value_of("datasource_url").unwrap(); + let query = matches.value_of("query").unwrap(); + let from = matches.value_of("from").unwrap().parse().unwrap(); + let to = matches.value_of("to").unwrap().parse().unwrap(); + + CLI{ + url: url.to_owned(), + key: key.to_owned(), + datasource_url: datasource_url.to_owned(), + query: query.to_owned(), + from, + to + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ef53d38..2850e31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,70 +1,25 @@ -use clap::{App, Arg, SubCommand}; + use subbeat::grafana_service; mod types; +mod cli; #[tokio::main] async fn main() -> types::Result<()> { - let matches = App::new("subbeat") - .version("0.0.2") - .about("Timeseries toolkit") - .arg( - Arg::with_name("GRAFANA_URL") - .help("URL to your Grafana instance") - .required(true) - .index(1), - ) - .arg( - Arg::with_name("GRAFANA_API_KEY") - .help("Grafna API Key. Go to http:///org/apikeys to get one") - .required(true) - .index(2), - ) - .arg( - Arg::with_name("datasource_url") - .help("relative path to datasource") - .required(true) - .index(3), - ) - .arg( - Arg::with_name("query") - .help("your query to datasource") - .required(true) - .index(4), - ) - .arg( - Arg::with_name("from") - .help("timestamp") - .required(true) - .index(5), - ) - .arg( - Arg::with_name("to") - .help("timestampt") - .required(true) - .index(6), - ) - .get_matches(); - - let url = matches.value_of("GRAFANA_URL").unwrap(); - let key = matches.value_of("GRAFANA_API_KEY").unwrap(); - let datasource_url = matches.value_of("datasource_url").unwrap(); - let query = matches.value_of("query").unwrap(); - let from = matches.value_of("from").unwrap().parse().unwrap(); - let to = matches.value_of("to").unwrap().parse().unwrap(); + let cli = cli::CLI::new(); - let gs = grafana_service::GrafanaService::new(url.to_string(), key.to_string()); + let gs = grafana_service::GrafanaService::new(cli.url.to_string(), cli.key.to_string()); // gs.test_connection().await?; // gs.get_datasources().await?; // "http://localhost:3000/d/YeBxHjzWz/starter-app-stats?editPanel=2&orgId=1" let r = gs .extract_metrics( - datasource_url, - query, - from, - to, + &cli.datasource_url, + &cli.query, + cli.from, + cli.to, 15, ) .await?;