Skip to content

Commit 2c52bd0

Browse files
committed
feat(config): Allow loading config from JSON file
1 parent c8825bc commit 2c52bd0

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/main.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[macro_use]
22
extern crate log;
33

4+
use std::path::PathBuf;
5+
46
use structopt::StructOpt;
57
use tokio::runtime::Builder;
68
use tokio::signal;
@@ -11,16 +13,23 @@ struct Opts {
1113
verbose: u32,
1214
#[structopt(short, long = "db-path")]
1315
database_path: Option<String>,
16+
#[structopt(short, long = "config")]
17+
config_path: Option<PathBuf>,
1418
#[structopt(long)]
1519
dump_config: bool,
1620
}
1721

1822
async fn run(opts: Opts) -> color_eyre::eyre::Result<()> {
19-
// Connect to database
20-
let mut db = hyperion::db::Db::try_default(opts.database_path.as_deref()).await?;
21-
2223
// Load configuration
23-
let config = hyperion::models::Config::load(&mut db).await?;
24+
let config = {
25+
if let Some(config_path) = opts.config_path.as_deref() {
26+
hyperion::models::Config::load_file(config_path).await?
27+
} else {
28+
// Connect to database
29+
let mut db = hyperion::db::Db::try_default(opts.database_path.as_deref()).await?;
30+
hyperion::models::Config::load(&mut db).await?
31+
}
32+
};
2433

2534
// Dump configuration if this was asked
2635
if opts.dump_config {

src/models.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ pub trait DeviceConfig {
272272
}
273273

274274
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Validate)]
275-
#[serde(default)]
275+
#[serde(default, rename_all = "camelCase")]
276276
pub struct Dummy {
277277
#[validate(range(min = 1))]
278278
pub hardware_led_count: u32,
@@ -1374,6 +1374,8 @@ impl InstanceConfigCreator {
13741374

13751375
#[derive(Debug, Error)]
13761376
pub enum ConfigError {
1377+
#[error("i/o error: {0}")]
1378+
Io(#[from] std::io::Error),
13771379
#[error("error querying the database: {0}")]
13781380
Sqlx(#[from] sqlx::Error),
13791381
#[error("error loading instance: {0}")]
@@ -1387,6 +1389,8 @@ pub enum ConfigError {
13871389
// TODO: Say which setting?
13881390
#[error("missing hyperion_inst field on instance setting")]
13891391
MissingHyperionInst,
1392+
#[error("invalid JSON: {0}")]
1393+
Json(#[from] serde_json::Error),
13901394
}
13911395

13921396
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -1601,6 +1605,16 @@ impl Config {
16011605
})
16021606
}
16031607

1608+
pub async fn load_file(path: &std::path::Path) -> Result<Self, ConfigError> {
1609+
use tokio::io::AsyncReadExt;
1610+
1611+
let mut file = tokio::fs::File::open(path).await?;
1612+
let mut full = String::new();
1613+
file.read_to_string(&mut full).await?;
1614+
1615+
Ok(serde_json::from_str(&full)?)
1616+
}
1617+
16041618
pub fn uuid(&self) -> uuid::Uuid {
16051619
// There should always be a meta uuid
16061620
self.meta.first().map(|meta| meta.uuid).unwrap_or_default()

0 commit comments

Comments
 (0)