1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::path::{Path, PathBuf};
use std::{fs, io::ErrorKind, sync::LazyLock};

use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};

use super::config::Environment;
use crate::{platform, util};

static DATA_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
    [platform::data_dir(), Path::new("integration")]
        .into_iter()
        .collect()
});

pub struct EnvsDir {
    path: PathBuf,
}

#[derive(Deserialize, Serialize)]
pub struct State {
    pub active: String,
    pub config: Environment,
}

impl EnvsDir {
    pub fn new(integration: &str) -> Self {
        let config = format!("{integration}.json");
        let path = [&DATA_DIR, Path::new(&config)].iter().collect();
        Self { path }
    }

    /// Check if the named environment is active. If the current config could not be loaded or a
    /// different environment is active, an error is returned.
    pub fn check_active(&self, name: &str) -> Result<bool> {
        match self.active()? {
            None => Ok(false),
            Some(active) if active == name => Ok(true),
            Some(active) => Err(anyhow!(
                "Requested environment {name:?} does not match active one {active:?}"
            )),
        }
    }

    /// Return the currently active environment name.
    pub fn active(&self) -> Result<Option<String>> {
        self.load().map(|state| state.map(|config| config.active))
    }

    /// Load the currently active state data.
    pub fn load(&self) -> Result<Option<State>> {
        let json = match fs::read_to_string(&self.path) {
            Ok(json) => json,
            Err(error) if error.kind() == ErrorKind::NotFound => return Ok(None),
            Err(error) => {
                return Err(error).context(format!("Could not read state file {:?}", self.path))
            }
        };
        let state: State = serde_json::from_str(&json)
            .with_context(|| format!("Could not parse state file {:?}", self.path))?;
        Ok(Some(state))
    }

    pub fn save(&self, environment: &str, config: &Environment) -> Result<()> {
        let config = State {
            active: environment.into(),
            config: config.clone(),
        };
        let path = &*DATA_DIR;
        if !path.is_dir() {
            fs::create_dir_all(path)
                .with_context(|| format!("failed to create directory {path:?}"))?;
        }

        let config = serde_json::to_string(&config)?;
        fs::write(&self.path, config)
            .with_context(|| format!("failed to write file {:?}", self.path))
    }

    pub fn remove(&self) -> Result<()> {
        if util::exists(&self.path)? {
            fs::remove_file(&self.path)
                .with_context(|| format!("failed to remove {:?}", self.path))?;
        }

        Ok(())
    }
}