clever-project 0.0.8

Declare Clever Cloud resources in a YAML/JSON file and sync them via the clever-tools CLI.
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::path::PathBuf;

use clap::{Args, Parser, Subcommand, ValueEnum};

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Default)]
#[value(rename_all = "lowercase")]
pub enum OutputFormat {
    /// Human-readable output (the default). Logs go to stderr.
    #[default]
    Text,
    /// Single JSON document on stdout — pipes cleanly into `jq` and CI. Logs
    /// still go to stderr.
    Json,
}

impl OutputFormat {
    pub fn is_json(self) -> bool {
        matches!(self, OutputFormat::Json)
    }
}

#[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),
    /// Remove a stale state lock file. Use this when a previous `apply` or
    /// `delete` was killed (Ctrl+C, crash, kill -9) and left a lock behind.
    Unlock(UnlockArgs),
    /// Print a shell completion script to stdout. Redirect into the right
    /// location for your shell (see `clever-project completions --help`).
    Completions(CompletionsArgs),
}

#[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/.toml)
    #[arg(short = 'o', long)]
    pub output: PathBuf,

    /// Output format for the CLI's own log messages: human-readable text
    /// (the default) or a single JSON document on stdout.
    #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
    pub format: OutputFormat,
}

#[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 --variables-file-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 = "variables-file-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 = "secrets-file-path")]
    pub secrets_path: Option<PathBuf>,

    /// Set a secret (key=value). Overrides values from the project's secrets
    /// files (or from --secrets-file-path).
    #[arg(long = "secret", value_parser = parse_kv)]
    pub secrets: Vec<(String, String)>,

    /// 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)>,

    /// Output format: `text` (default) or `json`.
    #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
    pub format: OutputFormat,

    /// Skip the state lock. Use only when you've verified no other run is in
    /// progress against this project (CI matrix sharing a workspace, etc.).
    #[arg(long)]
    pub no_lock: bool,

    /// Skip every pre/post hook declared in the project file.
    #[arg(long)]
    pub skip_hooks: bool,
}

#[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 --variables-file-path.
    #[arg(long = "variable", value_parser = parse_kv)]
    pub variables: Vec<(String, String)>,

    /// Load variable overrides from a YAML/JSON file (repeatable).
    #[arg(long = "variables-file-path")]
    pub variable_paths: Vec<PathBuf>,

    /// Explicit path to a secrets file.
    #[arg(long = "secrets-file-path")]
    pub secrets_path: Option<PathBuf>,

    /// Set a secret (key=value). Overrides values from the project's secrets
    /// files (or from --secrets-file-path).
    #[arg(long = "secret", value_parser = parse_kv)]
    pub secrets: Vec<(String, String)>,

    /// 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,

    /// Output format: `text` (default) or `json`.
    #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
    pub format: OutputFormat,
}

#[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 --variables-file-path.
    #[arg(long = "variable", value_parser = parse_kv)]
    pub variables: Vec<(String, String)>,

    /// Load variable overrides from a YAML/JSON file (repeatable).
    #[arg(long = "variables-file-path")]
    pub variable_paths: Vec<PathBuf>,

    /// Explicit path to a secrets file.
    #[arg(long = "secrets-file-path")]
    pub secrets_path: Option<PathBuf>,

    /// Set a secret (key=value). Overrides values from the project's secrets
    /// files (or from --secrets-file-path).
    #[arg(long = "secret", value_parser = parse_kv)]
    pub secrets: Vec<(String, String)>,

    /// 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,

    /// Output format: `text` (default) or `json`.
    #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
    pub format: OutputFormat,
}

#[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 --variables-file-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 = "variables-file-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 = "secrets-file-path")]
    pub secrets_path: Option<PathBuf>,

    /// Set a secret (key=value). Overrides values from the project's secrets
    /// files (or from --secrets-file-path).
    #[arg(long = "secret", value_parser = parse_kv)]
    pub secrets: Vec<(String, String)>,

    /// 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)>,

    /// Output format: `text` (default) or `json`.
    #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
    pub format: OutputFormat,

    /// Skip the state lock. Use only when you've verified no other run is in
    /// progress against this project.
    #[arg(long)]
    pub no_lock: bool,

    /// Skip every pre/post hook declared in the project file.
    #[arg(long)]
    pub skip_hooks: bool,
}

#[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,

    /// Output format: `text` (default) or `json`. JSON mode implies
    /// `--non-interactive`.
    #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
    pub format: OutputFormat,
}

#[derive(Debug, Args)]
pub struct CompletionsArgs {
    /// Target shell (bash, zsh, fish, elvish, powershell).
    #[arg(value_enum)]
    pub shell: clap_complete::Shell,
}

#[derive(Debug, Args)]
pub struct UnlockArgs {
    /// Project file path (.yaml/.yml/.json). If omitted, looks for
    /// `project.clever.yaml`, `.yml` or `.json` in the current directory.
    pub file: Option<PathBuf>,

    /// Skip the interactive confirmation prompt.
    #[arg(long, alias = "auto-approve")]
    pub yes: bool,

    /// Output format: `text` (default) or `json`. JSON mode implies `--yes`.
    #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
    pub format: OutputFormat,
}

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()))
}