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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::fs;

use anyhow::Error;
use anyhow::{anyhow, bail};
use serde::Deserialize;
use serde::Serialize;

use crate::tui;

// TODO: Move this to a template file
pub const CONTEXT_DEFAULT_TEXT: &str = "version = \"1.0\"

[[environment]]
name = 'local'
target = 'docker'
set = true

[[environment]]
name = 'prod'
target = 'tembo-cloud'
org_id = 'ORG_ID'
profile = 'prod'
";

// TODO: Move this to a template file
pub const CREDENTIALS_DEFAULT_TEXT: &str = "version = \"1.0\"

[[profile]]
name = 'prod'
tembo_access_token = 'ACCESS_TOKEN'
tembo_host = 'https://api.tembo.io'
tembo_data_host = 'https://api.data-1.use1.tembo.io'
";

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Context {
    pub version: String,
    pub environment: Vec<Environment>,
}

// Config struct holds to data from the `[config]` section.
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Environment {
    pub name: String,
    pub target: String,
    pub org_id: Option<String>,
    pub profile: Option<String>,
    pub set: Option<bool>,
    pub selected_profile: Option<Profile>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Credential {
    pub version: String,
    pub profile: Vec<Profile>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Profile {
    pub name: String,
    pub tembo_access_token: String,
    pub tembo_host: String,
    pub tembo_data_host: String,
}

pub enum Target {
    Docker,
    TemboCloud,
}

impl ToString for Target {
    fn to_string(&self) -> String {
        match self {
            Self::Docker => String::from("docker"),
            Self::TemboCloud => String::from("tembo-cloud"),
        }
    }
}

pub fn tembo_home_dir() -> String {
    let mut tembo_home = home::home_dir().unwrap().as_path().display().to_string();
    tembo_home.push_str("/.tembo");
    tembo_home
}

pub fn tembo_context_file_path() -> String {
    tembo_home_dir() + "/context"
}

pub fn tembo_credentials_file_path() -> String {
    tembo_home_dir() + "/credentials"
}

pub fn list_context() -> Result<Option<Context>, anyhow::Error> {
    let filename = tembo_context_file_path();

    let contents = fs::read_to_string(&filename)
        .map_err(|err| anyhow!("Error reading file {filename}: {err}"))?;

    let maybe_context: Result<Context, toml::de::Error> = toml::from_str(&contents);

    match maybe_context {
        Ok(context) => Ok(Some(context)),
        Err(err) => {
            eprintln!("\nInvalid context file {filename}\n");

            tui::error(&format!("Error: {}", err.message()));

            eprintln!("\nExample context file: \n\n{}", CONTEXT_DEFAULT_TEXT);

            Err(Error::msg("Error listing tembo context!"))
        }
    }
}

pub fn get_current_context() -> Result<Environment, anyhow::Error> {
    let maybe_context = list_context()?;

    if let Some(context) = maybe_context {
        let maybe_profiles = list_credential_profiles()?;

        if let Some(profiles) = maybe_profiles {
            for mut env in context.environment {
                if let Some(_is_set) = env.set {
                    if let Some(profile) = &env.profile {
                        let credential =
                            profiles.iter().rev().find(|c| &c.name == profile).unwrap();

                        env.selected_profile = Some(credential.to_owned());
                    }

                    return Ok(env);
                }
            }
        }
    }

    bail!("Tembo context not set");
}

pub fn list_credential_profiles() -> Result<Option<Vec<Profile>>, anyhow::Error> {
    let filename = tembo_credentials_file_path();

    let contents = fs::read_to_string(&filename)
        .map_err(|err| anyhow!("Error reading file {filename}: {err}"))?;

    let maybe_credential: Result<Credential, toml::de::Error> = toml::from_str(&contents);

    match maybe_credential {
        Ok(credential) => Ok(Some(credential.profile)),
        Err(err) => {
            eprintln!("\nInvalid credentials file {filename}\n");

            tui::error(&format!("Error: {}", err.message()));

            eprintln!(
                "\nExample credentials file: \n\n{}",
                CREDENTIALS_DEFAULT_TEXT
            );

            Err(Error::msg("Error listing tembo credentials profiles!"))
        }
    }
}