feat: add cli interface to change config

This commit is contained in:
Matthieu Bessat 2024-07-23 23:23:44 +02:00
parent 544d8a6f89
commit 481d59e963
5 changed files with 59 additions and 8 deletions

View file

@ -3,6 +3,7 @@ mod models;
mod executor;
mod scheduler;
use argh::FromArgs;
use log::info;
use anyhow::{anyhow, Context, Result};
use axum::routing::get;
@ -27,8 +28,16 @@ pub struct AppState {
templating_env: Environment<'static>
}
fn get_config() -> Result<Config> {
let inp_def_yaml = fs::read_to_string("./config.yaml")
#[derive(Debug, FromArgs)]
/// Autotasker daemon
struct CliFlags {
/// path to YAML config file to use to configure autotasker
#[argh(option)]
config: Option<String>,
}
fn get_config(path: String) -> Result<Config> {
let inp_def_yaml = fs::read_to_string(path)
.expect("Should have been able to read the the config file");
serde_yaml::from_str(&inp_def_yaml)
@ -60,6 +69,7 @@ fn build_templating_env() -> Environment<'static> {
#[tokio::main]
async fn main() -> Result<()> {
let flags: CliFlags = argh::from_env();
env_logger::init();
info!("Starting autotasker");
@ -68,7 +78,12 @@ async fn main() -> Result<()> {
// start channel to talk to executor daemon
let (tx, rx) = mpsc::channel::<ExecutorOrder>(32);
let config: Config = get_config().expect("Cannot get config");
let config_path = match flags.config {
Some(v) => v,
None => "/etc/autotasker/config.yaml".to_string()
};
info!("Using config file at {}", &config_path);
let config: Config = get_config(config_path).expect("Cannot get config");
let state = AppState {
config,