coodev-runner 0.1.42

A simple runner for coodev
Documentation
use crate::error;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RepositorySecret {
  pub key: String,
  pub value: String,
  pub repository: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OrganizationSecret {
  pub key: String,
  pub value: String,
  pub organization: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GlobalSecret {
  pub key: String,
  pub value: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Secret {
  Repository(RepositorySecret),
  Organization(OrganizationSecret),
  Global(GlobalSecret),
}

pub struct SecretBuilder {
  pub key: String,
  pub owner: Option<String>,
  pub repository: Option<String>,
  pub value: Option<String>,
}

impl SecretBuilder {
  pub fn repository(&mut self, repository: impl Into<String>) -> &mut Self {
    self.repository = Some(repository.into());

    self
  }

  pub fn owner(&mut self, owner: impl Into<String>) -> &mut Self {
    self.owner = Some(owner.into());

    self
  }

  pub fn value(&mut self, value: impl Into<String>) -> &mut Self {
    self.value = Some(value.into());

    self
  }

  pub fn build(&self) -> crate::Result<Secret> {
    let key = self.key.clone();
    let value = self.value.clone().ok_or({
      error::Error::workflow_config_error(
        "Secret value is required. Please set secret value by Secret::new(key).value(value)",
      )
    })?;

    match (self.owner.clone(), self.repository.clone()) {
      (Some(owner), Some(repository)) => Ok(Secret::Repository(RepositorySecret {
        key,
        value,
        repository: format!("{}/{}", owner, repository),
      })),
      (Some(owner), None) => Ok(Secret::Organization(OrganizationSecret {
        key,
        value,
        organization: owner,
      })),
      (None, None) => Ok(Secret::Global(GlobalSecret { key, value })),
      (None, Some(_)) => Err(error::Error::workflow_config_error(
        "Repository owner is required. Please set repository owner by Secret::new(key).owner(owner)"
      )),
    }
  }
}

impl Secret {
  pub fn new(key: impl Into<String>) -> SecretBuilder {
    SecretBuilder {
      key: key.into(),
      owner: None,
      repository: None,
      value: None,
    }
  }
}