-
Notifications
You must be signed in to change notification settings - Fork 242
/
args.rs
293 lines (270 loc) · 8.58 KB
/
args.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use std::{
ffi::OsStr,
fs::{canonicalize, create_dir_all},
io::{self, ErrorKind},
path::PathBuf,
};
use clap::Parser;
use clap_complete::Shell;
use shuttle_common::project::ProjectName;
use uuid::Uuid;
use crate::init::Framework;
#[derive(Parser)]
#[clap(
version,
about,
// Cargo passes in the subcommand name to the invoked executable. Use a
// hidden, optional positional argument to deal with it.
arg(clap::Arg::with_name("dummy")
.possible_value("shuttle")
.required(false)
.hidden(true))
)]
pub struct Args {
/// run this command against the api at the supplied url
/// (allows targeting a custom deployed instance for this command only)
#[clap(long, env = "SHUTTLE_API")]
pub api_url: Option<String>,
#[clap(flatten)]
pub project_args: ProjectArgs,
#[clap(subcommand)]
pub cmd: Command,
}
// Common args for subcommands that deal with projects.
#[derive(Parser, Debug)]
pub struct ProjectArgs {
/// Specify the working directory
#[clap(
global = true,
long,
parse(try_from_os_str = parse_path),
default_value = ".",
)]
pub working_directory: PathBuf,
/// Specify the name of the project (overrides crate name)
#[clap(global = true, long)]
pub name: Option<ProjectName>,
}
#[derive(Parser)]
pub enum Command {
/// deploy a shuttle service
Deploy(DeployArgs),
/// manage deployments of a shuttle service
#[clap(subcommand)]
Deployment(DeploymentCommand),
/// create a new shuttle service
Init(InitArgs),
/// generate shell completions
Generate {
/// which shell
#[clap(short, long, env, default_value_t = Shell::Bash)]
shell: Shell,
/// output to file or stdout by default
#[clap(short, long, env)]
output: Option<PathBuf>,
},
/// view the status of a shuttle service
Status,
/// view the logs of a deployment in this shuttle service
Logs {
/// Deployment ID to get logs for. Defaults to currently running deployment
id: Option<Uuid>,
#[clap(short, long)]
/// Follow log output
follow: bool,
},
/// delete this shuttle service
Delete,
/// manage secrets for this shuttle service
Secrets,
/// create user credentials for the shuttle platform
Auth(AuthArgs),
/// login to the shuttle platform
Login(LoginArgs),
/// run a shuttle service locally
Run(RunArgs),
/// manage a project on shuttle
#[clap(subcommand)]
Project(ProjectCommand),
}
#[derive(Parser)]
pub enum DeploymentCommand {
/// list all the deployments for a service
List,
/// view status of a deployment
Status {
/// ID of deployment to get status for
id: Uuid,
},
}
#[derive(Parser)]
pub enum ProjectCommand {
/// create an environment for this project on shuttle
New,
/// remove this project environment from shuttle
Rm,
/// show the status of this project's environment on shuttle
Status,
}
#[derive(Parser, Clone, Debug)]
pub struct LoginArgs {
/// api key for the shuttle platform
#[clap(long)]
pub api_key: Option<String>,
}
#[derive(Parser)]
pub struct AuthArgs {
/// the desired username for the shuttle platform
#[clap()]
pub username: String,
}
#[derive(Parser)]
pub struct DeployArgs {
/// allow dirty working directories to be packaged
#[clap(long)]
pub allow_dirty: bool,
/// allows pre-deploy tests to be skipped
#[clap(long)]
pub no_test: bool,
}
#[derive(Parser, Debug)]
pub struct RunArgs {
/// port to start service on
#[clap(long, default_value = "8000")]
pub port: u16,
}
#[derive(Parser, Debug)]
pub struct InitArgs {
/// Initialize with axum framework
#[clap(long, conflicts_with_all = &["rocket", "tide", "tower", "poem", "serenity", "warp", "salvo", "thruster"])]
pub axum: bool,
/// Initialize with rocket framework
#[clap(long, conflicts_with_all = &["axum", "tide", "tower", "poem", "serenity", "warp", "salvo", "thruster"])]
pub rocket: bool,
/// Initialize with tide framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tower", "poem", "serenity", "warp", "salvo", "thruster"])]
pub tide: bool,
/// Initialize with tower framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tide", "poem", "serenity", "warp", "salvo", "thruster"])]
pub tower: bool,
/// Initialize with poem framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tide", "tower", "serenity", "warp", "salvo", "thruster"])]
pub poem: bool,
/// Initialize with salvo framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tide", "tower", "poem", "warp", "serenity", "thruster"])]
pub salvo: bool,
/// Initialize with serenity framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tide", "tower", "poem", "warp", "salvo", "thruster"])]
pub serenity: bool,
/// Initialize with warp framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tide", "tower", "poem", "serenity", "salvo", "thruster"])]
pub warp: bool,
/// Initialize with thruster framework
#[clap(long, conflicts_with_all = &["axum", "rocket", "tide", "tower", "poem", "warp", "salvo", "serenity"])]
pub thruster: bool,
/// Whether to create the environment for this project on Shuttle
#[clap(long)]
pub new: bool,
#[clap(flatten)]
pub login_args: LoginArgs,
/// Path to initialize a new shuttle project
#[clap(
parse(try_from_os_str = parse_init_path),
default_value = ".",
)]
pub path: PathBuf,
}
impl InitArgs {
pub fn framework(&self) -> Option<Framework> {
if self.axum {
Some(Framework::Axum)
} else if self.rocket {
Some(Framework::Rocket)
} else if self.tide {
Some(Framework::Tide)
} else if self.tower {
Some(Framework::Tower)
} else if self.poem {
Some(Framework::Poem)
} else if self.salvo {
Some(Framework::Salvo)
} else if self.serenity {
Some(Framework::Serenity)
} else if self.warp {
Some(Framework::Warp)
} else if self.thruster {
Some(Framework::Thruster)
} else {
None
}
}
}
// Helper function to parse and return the absolute path
fn parse_path(path: &OsStr) -> Result<PathBuf, io::Error> {
canonicalize(path).map_err(|e| {
io::Error::new(
ErrorKind::InvalidInput,
format!("could not turn {path:?} into a real path: {e}"),
)
})
}
// Helper function to parse, create if not exists, and return the absolute path
fn parse_init_path(path: &OsStr) -> Result<PathBuf, io::Error> {
// Create the directory if does not exist
create_dir_all(path)?;
parse_path(path)
}
#[cfg(test)]
mod tests {
use super::*;
fn init_args_factory(framework: &str) -> InitArgs {
let mut init_args = InitArgs {
axum: false,
rocket: false,
tide: false,
tower: false,
poem: false,
salvo: false,
serenity: false,
warp: false,
thruster: false,
new: false,
login_args: LoginArgs { api_key: None },
path: PathBuf::new(),
};
match framework {
"axum" => init_args.axum = true,
"rocket" => init_args.rocket = true,
"tide" => init_args.tide = true,
"tower" => init_args.tower = true,
"poem" => init_args.poem = true,
"salvo" => init_args.salvo = true,
"serenity" => init_args.serenity = true,
"warp" => init_args.warp = true,
"thruster" => init_args.thruster = true,
_ => unreachable!(),
}
init_args
}
#[test]
fn test_init_args_framework() {
let framework_strs = vec![
"axum", "rocket", "tide", "tower", "poem", "salvo", "serenity", "warp", "thruster",
];
let frameworks: Vec<Framework> = vec![
Framework::Axum,
Framework::Rocket,
Framework::Tide,
Framework::Tower,
Framework::Poem,
Framework::Salvo,
Framework::Serenity,
Framework::Warp,
Framework::Thruster,
];
for (framework_str, expected_framework) in framework_strs.into_iter().zip(frameworks) {
let framework = init_args_factory(framework_str).framework();
assert_eq!(framework, Some(expected_framework));
}
}
}