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
165
166
167
168
169
170
171
172
173
174
pub(crate) mod render;
use anyhow::Context;
use colored::Colorize;
use std::{
    env::current_dir,
    path::{Path, PathBuf},
    str::FromStr,
};
use wasmer_backend_api::{types::Secret as BackendSecret, WasmerClient};

use crate::commands::app::util::{get_app_config_from_dir, prompt_app_ident, AppIdent};

#[derive(serde::Serialize, serde::Deserialize)]
pub(super) struct Secret {
    pub name: String,
    pub value: String,
}

pub(super) async fn read_secrets_from_file(path: &Path) -> anyhow::Result<Vec<Secret>> {
    let mut ret = vec![];
    for item in dotenvy::from_path_iter(path)? {
        let (name, value) = item?;
        ret.push(Secret { name, value })
    }
    Ok(ret)
}

pub(super) async fn get_secret_by_name(
    client: &WasmerClient,
    app_id: &str,
    secret_name: &str,
) -> anyhow::Result<Option<BackendSecret>> {
    wasmer_backend_api::query::get_app_secret_by_name(client, app_id, secret_name).await
}
pub(crate) async fn get_secrets(
    client: &WasmerClient,
    app_id: &str,
) -> anyhow::Result<Vec<wasmer_backend_api::types::Secret>> {
    wasmer_backend_api::query::get_all_app_secrets(client, app_id).await
}

pub(crate) async fn get_secret_value(
    client: &WasmerClient,
    secret: &wasmer_backend_api::types::Secret,
) -> anyhow::Result<String> {
    wasmer_backend_api::query::get_app_secret_value_by_id(client, secret.id.clone().into_inner())
        .await?
        .ok_or_else(|| {
            anyhow::anyhow!(
                "No value found for secret with name '{}'",
                secret.name.bold()
            )
        })
}

pub(crate) async fn get_secret_value_by_name(
    client: &WasmerClient,
    app_id: &str,
    secret_name: &str,
) -> anyhow::Result<String> {
    match get_secret_by_name(client, app_id, secret_name).await? {
        Some(secret) => get_secret_value(client, &secret).await,
        None => anyhow::bail!("No secret found with name {secret_name} for app {app_id}"),
    }
}

pub(crate) async fn reveal_secrets(
    client: &WasmerClient,
    app_id: &str,
) -> anyhow::Result<Vec<Secret>> {
    let secrets = wasmer_backend_api::query::get_all_app_secrets(client, app_id).await?;
    let mut ret = vec![];
    for secret in secrets {
        let name = secret.name.clone();
        let value = get_secret_value(client, &secret).await?;
        ret.push(Secret { name, value });
    }

    Ok(ret)
}

/// Utility struct used just to implement [`CliRender`].
#[derive(Debug, serde::Serialize)]
pub(super) struct BackendSecretWrapper(pub BackendSecret);

impl From<BackendSecret> for BackendSecretWrapper {
    fn from(value: BackendSecret) -> Self {
        Self(value)
    }
}

/// A secrets-specific app to retrieve an app identifier.
pub(super) async fn get_app_id(
    client: &WasmerClient,
    app: Option<&AppIdent>,
    app_dir_path: Option<&PathBuf>,
    quiet: bool,
    non_interactive: bool,
) -> anyhow::Result<String> {
    if let Some(app_id) = app {
        let app = app_id.resolve(client).await?;
        return Ok(app.id.into_inner());
    }

    let path = if let Some(path) = app_dir_path {
        path.clone()
    } else {
        current_dir()?
    };

    if let Ok(r) = get_app_config_from_dir(&path) {
        let (app, _) = r;

        let app_name = if let Some(owner) = &app.owner {
            format!(
                "{owner}/{}",
                &app.name.clone().context("App name has to be specified")?
            )
        } else {
            app.name
                .clone()
                .context("App name has to be specified")?
                .to_string()
        };

        let id = if let Some(id) = &app.app_id {
            Some(id.clone())
        } else if let Ok(app_ident) = AppIdent::from_str(&app_name) {
            if let Ok(app) = app_ident.resolve(client).await {
                Some(app.id.into_inner())
            } else {
                if !quiet {
                    eprintln!("{}: the app found in {} does not exist.\n{}: maybe it was not deployed yet?", 
                            "Warning".bold().yellow(), 
                            format!("'{}'", path.display()).dimmed(), 
                            "Hint".bold());
                }
                None
            }
        } else {
            None
        };

        if let Some(id) = id {
            if !quiet {
                if let Some(owner) = &app.owner {
                    eprintln!(
                        "Managing secrets related to app {} ({owner}).",
                        app.name.context("App name has to be specified")?.bold()
                    );
                } else {
                    eprintln!(
                        "Managing secrets related to app {}.",
                        app.name.context("App name has to be specified")?.bold()
                    );
                }
            }
            return Ok(id);
        }
    } else if let Some(path) = app_dir_path {
        anyhow::bail!(
            "No app configuration file found in path {}.",
            path.display()
        )
    }

    if non_interactive {
        anyhow::bail!("No app id given. Provide one using the `--app` flag.")
    } else {
        let id = prompt_app_ident("Enter the name of the app")?;
        let app = id.resolve(client).await?;
        Ok(app.id.into_inner())
    }
}