start on cli - generate core config

This commit is contained in:
beckerinj
2022-12-11 04:15:19 -05:00
parent e81b6a14de
commit daf06a33f8
12 changed files with 231 additions and 41 deletions

View File

@@ -1,10 +1,17 @@
[package]
name = "cli"
name = "monitor_cli"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
monitor_types = { path = "../lib/types" }
# monitor_types = "0.1.0"
clap = { version="4.0", features=["derive"] }
bollard = "0.13"
bollard = "0.13"
async_timing_util = "0.1.12"
rand = "0.8"
serde = "1.0"
serde_json = "1.0"
toml = "0.5"

85
cli/src/helpers.rs Normal file
View File

@@ -0,0 +1,85 @@
use std::{
fs::{self, File},
io::Write,
};
use async_timing_util::Timelength;
use clap::ArgMatches;
use monitor_types::{CoreConfig, MongoConfig};
use rand::{distributions::Alphanumeric, Rng};
use serde::Serialize;
pub fn gen_core_config(sub_matches: &ArgMatches) {
let path = sub_matches
.get_one::<String>("path")
.map(|p| p.as_str())
.unwrap_or("$HOME/.monitor/config.toml")
.to_string();
let port = sub_matches
.get_one::<String>("port")
.map(|p| p.as_str())
.unwrap_or("9000")
.parse::<u16>()
.expect("invalid port");
let mongo_uri = sub_matches
.get_one::<String>("mongo_uri")
.map(|p| p.as_str())
.unwrap_or("mongodb://mongo")
.to_string();
let mongo_db_name = sub_matches
.get_one::<String>("mongo_db_name")
.map(|p| p.as_str())
.unwrap_or("monitor")
.to_string();
let jwt_valid_for = sub_matches
.get_one::<String>("jwt_valid_for")
.map(|p| p.as_str())
.unwrap_or("1-wk")
.parse()
.expect("invalid jwt_valid_for");
let slack_url = sub_matches
.get_one::<String>("slack_url")
.map(|p| p.to_owned());
let config = CoreConfig {
port,
jwt_valid_for,
slack_url,
github_oauth: Default::default(),
mongo: MongoConfig {
uri: mongo_uri,
db_name: mongo_db_name,
app_name: "monitor".to_string(),
},
jwt_secret: generate_secret(40),
github_webhook_secret: generate_secret(30),
};
write_to_toml(&path, config);
println!("core config has been generated ✅");
}
pub fn start_mongo(sub_matches: &ArgMatches) {}
pub fn start_core(sub_matches: &ArgMatches) {}
pub fn get_periphery_config(sub_matches: &ArgMatches) {}
pub fn start_periphery(sub_matches: &ArgMatches) {}
fn write_to_toml(path: &str, toml: impl Serialize) {
fs::write(
path,
toml::to_string(&toml).expect("failed to parse config into toml"),
)
.expect("failed to write toml to file");
}
fn generate_secret(length: usize) -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(length)
.map(char::from)
.collect()
}

View File

@@ -1,3 +1,92 @@
fn main() {
println!("Hello, world!");
}
#![allow(unused)]
use clap::{arg, Arg, Command};
mod helpers;
use helpers::*;
fn cli() -> Command {
Command::new("monitor_cli")
.about("\na cli to set up monitor components")
.subcommand_required(true)
.arg_required_else_help(true)
.allow_external_subcommands(true)
.subcommand(
Command::new("core")
.about("tools to set up monitor core")
.subcommand_required(true)
.arg_required_else_help(true)
.allow_external_subcommands(true)
.subcommand(
Command::new("config_gen")
.about("generate a core config")
.arg(
arg!(--path <PATH> "sets path of generated config file. default is '~/.monitor/config.toml'")
.required(false)
)
.arg(
arg!(--port <PORT> "sets port core will run on. default is 9000")
.required(false)
)
.arg(
arg!(--mongo_uri <URI> "sets the mongo uri to use. default is 'mongodb://mongo'")
.required(false)
)
.arg(
arg!(--mongo_db_name <NAME> "sets the db name to use. default is 'monitor'")
.required(false)
)
.arg(
arg!(--jwt_valid_for <TIMELENGTH> "sets the length of time jwt stays valid for. default is 1-wk (one week)")
.required(false)
)
.arg(
arg!(--slack_url <URL> "sets the slack url to use for slack notifications")
.required(false)
),
)
.subcommand(Command::new("start_mongo").about("start up a mongo for monitor"))
.subcommand(Command::new("start").about("start up monitor core")),
)
.subcommand(
Command::new("periphery")
.about("tools to set up monitor periphery")
.subcommand_required(true)
.arg_required_else_help(true)
.allow_external_subcommands(true)
.subcommand(Command::new("config_gen").about("generate a periphery config"))
.subcommand(Command::new("start").about("start up monitor periphery")),
)
}
fn main() {
let matches = cli().get_matches();
match matches.subcommand() {
Some(("core", sub_matches)) => {
let core_command = sub_matches.subcommand().expect("invalid call, should be 'monitor_cli core <config_gen, start_mongo, start> <flags>'");
match core_command {
("config_gen", sub_matches) => gen_core_config(sub_matches),
("start_mongo", sub_matches) => start_mongo(sub_matches),
("start", sub_matches) => start_core(sub_matches),
_ => {
println!("invalid call, should be 'monitor_cli core <config_gen, start_mongo, start> <flags>'")
}
}
}
Some(("periphery", sub_matches)) => {
let periphery_command = sub_matches.subcommand().expect(
"invalid call, should be 'monitor_cli periphery <config_gen, start> <flags>'",
);
match periphery_command {
("config_gen", sub_matches) => get_periphery_config(sub_matches),
("start", sub_matches) => start_periphery(sub_matches),
_ => {
println!("invalid call, should be 'monitor_cli core <config_gen, start_mongo, start> <flags>'")
}
}
}
_ => unreachable!(),
}
}