-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvariables.rs
127 lines (116 loc) · 3.61 KB
/
variables.rs
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
use anyhow::{Context, Result};
use clap::Parser;
use cloud::{client::Client as CloudClient, CloudClientInterface};
use serde::Deserialize;
use serde_json::from_str;
use spin_common::arg_parser::parse_kv;
use uuid::Uuid;
use crate::commands::{client_and_app_id, CommonArgs};
#[derive(Deserialize)]
pub(crate) struct Variable {
pub key: String,
}
/// Manage Spin application variables
#[derive(Parser, Debug)]
#[clap(about = "Manage Spin application variables")]
pub enum VariablesCommand {
/// Set variables
Set(SetCommand),
/// Delete variables
Delete(DeleteCommand),
/// List all variables of an application
List(ListCommand),
}
#[derive(Parser, Debug)]
pub struct SetCommand {
/// Variable pair to set
#[clap(parse(try_from_str = parse_kv))]
pub variables_to_set: Vec<(String, String)>,
#[clap(flatten)]
common: CommonArgs,
/// Name of Spin app
#[clap(name = "app", long = "app")]
pub app: String,
}
#[derive(Parser, Debug)]
pub struct DeleteCommand {
/// Variable pair to set
pub variables_to_delete: Vec<String>,
#[clap(flatten)]
common: CommonArgs,
/// Name of Spin app
#[clap(name = "app", long = "app")]
pub app: String,
}
#[derive(Parser, Debug)]
pub struct ListCommand {
#[clap(flatten)]
common: CommonArgs,
/// Name of Spin app
#[clap(name = "app", long = "app")]
pub app: String,
}
impl VariablesCommand {
pub async fn run(self) -> Result<()> {
match self {
Self::Set(cmd) => {
let (client, app_id) =
client_and_app_id(cmd.common.deployment_env_id.as_deref(), &cmd.app).await?;
set_variables(&client, app_id, &cmd.variables_to_set).await?;
}
Self::Delete(cmd) => {
let (client, app_id) =
client_and_app_id(cmd.common.deployment_env_id.as_deref(), &cmd.app).await?;
delete_variables(&client, app_id, &cmd.variables_to_delete).await?;
}
Self::List(cmd) => {
let (client, app_id) =
client_and_app_id(cmd.common.deployment_env_id.as_deref(), &cmd.app).await?;
let var_names = get_variables(&client, app_id).await?;
for v in var_names {
println!("{}", v.key);
}
}
}
Ok(())
}
}
pub(crate) async fn set_variables(
client: &CloudClient,
app_id: Uuid,
variables: &[(String, String)],
) -> Result<()> {
for var in variables {
CloudClient::add_variable_pair(client, app_id, var.0.to_owned(), var.1.to_owned())
.await
.with_context(|| format!("Problem creating variable {}", var.0))?;
}
Ok(())
}
pub(crate) async fn delete_variables(
client: &CloudClient,
app_id: Uuid,
variables: &[String],
) -> Result<()> {
for var in variables {
CloudClient::delete_variable_pair(client, app_id, var.to_owned())
.await
.with_context(|| format!("Problem deleting variable {var}"))?;
}
Ok(())
}
async fn get_variables_json(client: &CloudClient, app_id: Uuid) -> Result<Vec<String>> {
let vars = CloudClient::get_variable_pairs(client, app_id)
.await
.context("Problem listing variables")?;
Ok(vars)
}
pub(crate) async fn get_variables(client: &CloudClient, app_id: Uuid) -> Result<Vec<Variable>> {
let vars = get_variables_json(client, app_id).await?;
let var_names = vars
.iter()
.map(|var| from_str(var))
.collect::<Result<Vec<Variable>, _>>()
.context("could not parse variable")?;
Ok(var_names)
}