cd_env/
api.rs

1use serde::{Deserialize, Serialize};
2use std::env;
3
4/// List of supported CD providers.
5#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
6pub enum CdProvider {
7    AwsCodedeploy,
8    DigitalOceanAppPlatform,
9    Fly,
10    GoCD,
11    GoogleAppEngine,
12    GoogleCloudRun,
13    Harness,
14    Heroku,
15    Netlify,
16    Octopus,
17    Railway,
18    Release,
19    Render,
20    Seed,
21    Vercel,
22    #[default]
23    Unknown,
24}
25
26// Other fields to maybe track: environment, url, deploy ID
27#[derive(Clone, Debug, Default, Deserialize, Serialize)]
28pub struct CdEnvironment {
29    /// Source branch that was deployed.
30    pub branch: Option<String>,
31
32    /// Prefix that all environment variables use.
33    pub env_prefix: Option<String>,
34
35    /// Name of the provider.
36    pub provider: CdProvider,
37
38    /// Revision (commit, sha, etc) that triggered the deploy.
39    pub revision: String,
40
41    /// Unique ID of the deployed service.
42    pub service_id: Option<String>,
43}
44
45pub fn var(key: &str) -> String {
46    env::var(key).unwrap_or_default()
47}
48
49pub fn opt_var(key: &str) -> Option<String> {
50    match env::var(key) {
51        Ok(value) => {
52            if value == "false" || value.is_empty() {
53                None
54            } else {
55                Some(value)
56            }
57        }
58        Err(_) => None,
59    }
60}