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
294
295
296
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(name = "clever-project", version, about = "Sync a project description with Clever Cloud", long_about = None)]
pub struct Cli {
/// Verbose output
#[arg(short, long, global = true)]
pub verbose: bool,
#[command(subcommand)]
pub command: Command,
}
#[derive(Debug, Subcommand)]
pub enum Command {
/// Read resources from a Clever Cloud org into a project file
Read(ReadArgs),
/// Create or update resources from a project file
Apply(ApplyArgs),
/// Delete resources listed in a project file
Delete(DeleteArgs),
/// Validate a project file (syntax, variables, dependencies, sizing,
/// kinds, regions). Doesn't modify anything.
Check(CheckArgs),
/// Compare a project file against the live Clever Cloud org and report
/// any drift. Read-only; doesn't modify anything.
Status(StatusArgs),
/// Scaffold a new project file by asking a few questions (or via flags
/// in non-interactive mode).
Init(InitArgs),
}
#[derive(Debug, Args)]
pub struct ReadArgs {
/// Target organisation (overrides project file)
#[arg(long)]
pub org: String,
/// App name to read (can be repeated)
#[arg(long = "app")]
pub apps: Vec<String>,
/// Addon name to read (can be repeated)
#[arg(long = "addon")]
pub addons: Vec<String>,
/// Read every app and addon in the org
#[arg(long, conflicts_with_all = ["apps", "addons"])]
pub all: bool,
/// Output file path (.yaml/.yml/.json)
#[arg(short = 'o', long)]
pub output: PathBuf,
}
#[derive(Debug, Args)]
pub struct ApplyArgs {
/// Project file path (.yaml/.yml/.json). If omitted, looks for
/// `project.clever.yaml`, `.yml` or `.json` in the current directory.
pub file: Option<PathBuf>,
/// Override the organisation defined in the project file
#[arg(long)]
pub org: Option<String>,
/// Override the default region defined in the project file
#[arg(long)]
pub region: Option<String>,
/// Value for the special variable `${env}` (default `prod`)
#[arg(long)]
pub env: Option<String>,
/// Set a variable (key=value). Overrides values from the project file
/// and from --variable-path.
#[arg(long = "variable", value_parser = parse_kv)]
pub variables: Vec<(String, String)>,
/// Load variable overrides from a YAML/JSON file (flat key/value
/// mapping). Can be repeated; later files override earlier ones, and
/// --variable beats anything from these files.
#[arg(long = "variable-path")]
pub variable_paths: Vec<PathBuf>,
/// Explicit path to a secrets file. When omitted, secrets are
/// auto-discovered next to the project file (`<stem>.secrets` and
/// `<stem>.<env>.secrets`).
#[arg(long)]
pub secrets_path: Option<PathBuf>,
/// Plan only: read current state and log what would change without
/// mutating anything on Clever Cloud.
#[arg(long)]
pub dry_run: bool,
/// Skip the interactive confirmation prompt. Required when stdin is not
/// a TTY (CI environments, piped invocations).
#[arg(long, alias = "auto-approve")]
pub yes: bool,
/// Restrict the run to one or more resources. Repeatable. Syntax:
/// `apps.KEY`, `addons.KEY`, or `network_groups.KEY` (also `app.`,
/// `addon.`, `ng.` as shorter aliases). Without any `--target`, the
/// whole project is processed (the default).
#[arg(long = "target", value_parser = crate::commands::targets::parse_target_arg)]
pub targets: Vec<(crate::commands::targets::TargetKind, String)>,
}
#[derive(Debug, Args)]
pub struct CheckArgs {
/// Project file path (.yaml/.yml/.json). If omitted, looks for
/// `project.clever.yaml`, `.yml` or `.json` in the current directory.
pub file: Option<PathBuf>,
/// Override the organisation defined in the project file
#[arg(long)]
pub org: Option<String>,
/// Override the default region defined in the project file
#[arg(long)]
pub region: Option<String>,
/// Value for the special variable `${env}` (default `prod`)
#[arg(long)]
pub env: Option<String>,
/// Set a variable (key=value). Overrides values from the project file
/// and from --variable-path.
#[arg(long = "variable", value_parser = parse_kv)]
pub variables: Vec<(String, String)>,
/// Load variable overrides from a YAML/JSON file (repeatable).
#[arg(long = "variable-path")]
pub variable_paths: Vec<PathBuf>,
/// Explicit path to a secrets file.
#[arg(long)]
pub secrets_path: Option<PathBuf>,
/// Skip live validation against Clever's API (addon catalog, app
/// instance flavors). Useful in CI environments without `clever login`.
/// Static validation (syntax, variables, kinds, regions, dependencies,
/// uniqueness) is always performed.
#[arg(long)]
pub offline: bool,
}
#[derive(Debug, Args)]
pub struct StatusArgs {
/// Project file path (.yaml/.yml/.json). If omitted, looks for
/// `project.clever.yaml`, `.yml` or `.json` in the current directory.
pub file: Option<PathBuf>,
/// Override the organisation defined in the project file
#[arg(long)]
pub org: Option<String>,
/// Override the default region defined in the project file
#[arg(long)]
pub region: Option<String>,
/// Value for the special variable `${env}` (default `prod`)
#[arg(long)]
pub env: Option<String>,
/// Set a variable (key=value). Overrides values from the project file
/// and from --variable-path.
#[arg(long = "variable", value_parser = parse_kv)]
pub variables: Vec<(String, String)>,
/// Load variable overrides from a YAML/JSON file (repeatable).
#[arg(long = "variable-path")]
pub variable_paths: Vec<PathBuf>,
/// Explicit path to a secrets file.
#[arg(long)]
pub secrets_path: Option<PathBuf>,
/// Hide resources that are perfectly in sync; only show drift.
#[arg(long)]
pub brief: bool,
/// Exit with code 1 if any drift, orphan, or pending creation is found.
/// Useful in CI checks.
#[arg(long)]
pub exit_on_drift: bool,
}
#[derive(Debug, Args)]
pub struct DeleteArgs {
/// Project file path (.yaml/.yml/.json). If omitted, looks for
/// `project.clever.yaml`, `.yml` or `.json` in the current directory.
pub file: Option<PathBuf>,
/// Override the organisation defined in the project file
#[arg(long)]
pub org: Option<String>,
/// Override the default region defined in the project file
#[arg(long)]
pub region: Option<String>,
/// Value for the special variable `${env}` (default `prod`)
#[arg(long)]
pub env: Option<String>,
/// Set a variable (key=value). Overrides values from the project file
/// and from --variable-path.
#[arg(long = "variable", value_parser = parse_kv)]
pub variables: Vec<(String, String)>,
/// Load variable overrides from a YAML/JSON file (flat key/value
/// mapping). Can be repeated; later files override earlier ones, and
/// --variable beats anything from these files.
#[arg(long = "variable-path")]
pub variable_paths: Vec<PathBuf>,
/// Explicit path to a secrets file. When omitted, secrets are
/// auto-discovered next to the project file (`<stem>.secrets` and
/// `<stem>.<env>.secrets`).
#[arg(long)]
pub secrets_path: Option<PathBuf>,
/// Plan only: log what would be deleted without mutating anything.
#[arg(long)]
pub dry_run: bool,
/// Skip the interactive confirmation prompt. Required when stdin is not
/// a TTY (CI environments, piped invocations).
#[arg(long, alias = "auto-approve")]
pub yes: bool,
/// Restrict the run to one or more resources. Same syntax as apply's
/// `--target` (`apps.KEY`, `addons.KEY`, `network_groups.KEY`, ...).
#[arg(long = "target", value_parser = crate::commands::targets::parse_target_arg)]
pub targets: Vec<(crate::commands::targets::TargetKind, String)>,
}
#[derive(Debug, Args)]
pub struct InitArgs {
/// Project name (free-form). Asked interactively if omitted.
#[arg(long)]
pub name: Option<String>,
/// Clever Cloud organisation id (`orga_xxx`).
#[arg(long)]
pub org: Option<String>,
/// Default region. Defaults to `par`.
#[arg(long)]
pub region: Option<String>,
/// App kind (`node`, `docker`, `python`, `jar`, ...).
#[arg(long)]
pub kind: Option<String>,
/// GitHub source. Accepts `owner/repo`, `github.com/owner/repo`, or a
/// full URL. Omit (or pass `--no-source` in non-interactive mode) for
/// no source.
#[arg(long)]
pub source: Option<String>,
/// Explicitly create the project with no source, even in interactive
/// mode. Useful for `--non-interactive` runs.
#[arg(long, conflicts_with = "source")]
pub no_source: bool,
/// Addon kind to provision alongside the app (repeatable). E.g.
/// `--addon postgresql --addon redis`.
#[arg(long = "addon")]
pub addons: Vec<String>,
/// Output path. Default `project.clever.yaml`.
#[arg(short = 'o', long)]
pub output: Option<PathBuf>,
/// Don't prompt for anything. Fail if a required field wasn't passed.
#[arg(long)]
pub non_interactive: bool,
/// Overwrite the output file if it already exists.
#[arg(long)]
pub force: bool,
}
fn parse_kv(s: &str) -> Result<(String, String), String> {
let (k, v) = s
.split_once('=')
.ok_or_else(|| format!("expected key=value, got `{s}`"))?;
if k.is_empty() {
return Err(format!("empty key in `{s}`"));
}
Ok((k.to_string(), v.to_string()))
}