Skip to main content

autoschematic_connector_github/
config.rs

1use autoschematic_core::{connector::Resource, macros::FieldTypes, util::RON};
2use autoschematic_macros::FieldTypes;
3use documented::{Documented, DocumentedFields};
4use ron::ser::PrettyConfig;
5use serde::{Deserialize, Serialize};
6use std::path::Path;
7
8// #[derive(Debug, Serialize, Deserialize, Clone)]
9// pub enum GithubRateLimitStrategy {
10//     Conservative,
11//     Aggressive,
12// }
13
14#[derive(Debug, Serialize, Deserialize, Clone)]
15pub enum GithubRepositoryOwner {
16    User(String),
17    Organization(String),
18}
19
20#[derive(Debug, PartialEq, Serialize, Deserialize, Documented, DocumentedFields, Clone, FieldTypes)]
21#[serde(deny_unknown_fields)]
22/// The primary configuration block for the GithubConnector.
23pub struct GitHubConnectorConfig {
24    /// A list of organization slugs that this connector should try and connect to and work with.
25    pub orgs: Vec<String>,
26    /// A list of user logins that this connector should manage resources under.
27    pub users: Vec<String>,
28    /// If using Github enterprise, the url for the enterprise
29    pub enterprise_url: Option<String>,
30    /// The number of requests to make in parallel. Defaults to 5.
31    pub concurrent_requests: usize,
32}
33
34impl Default for GitHubConnectorConfig {
35    fn default() -> Self {
36        Self {
37            orgs: Vec::new(),
38            users: Vec::new(),
39            enterprise_url: None,
40            concurrent_requests: 5,
41        }
42    }
43}
44
45impl GitHubConnectorConfig {
46    pub fn try_load(prefix: &Path) -> anyhow::Result<Option<Self>> {
47        let config_path = prefix.join("github").join("config.ron");
48
49        if !config_path.exists() {
50            return Ok(None);
51        }
52
53        let config_str = std::fs::read_to_string(&config_path)?;
54        let config: GitHubConnectorConfig = RON.from_str(&config_str)?;
55        Ok(Some(config))
56    }
57}
58
59impl Resource for GitHubConnectorConfig {
60    fn to_bytes(&self) -> anyhow::Result<Vec<u8>> {
61        Ok(RON.to_string_pretty(self, PrettyConfig::default())?.into())
62    }
63
64    fn from_bytes(_addr: &impl autoschematic_core::connector::ResourceAddress, s: &[u8]) -> anyhow::Result<Self>
65    where
66        Self: Sized,
67    {
68        Ok(RON.from_str(str::from_utf8(s)?)?)
69    }
70}