Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat: optionally load config from json file
Defelo committed 7 months ago
commit adde3f443ca5060b6abae159d7e6a3750cb02534
parent 0c95914
2 files changed +12 -5
modified src/bin/cib.rs
@@ -9,9 +9,7 @@ use std::{
};

use clap::Parser;
-

use radicle::Profile;
-

use radicle_ci_broker::{
    adapter::AdapterError,
    broker::{Broker, BrokerError},
@@ -58,7 +56,7 @@ fn fallible_main() -> Result<(), CibError> {
#[derive(Debug, Parser)]
#[command(version = env!("VERSION"))]
struct Args {
-
    /// Load configuration from this YAML file. (There is no default
+
    /// Load configuration from this YAML or JSON file. (There is no default
    /// location.)
    #[clap(long)]
    config: PathBuf,
modified src/config.rs
@@ -2,6 +2,7 @@

use std::{
    collections::HashMap,
+
    ffi::OsStr,
    path::{Path, PathBuf},
    time::Duration,
};
@@ -65,8 +66,12 @@ impl Config {
    pub fn load(filename: &Path) -> Result<Self, ConfigError> {
        let config =
            std::fs::read(filename).map_err(|e| ConfigError::ReadConfig(filename.into(), e))?;
-
        let config: Config = serde_norway::from_slice(&config)
-
            .map_err(|e| ConfigError::ParseConfig(filename.into(), e))?;
+
        let config: Config = match filename.extension().and_then(OsStr::to_str) {
+
            Some("json") => serde_json::from_slice(&config)
+
                .map_err(|e| ConfigError::ParseConfigJson(filename.into(), e))?,
+
            _ => serde_norway::from_slice(&config)
+
                .map_err(|e| ConfigError::ParseConfig(filename.into(), e))?,
+
        };
        config.check(filename)?;
        Ok(config)
    }
@@ -201,6 +206,10 @@ pub enum ConfigError {
    #[error("failed to parse configuration file as YAML: {0}")]
    ParseConfig(PathBuf, #[source] serde_norway::Error),

+
    /// Can't parse config file as JSON.
+
    #[error("failed to parse configuration file as JSON: {0}")]
+
    ParseConfigJson(PathBuf, #[source] serde_json::Error),
+

    /// Can't convert configuration into JSON.
    #[error("failed to convert configuration into JSON")]
    ToJson(#[source] serde_json::Error),