/
deploy.rs
179 lines (152 loc) · 5.04 KB
/
deploy.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
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
175
176
177
178
179
use std::{
fmt::{Display, Formatter},
path::PathBuf,
};
use anyhow::{anyhow, Result};
use dialoguer::{Confirm, Input, Select};
use serde::{Deserialize, Serialize};
use crate::utils::{
create_deployment, debug, get_function_config, info, print_progress, validate_code_file,
validate_public_dir, write_function_config, Config, DeploymentConfig, TrpcClient,
};
#[derive(Deserialize, Debug)]
struct Organization {
name: String,
id: String,
}
impl Display for Organization {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
type OrganizationsResponse = Vec<Organization>;
#[derive(Serialize, Debug)]
struct CreateFunctionRequest {
name: String,
domains: Vec<String>,
env: Vec<String>,
cron: Option<String>,
}
#[derive(Deserialize, Debug)]
struct CreateFunctionResponse {
id: String,
}
#[derive(Deserialize, Debug)]
struct Function {
id: String,
name: String,
}
impl Display for Function {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
type FunctionsResponse = Vec<Function>;
pub async fn deploy(
file: PathBuf,
client: Option<PathBuf>,
public_dir: Option<PathBuf>,
prod: bool,
) -> Result<()> {
let config = Config::new()?;
if config.token.is_none() {
return Err(anyhow!(
"You are not logged in. Please login with `lagon login`",
));
}
validate_code_file(&file)?;
let client = match client {
Some(client) => {
validate_code_file(&client)?;
Some(client)
}
None => None,
};
let public_dir = validate_public_dir(public_dir)?;
match get_function_config()? {
None => {
println!("{}", debug("No deployment config found..."));
println!();
let trpc_client = TrpcClient::new(&config);
let response = trpc_client
.query::<(), OrganizationsResponse>("organizationsList", None)
.await?;
let organizations = response.result.data;
let index = Select::new().items(&organizations).default(0).interact()?;
let organization = &organizations[index];
match Confirm::new()
.with_prompt(info("Link to an existing Function?"))
.interact()?
{
true => {
let response = trpc_client
.query::<(), FunctionsResponse>("functionsList", None)
.await?;
let index = Select::new()
.items(&response.result.data)
.default(0)
.interact()?;
let function = &response.result.data[index];
write_function_config(DeploymentConfig {
function_id: function.id.clone(),
organization_id: organization.id.clone(),
})?;
create_deployment(
function.id.clone(),
&file,
&client,
&public_dir,
&config,
prod,
)
.await?;
}
false => {
let name = Input::<String>::new()
.with_prompt(info("What is the name of this new Function?"))
.interact_text()?;
println!();
let message = format!("Creating Function {}...", name);
let end_progress = print_progress(&message);
let response = trpc_client
.mutation::<CreateFunctionRequest, CreateFunctionResponse>(
"functionCreate",
CreateFunctionRequest {
name,
domains: Vec::new(),
env: Vec::new(),
cron: None,
},
)
.await?;
end_progress();
write_function_config(DeploymentConfig {
function_id: response.result.data.id.clone(),
organization_id: organization.id.clone(),
})?;
create_deployment(
response.result.data.id,
&file,
&client,
&public_dir,
&config,
prod,
)
.await?;
}
};
Ok(())
}
Some(function_config) => {
create_deployment(
function_config.function_id,
&file,
&client,
&public_dir,
&config,
prod,
)
.await
}
}
}