lezeh_deployment/
config.rs

1use std::collections::HashMap;
2use std::fs;
3use std::path::Path;
4
5use serde::Deserialize;
6use serde::Serialize;
7use thiserror::Error;
8
9use lezeh_common::types::ResultAnyError;
10
11/// Phab config
12/// -------------
13#[derive(Debug, Serialize, Deserialize, Clone)]
14pub struct PhabConfig {
15  pub host: String,
16  pub api_token: String,
17  pub pkcs12_path: String,
18  pub pkcs12_password: String,
19}
20
21/// Ghub config
22/// -------------
23#[derive(Debug, Serialize, Deserialize, Clone)]
24pub struct GhubConfig {
25  pub api_token: String,
26}
27
28#[derive(Debug, Serialize, Deserialize, Clone)]
29pub struct RepositoryConfig {
30  pub key: String,
31  pub path: String,
32  pub github_path: String, // For example: sendyhalim/foo
33  pub deployment_scheme_by_key: HashMap<String, DeploymentSchemeConfig>,
34}
35
36#[derive(Debug, Serialize, Deserialize, Clone)]
37pub struct DeploymentSchemeConfig {
38  pub name: String,
39  pub default_pull_request_title: String,
40  pub merge_from_branch: String,
41  pub merge_into_branch: String,
42}
43
44#[derive(Debug, Serialize, Deserialize, Clone)]
45pub struct MergeFeatureBranchesConfig {
46  pub output_template_path: Option<String>,
47}
48
49impl Default for MergeFeatureBranchesConfig {
50  fn default() -> Self {
51    return MergeFeatureBranchesConfig {
52      output_template_path: Some("merge_feature_branches_default.hbs".to_owned()),
53    };
54  }
55}
56
57#[derive(Debug, Serialize, Deserialize, Clone)]
58pub struct Config {
59  pub phab: PhabConfig,
60  pub ghub: GhubConfig,
61  pub repositories: Vec<RepositoryConfig>,
62  pub merge_feature_branches: Option<MergeFeatureBranchesConfig>,
63}
64
65impl Config {
66  pub fn from(setting_path: impl AsRef<Path> + std::fmt::Display) -> ResultAnyError<Config> {
67    let config_str = fs::read_to_string(&setting_path).map_err(|err| {
68      return ConfigError::ReadConfigError {
69        config_path: setting_path.to_string(),
70        root_err: format!("{:#?}", err),
71      };
72    })?;
73
74    let mut config: Config = serde_yaml::from_str(&config_str).map_err(|err| {
75      return ConfigError::ConfigDeserializeError {
76        config_path: setting_path.to_string(),
77        root_err: format!("{:#?}", err),
78      };
79    })?;
80
81    if config.merge_feature_branches.is_none() {
82      config.merge_feature_branches = Some(Default::default());
83    }
84
85    return Ok(config);
86  }
87}
88
89#[derive(Error, Debug)]
90pub enum ConfigError {
91  #[error("Failed reading config {config_path} err {root_err}")]
92  ReadConfigError {
93    config_path: String,
94    root_err: String,
95  },
96
97  #[error("Could not deserialize config please check {config_path} err {root_err}")]
98  ConfigDeserializeError {
99    config_path: String,
100    root_err: String,
101  },
102}