depl 2.4.3

Toolkit for a bunch of local and remote CI/CD actions
Documentation
//! Run environment module.
//!
//! `RunEnvironment` is just a struct that have all needed variables
//! and paths during pipeline execution.

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::{Arc, atomic::AtomicBool};

use crate::entities::daemons::Daemons;
use crate::entities::driver::PipelineDriver;
use crate::entities::info::ShortName;
use crate::entities::observe_executor::ObserveClient;
use crate::entities::placements::Placement;
use crate::entities::remote_host::RemoteHost;
use crate::entities::skipper::Skipper;

/// Run environment.
pub struct RunEnvironment<'a> {
  /// Actual master pipeline name.
  pub master_pipeline: &'a str,
  /// Actual run folder. This is where Deployer runs all the actions.
  pub run_dir: &'a Path,
  /// System-wide or local common cache folder (usually `~/.cache`).
  pub cache_dir: &'a Path,
  /// System-wide or local common configuration folder (usually `~/.config`).
  #[allow(dead_code)]
  pub config_dir: &'a Path,
  /// Project folder.
  pub project_dir: Option<&'a Path>,
  /// Deployer storage folder.
  pub storage_dir: &'a Path,
  /// Artifacts folder (usually `%project_dir%/artifacts`).
  pub artifacts_dir: &'a Path,
  /// Artifacts placements.
  pub artifacts_placements: &'a [Placement],
  /// Remote hosts's Registry. Used to find hosts by short name.
  pub remotes: &'a BTreeMap<ShortName, RemoteHost>,
  /// What files you should ignore when running pipelines remotely.
  pub ignore: &'a BTreeSet<PathBuf>,
  /// Log file.
  pub log_file: Option<&'a Path>,
  /// New run flag. Pipeline execution will run all actions.
  pub new_build: bool,
  /// Is build environment containered?
  pub containered_build: bool,
  /// Is run environment containered?
  pub containered_run: bool,
  /// Is run environment under Ansible?
  pub ansible_run: bool,
  /// Current running daemons.
  pub daemons: Daemons,
  /// Observe actions executor client.
  pub observe_cli: &'a Option<ObserveClient>,
  /// Skipper to count actions to skip (see `--skip` option for `deployer run/watch`).
  pub skipper: Skipper,
  /// Pipeline driver.
  pub driver: PipelineDriver,
  /// Signal that restart is requested (used in watch mode).
  pub restart_requested: Option<Arc<AtomicBool>>,
}

/// Runs a closure with empty environment.
pub async fn with_empty_env<T>(f: impl AsyncFn(RunEnvironment<'_>) -> T) -> T {
  let empty_path = PathBuf::from(".");
  let empty_str = String::new();
  let empty_remotes = BTreeMap::new();
  let empty_ignore = BTreeSet::new();

  let env = RunEnvironment {
    run_dir: &empty_path,
    config_dir: &empty_path,
    new_build: false,
    cache_dir: &empty_path,
    project_dir: None,
    storage_dir: &empty_path,
    artifacts_dir: &empty_path,
    artifacts_placements: &[],
    remotes: &empty_remotes,
    ignore: &empty_ignore,
    log_file: None,
    containered_build: false,
    containered_run: false,
    master_pipeline: &empty_str,
    ansible_run: false,
    daemons: Default::default(),
    observe_cli: &None,
    skipper: Default::default(),
    driver: PipelineDriver::Deployer,
    restart_requested: None,
  };

  f(env).await
}