subbeat
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
8.3 KiB

3 years ago
use clap::{App, Arg, SubCommand};
3 years ago
use subbeat::{datasources::grafana::Grafana, types::{DatasourceConfig, GrafanaConfig, InfluxConfig, PrometheusConfig, QueryConfig}};
3 years ago
pub struct CLI {
3 years ago
pub query_config: QueryConfig,
3 years ago
}
impl CLI {
3 years ago
// TODO: convert to result
3 years ago
pub fn new() -> CLI {
let matches = App::new("subbeat")
3 years ago
.version("0.0.5")
3 years ago
.about("Timeseries toolkit")
3 years ago
.subcommand(
SubCommand::with_name("grafana")
.about("Use Grafana as datasource")
.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://<grafana-url>/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),
)
.arg(
Arg::with_name("step")
.help("aggregation step")
.required(true)
.index(7),
),
3 years ago
)
3 years ago
.subcommand(
SubCommand::with_name("prometheus")
.about("Use prometheus API as datasource")
.arg(
Arg::with_name("PROM_URL")
3 years ago
.help("URL to your Prometheus instance")
3 years ago
.required(true)
.index(1),
)
.arg(
Arg::with_name("query")
.help("your query to datasource")
.required(true)
.index(2),
)
.arg(
Arg::with_name("from")
.help("timestamp")
.required(true)
.index(3),
)
.arg(
Arg::with_name("to")
.help("timestampt")
.required(true)
.index(4),
)
.arg(
Arg::with_name("step")
.help("aggregation step")
.required(true)
.index(5),
),
)
3 years ago
.subcommand(
SubCommand::with_name("influx")
.about("Use influxdb API as datasource")
.arg(
Arg::with_name("INFLUX_URL")
.help("URL to your influxdb instance")
.required(true)
.index(1),
)
.arg(
Arg::with_name("ORG_ID")
.help("URL to your influxdb instance")
.required(true)
3 years ago
.index(2),
3 years ago
)
.arg(
Arg::with_name("TOKEN")
.help("URL to your influxdb instance")
.required(true)
3 years ago
.index(3),
3 years ago
)
.arg(
Arg::with_name("query")
.help("your flux query to datasource")
.required(true)
3 years ago
.index(4),
3 years ago
)
.arg(
Arg::with_name("from")
.help("timestamp")
.required(true)
3 years ago
.index(5),
3 years ago
)
.arg(
Arg::with_name("to")
.help("timestampt")
.required(true)
3 years ago
.index(6),
3 years ago
)
.arg(
Arg::with_name("step")
.help("aggregation step")
.required(true)
3 years ago
.index(7),
3 years ago
),
)
3 years ago
.get_matches();
3 years ago
if let Some(matches) = matches.subcommand_matches("grafana") {
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 step = matches.value_of("step").unwrap().parse().unwrap();
return CLI {
3 years ago
query_config: QueryConfig {
3 years ago
datasource_config: DatasourceConfig::Grafana(GrafanaConfig {
url: url.to_owned(),
api_key: key.to_owned(),
datasource_url: datasource_url.to_owned(),
query: query.to_owned()
}),
3 years ago
from,
to,
step,
},
3 years ago
};
3 years ago
};
if let Some(matches) = matches.subcommand_matches("prometheus") {
let url = matches.value_of("PROM_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 step = matches.value_of("step").unwrap().parse().unwrap();
3 years ago
return CLI {
3 years ago
query_config: QueryConfig {
3 years ago
datasource_config: DatasourceConfig::Prometheus(PrometheusConfig {
url: url.to_owned(),
query: query.to_owned()
}),
3 years ago
from,
to,
step,
},
3 years ago
};
3 years ago
};
3 years ago
if let Some(matches) = matches.subcommand_matches("influx") {
3 years ago
let url = matches.value_of("INFLUX_URL").unwrap();
3 years ago
let org_id = matches.value_of("ORG_ID").unwrap();
let token = matches.value_of("TOKEN").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 step = matches.value_of("step").unwrap().parse().unwrap();
return CLI {
query_config: QueryConfig {
datasource_config: DatasourceConfig::Influx(InfluxConfig {
url: url.to_owned(),
org_id: org_id.to_owned(),
token: token.to_owned(),
query: query.to_owned()
}),
from,
to,
step,
},
};
};
3 years ago
panic!("Unknown datasource");
3 years ago
}
3 years ago
}