Skip to content

Commit 2f27347

Browse files
committed
x
1 parent 005469a commit 2f27347

File tree

6 files changed

+93
-3
lines changed

6 files changed

+93
-3
lines changed

Cargo.lock

Lines changed: 11 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ self_cell = "1.2.0"
478478
semver = "1.0.14"
479479
seq-marked = { version = "0.3.1", features = ["seq-marked-serde", "seq-marked-bincode", "seqv-serde"] }
480480
serde = { version = "1.0.164", features = ["derive", "rc"] }
481+
serde-bridge = "0.0.3"
482+
serde-env = { git = "https://github.com/Xuanwo/serde-env", rev = "085d4b1" }
481483
serde_derive = "1"
482484
serde_ignored = "0.1.10"
483485
serde_json = { version = "1.0.85", default-features = false, features = ["preserve_order", "unbounded_depth"] }

src/query/config/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ databend-common-storage = { workspace = true }
2626
databend-common-tracing = { workspace = true }
2727
log = { workspace = true }
2828
serde = { workspace = true }
29+
serde-bridge = { workspace = true }
30+
serde-env = { workspace = true }
2931
serde_ignored = { workspace = true }
3032
serde_with = { workspace = true }
3133
serfig = { workspace = true }

src/query/config/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ use databend_common_tracing::CONFIG_DEFAULT_LOG_LEVEL;
5959
use serde::Deserialize;
6060
use serde::Serialize;
6161
use serde_with::with_prefix;
62-
use serfig::collectors::from_env;
6362
use serfig::collectors::from_file;
6463
use serfig::collectors::from_self;
6564

65+
use super::env::from_env;
6666
use super::inner;
6767
use super::inner::CatalogConfig as InnerCatalogConfig;
6868
use super::inner::CatalogHiveConfig as InnerCatalogHiveConfig;
@@ -3529,7 +3529,7 @@ pub struct SpillConfig {
35293529
/// Allow space in bytes to spill to local disk.
35303530
pub spill_local_disk_max_bytes: u64,
35313531

3532-
// TODO: We need to fix StorageConfig so that it supports environment variables and command line injections.
3532+
// TODO: We need to fix StorageConfig so that it supports command line injections.
35333533
#[clap(skip)]
35343534
pub storage: Option<StorageConfig>,
35353535
}

src/query/config/src/env.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2021 Datafuse Labs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::fmt::Debug;
16+
use std::marker::PhantomData;
17+
18+
use log::debug;
19+
use serde::de::DeserializeOwned;
20+
use serde::Serialize;
21+
use serde_bridge::IntoValue;
22+
use serde_bridge::Value;
23+
use serfig::collectors::IntoCollector;
24+
use serfig::Collector;
25+
26+
pub fn from_env<V>() -> Environment<V>
27+
where V: DeserializeOwned + Serialize + Debug {
28+
Environment {
29+
_v: Default::default(),
30+
}
31+
}
32+
33+
#[derive(Debug)]
34+
pub struct Environment<V: DeserializeOwned + Serialize + Debug> {
35+
_v: PhantomData<V>,
36+
}
37+
38+
impl<V> Collector<V> for Environment<V>
39+
where V: DeserializeOwned + Serialize + Debug
40+
{
41+
fn collect(&mut self) -> anyhow::Result<Value> {
42+
let v: V = serde_env::from_env()?;
43+
debug!("value parsed from env: {:?}", v);
44+
Ok(v.into_value()?)
45+
}
46+
}
47+
48+
impl<V> IntoCollector<V> for Environment<V>
49+
where V: DeserializeOwned + Serialize + Debug + 'static
50+
{
51+
fn into_collector(self) -> Box<dyn Collector<V>> {
52+
Box::new(self)
53+
}
54+
}
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use crate::Config;
59+
60+
#[test]
61+
fn test_env() {
62+
// see https://github.com/Xuanwo/serde-env/pull/48
63+
let cfg: Config = serde_env::from_iter([
64+
("LOG_TRACING_OTLP_ENDPOINT", "http://127.0.2.1:1111"),
65+
("LOG_TRACING_CAPTURE_LOG_LEVEL", "DebuG"),
66+
])
67+
.unwrap();
68+
69+
assert_eq!(
70+
cfg.log.tracing.tracing_otlp.endpoint,
71+
"http://127.0.2.1:1111"
72+
);
73+
assert_eq!(cfg.log.tracing.tracing_capture_log_level, "DebuG");
74+
}
75+
}

src/query/config/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod builtin;
2929
/// - `TryInto<inner::InnerConfig> for config::Config`
3030
/// - `From<inner::InnerConfig> for config::Config`
3131
mod config;
32+
mod env;
3233
mod global;
3334
mod inner;
3435
mod mask;

0 commit comments

Comments
 (0)