Skip to content

Commit c836d3b

Browse files
committed
use figment for config
1 parent f59b385 commit c836d3b

File tree

7 files changed

+115
-32
lines changed

7 files changed

+115
-32
lines changed

Cargo.lock

Lines changed: 87 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ rayon = "1.6.1"
2424
regex = "1.7.1"
2525
serde = { version = "1.0.152", features = ["derive"] }
2626
serde_json = "1.0.91"
27-
serde_yaml = "0.9.16"
2827
tokio = { version = "1.23.0", features = ["rt", "macros", "time", "rt-multi-thread", "io-util", "fs", "signal"] }
2928
tokio-stream = "0.1.11"
3029
tokio-util = { version = "0.7.4", features = ["codec"] }
@@ -35,10 +34,7 @@ zettabgp = "0.3.4"
3534
hickory-resolver = "0.24.0"
3635
include_dir = { version = "0.7.3", optional = true }
3736
mime_guess = { version = "2.0.4", optional = true }
38-
39-
[[bin]]
40-
name = "fernglas-configcheck"
41-
path = "src/config_check.rs"
37+
figment = { version = "0.10.12", features = ["yaml", "env"] }
4238

4339
[features]
4440
embed-static = ["include_dir", "mime_guess"]

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
name = "config.yaml";
154154
text = builtins.toJSON cfg.settings;
155155
checkPhase = ''
156-
${fernglasPkgs.fernglas}/bin/fernglas-configcheck $out
156+
FERNGLAS_CONFIG_CHECK=1 ${fernglasPkgs.fernglas}/bin/fernglas $out
157157
'';
158158
};
159159
in {

frontend/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
static: dist,
1717
proxy: {
1818
    "/api/": {
19-
  target: 'http://localhost:3000'
19+
  target: 'https://lg.staging.service.wobcom.de'
2020
}
2121
  },
2222
},

src/config_check.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,18 @@ pub mod table_impl;
99

1010
use serde::Deserialize;
1111

12-
pub fn config_path_from_args() -> String {
13-
let mut args = std::env::args();
14-
let program = args.next().unwrap();
15-
let config_path = match args.next() {
16-
Some(v) => v,
17-
_ => usage(&program),
18-
};
12+
pub fn config_path_from_args() -> Option<String> {
13+
let mut args = std::env::args().skip(1);
14+
let config_path = args.next();
1915
if args.next().is_some() {
20-
usage(&program);
16+
usage();
2117
}
2218

2319
config_path
2420
}
2521

26-
fn usage(program: &str) -> ! {
22+
pub fn usage() -> ! {
23+
let program = std::env::args().next().unwrap();
2724
eprintln!("usage: {} <CONFIG>", program);
2825
std::process::exit(1)
2926
}
@@ -39,4 +36,6 @@ pub enum CollectorConfig {
3936
pub struct Config {
4037
pub collectors: Vec<CollectorConfig>,
4138
pub api: api::ApiServerConfig,
39+
/// Only check config and exit
40+
pub check_config: bool,
4241
}

src/main.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use fernglas::*;
22
use futures_util::future::{join_all, select_all};
33
use log::*;
44
use tokio::signal::unix::{signal, SignalKind};
5+
use figment::Figment;
6+
use figment::providers::{Yaml, Env, Format};
57

68
#[cfg(feature = "mimalloc")]
79
#[global_allocator]
@@ -11,8 +13,21 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
1113
async fn main() -> anyhow::Result<()> {
1214
env_logger::init();
1315

14-
let config_path = config_path_from_args();
15-
let cfg: Config = serde_yaml::from_slice(&tokio::fs::read(&config_path).await?)?;
16+
let mut figment = Figment::new();
17+
if let Some(config_path) = config_path_from_args() {
18+
figment = figment.merge(Yaml::file(config_path));
19+
}
20+
figment = figment.merge(Env::prefixed("FERNGLAS_"));
21+
22+
let cfg: Config = figment.extract()?;
23+
24+
if cfg.collectors.is_empty() {
25+
fernglas::usage();
26+
}
27+
28+
if cfg.check_config {
29+
std::process::exit(0);
30+
}
1631

1732
let store: store_impl::InMemoryStore = Default::default();
1833

0 commit comments

Comments
 (0)