greentic-start-dev 1.1.27190108346

Greentic lifecycle runner for start/restart/stop orchestration
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use std::path::PathBuf;

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

use crate::DEMO_DEFAULT_TEAM;
use crate::DEMO_DEFAULT_TENANT;
use crate::runtime::NatsMode;

#[derive(Parser)]
#[command(name = "greentic-start", version)]
pub(crate) struct Cli {
    #[arg(long, global = true)]
    pub(crate) locale: Option<String>,
    #[command(subcommand)]
    pub(crate) command: Command,
}

#[derive(Subcommand)]
pub(crate) enum Command {
    Start(StartArgs),
    Up(StartArgs),
    Stop(StopArgs),
    Restart(StartArgs),
    Warmup(WarmupArgs),
    Doctor(DoctorArgs),
}

#[derive(Parser, Clone)]
pub(crate) struct DoctorArgs {
    /// Bundle reference or extracted bundle directory to inspect.
    pub(crate) bundle: String,
    /// Emit stable machine-readable JSON.
    #[arg(long)]
    pub(crate) json: bool,
    /// Promote drift/tag/cache warnings to errors.
    #[arg(long)]
    pub(crate) strict: bool,
    /// Include longer remediation hints in human output.
    #[arg(long)]
    pub(crate) fix_hints: bool,
    /// Include informational checks in output.
    #[arg(long)]
    pub(crate) show_info: bool,
    /// Restrict checks to one diagnostic stage.
    #[arg(long, value_enum, default_value_t = DoctorStageArg::All)]
    pub(crate) stage: DoctorStageArg,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub(crate) enum DoctorStageArg {
    All,
    Setup,
    Cache,
    Locks,
    Answers,
    Runtime,
    Routes,
    Provider,
    Secrets,
}

#[derive(Parser, Clone)]
pub(crate) struct WarmupArgs {
    /// Path to a setup-resolved bundle directory whose components should be precompiled.
    #[arg(long)]
    pub(crate) bundle: PathBuf,
    /// Cache root directory. Defaults to `${GREENTIC_CACHE_DIR}` or `.greentic/cache/components`.
    #[arg(long, value_name = "DIR")]
    pub(crate) cache_dir: Option<PathBuf>,
    /// Fail on the first compile error instead of counting it as skipped.
    #[arg(long)]
    pub(crate) strict: bool,
}

#[derive(Parser, Clone)]
pub(crate) struct StartArgs {
    #[arg(long)]
    bundle: Option<String>,
    #[arg(long)]
    tenant: Option<String>,
    #[arg(long)]
    team: Option<String>,
    #[arg(long, hide = true, conflicts_with = "nats")]
    no_nats: bool,
    #[arg(long = "nats", value_enum, default_value_t = NatsModeArg::Off)]
    nats: NatsModeArg,
    #[arg(long)]
    nats_url: Option<String>,
    #[arg(long)]
    config: Option<PathBuf>,
    #[arg(long, value_enum, default_value_t = CloudflaredModeArg::Off)]
    cloudflared: CloudflaredModeArg,
    #[arg(long)]
    cloudflared_binary: Option<PathBuf>,
    #[arg(long, value_enum, default_value_t = NgrokModeArg::Off)]
    ngrok: NgrokModeArg,
    #[arg(long)]
    ngrok_binary: Option<PathBuf>,
    #[arg(long)]
    runner_binary: Option<PathBuf>,
    #[arg(long, value_enum, value_delimiter = ',')]
    restart: Vec<RestartTarget>,
    #[arg(long, value_name = "DIR")]
    log_dir: Option<PathBuf>,
    #[arg(long, conflicts_with = "quiet")]
    verbose: bool,
    #[arg(long, conflicts_with = "verbose")]
    quiet: bool,
    #[arg(long, help = "Do not open the first web UI URL in the default browser")]
    no_browser: bool,
    #[arg(long, help = "Enable mTLS admin API endpoint")]
    admin: bool,
    #[arg(long, default_value = "8443", help = "Port for the admin API endpoint")]
    admin_port: u16,
    #[arg(
        long,
        value_name = "DIR",
        help = "Directory containing admin TLS certs (server.crt, server.key, ca.crt)"
    )]
    admin_certs_dir: Option<PathBuf>,
    #[arg(
        long,
        value_delimiter = ',',
        help = "Comma-separated list of allowed client CNs (empty = allow all valid certs)"
    )]
    admin_allowed_clients: Vec<String>,
}

#[derive(Parser, Clone)]
pub(crate) struct StopArgs {
    #[arg(long)]
    bundle: Option<String>,
    #[arg(long)]
    state_dir: Option<PathBuf>,
    #[arg(long, default_value = DEMO_DEFAULT_TENANT)]
    tenant: String,
    #[arg(long, default_value = DEMO_DEFAULT_TEAM)]
    team: String,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub enum NatsModeArg {
    Off,
    On,
    External,
}

impl From<NatsModeArg> for NatsMode {
    fn from(value: NatsModeArg) -> Self {
        match value {
            NatsModeArg::Off => NatsMode::Off,
            NatsModeArg::On => NatsMode::On,
            NatsModeArg::External => NatsMode::External,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub enum CloudflaredModeArg {
    On,
    Off,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub enum NgrokModeArg {
    On,
    Off,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub enum RestartTarget {
    All,
    Cloudflared,
    Ngrok,
    Nats,
    Gateway,
    Egress,
    Subscriptions,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StartRequest {
    pub bundle: Option<String>,
    pub tenant: Option<String>,
    pub team: Option<String>,
    pub no_nats: bool,
    pub nats: NatsModeArg,
    pub nats_url: Option<String>,
    pub config: Option<PathBuf>,
    pub cloudflared: CloudflaredModeArg,
    pub cloudflared_binary: Option<PathBuf>,
    pub ngrok: NgrokModeArg,
    pub ngrok_binary: Option<PathBuf>,
    pub runner_binary: Option<PathBuf>,
    pub restart: Vec<RestartTarget>,
    pub log_dir: Option<PathBuf>,
    pub verbose: bool,
    pub quiet: bool,
    pub no_browser: bool,
    pub admin: bool,
    pub admin_port: u16,
    pub admin_certs_dir: Option<PathBuf>,
    pub admin_allowed_clients: Vec<String>,
    /// Whether the user explicitly set `--cloudflared` or `--ngrok` on the CLI.
    /// When `false` and the terminal is interactive, we prompt for tunnel selection.
    pub tunnel_explicit: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StopRequest {
    pub bundle: Option<String>,
    pub state_dir: Option<PathBuf>,
    pub tenant: String,
    pub team: String,
}

pub(crate) fn start_request_from_args(args: StartArgs, tunnel_explicit: bool) -> StartRequest {
    StartRequest {
        bundle: args.bundle,
        tenant: args.tenant,
        team: args.team,
        no_nats: args.no_nats,
        nats: args.nats,
        nats_url: args.nats_url,
        config: args.config,
        cloudflared: args.cloudflared,
        cloudflared_binary: args.cloudflared_binary,
        ngrok: args.ngrok,
        ngrok_binary: args.ngrok_binary,
        runner_binary: args.runner_binary,
        restart: args.restart,
        log_dir: args.log_dir,
        verbose: args.verbose,
        quiet: args.quiet,
        no_browser: args.no_browser,
        admin: args.admin,
        admin_port: args.admin_port,
        admin_certs_dir: args.admin_certs_dir,
        admin_allowed_clients: args.admin_allowed_clients,
        tunnel_explicit,
    }
}

pub(crate) fn stop_request_from_args(args: StopArgs) -> StopRequest {
    StopRequest {
        bundle: args.bundle,
        state_dir: args.state_dir,
        tenant: args.tenant,
        team: args.team,
    }
}

pub(crate) fn normalize_args(raw_tail: Vec<String>) -> Vec<String> {
    let mut out = vec!["greentic-start".to_string()];
    let mut stripped_demo_prefix = false;
    let mut skip_next_value = false;
    for arg in raw_tail {
        if skip_next_value {
            skip_next_value = false;
            out.push(arg);
            continue;
        }
        if arg_takes_value(&arg) {
            skip_next_value = true;
            out.push(arg);
            continue;
        }
        if !stripped_demo_prefix && !arg.starts_with('-') {
            stripped_demo_prefix = true;
            if arg == "demo" {
                continue;
            }
        }
        out.push(arg);
    }

    if only_global_flags(&out[1..]) {
        return out;
    }

    let known = ["start", "up", "stop", "restart", "warmup", "doctor"];
    let mut first_pos = None;
    let mut skip_next_value = false;
    for arg in out.iter().skip(1) {
        if skip_next_value {
            skip_next_value = false;
            continue;
        }
        if arg_takes_value(arg) {
            skip_next_value = true;
            continue;
        }
        if !arg.starts_with('-') {
            first_pos = Some(arg.clone());
            break;
        }
    }
    let should_insert_start = match first_pos {
        Some(cmd) => !known.contains(&cmd.as_str()),
        None => true,
    };
    if should_insert_start {
        out.insert(1, "start".to_string());
    }
    out
}

fn only_global_flags(args: &[String]) -> bool {
    if args.is_empty() {
        return false;
    }

    let mut index = 0;
    while index < args.len() {
        match args[index].as_str() {
            "--help" | "-h" | "--version" | "-V" => {
                index += 1;
            }
            "--locale" => {
                if index + 1 >= args.len() {
                    return false;
                }
                index += 2;
            }
            value if value.starts_with("--locale=") => {
                index += 1;
            }
            _ => return false,
        }
    }

    true
}

fn arg_takes_value(arg: &str) -> bool {
    matches!(
        arg,
        "--locale"
            | "--bundle"
            | "--tenant"
            | "--team"
            | "--nats"
            | "--nats-url"
            | "--config"
            | "--cloudflared"
            | "--cloudflared-binary"
            | "--ngrok"
            | "--ngrok-binary"
            | "--runner-binary"
            | "--restart"
            | "--log-dir"
            | "--stage"
            | "--state-dir"
            | "--admin-port"
            | "--admin-certs-dir"
            | "--admin-allowed-clients"
    )
}

pub(crate) fn restart_name(target: &RestartTarget) -> String {
    match target {
        RestartTarget::All => "all",
        RestartTarget::Cloudflared => "cloudflared",
        RestartTarget::Ngrok => "ngrok",
        RestartTarget::Nats => "nats",
        RestartTarget::Gateway => "gateway",
        RestartTarget::Egress => "egress",
        RestartTarget::Subscriptions => "subscriptions",
    }
    .to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn normalize_args_inserts_start_for_short_form() {
        let args = normalize_args(vec!["--tenant".into(), "demo".into()]);
        assert_eq!(args[0], "greentic-start");
        assert_eq!(args[1], "start");
        assert_eq!(args[2], "--tenant");
    }

    #[test]
    fn normalize_args_removes_demo_prefix() {
        let args = normalize_args(vec!["demo".into(), "start".into(), "--tenant".into()]);
        assert_eq!(args[0], "greentic-start");
        assert_eq!(args[1], "start");
        assert_eq!(args[2], "--tenant");
    }

    #[test]
    fn normalize_args_keeps_explicit_stop() {
        let args = normalize_args(vec!["stop".into(), "--tenant".into(), "demo".into()]);
        assert_eq!(args[0], "greentic-start");
        assert_eq!(args[1], "stop");
        assert_eq!(args[2], "--tenant");
        assert_eq!(args[3], "demo");
    }

    #[test]
    fn normalize_args_keeps_explicit_doctor() {
        let args = normalize_args(vec!["doctor".into(), ".".into(), "--json".into()]);
        assert_eq!(args[0], "greentic-start");
        assert_eq!(args[1], "doctor");
        assert_eq!(args[2], ".");
    }

    #[test]
    fn normalize_args_strips_only_leading_demo_prefix() {
        let args = normalize_args(vec![
            "--locale".into(),
            "en".into(),
            "demo".into(),
            "start".into(),
            "--tenant".into(),
            "demo".into(),
        ]);
        assert_eq!(args[0], "greentic-start");
        assert_eq!(args[1], "--locale");
        assert_eq!(args[2], "en");
        assert_eq!(args[3], "start");
        assert_eq!(args[4], "--tenant");
        assert_eq!(args[5], "demo");
    }

    #[test]
    fn normalize_args_keeps_runner_binary_value_with_demo_prefix() {
        let args = normalize_args(vec![
            "demo".into(),
            "start".into(),
            "--runner-binary".into(),
            "/tmp/runner".into(),
        ]);
        assert_eq!(args[0], "greentic-start");
        assert_eq!(args[1], "start");
        assert_eq!(args[2], "--runner-binary");
        assert_eq!(args[3], "/tmp/runner");
    }

    #[test]
    fn normalize_args_keeps_global_version_flag_without_start() {
        let args = normalize_args(vec!["--version".into()]);
        assert_eq!(
            args,
            vec!["greentic-start".to_string(), "--version".to_string()]
        );
    }

    #[test]
    fn normalize_args_keeps_global_help_flag_without_start() {
        let args = normalize_args(vec!["--help".into()]);
        assert_eq!(
            args,
            vec!["greentic-start".to_string(), "--help".to_string()]
        );
    }

    #[test]
    fn normalize_args_keeps_locale_and_version_without_start() {
        let args = normalize_args(vec!["--locale".into(), "en".into(), "--version".into()]);
        assert_eq!(
            args,
            vec![
                "greentic-start".to_string(),
                "--locale".to_string(),
                "en".to_string(),
                "--version".to_string(),
            ]
        );
    }
}