1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use config::Config as BaseConfig;
use config::File;
use config::FileFormat;
use serde::Deserialize;
use std::path::Path;

use crate::types::ResultDynError;

#[derive(Debug, Deserialize)]
pub struct PhabConfig {
  pub host: String,
  pub api_token: String,
  pub pkcs12_path: String,
  pub pkcs12_password: String,
}

#[derive(Debug, Deserialize)]
pub struct GhubConfig {
  pub api_token: String,
}

#[derive(Debug, Deserialize)]
pub struct RepositoryConfig {
  pub path: String,
  pub github_path: String, // For example: sendyhalim/foo
}

#[derive(Debug, Deserialize)]
pub struct DeploymentConfig {
  pub repositories: Vec<RepositoryConfig>,
}

#[derive(Debug, Deserialize)]
pub struct Config {
  pub phab: PhabConfig,
  pub ghub: GhubConfig,
  pub deployment: DeploymentConfig,
}

impl Config {
  pub fn new(setting_path: impl AsRef<Path>) -> ResultDynError<Config> {
    let mut c = BaseConfig::new();

    let file_config = File::new(setting_path.as_ref().to_str().unwrap(), FileFormat::Hjson);

    c.merge(file_config)?;

    return c.try_into().map_err(|err| {
      return failure::err_msg(err.to_string());
    });
  }
}