krab_cli 0.2.0

The `krab` CLI: dev workflow, generators, governance, and release operations for the Krab framework
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
use anyhow::Result;
use clap::{Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

mod auth_ops;
mod dev_workflow;
mod doctor;
mod generator;
mod governance;
mod project_model;
mod project_template;
mod release_ops;
mod topology;

use crate::auth_ops::dispatch_auth_action;
use crate::dev_workflow::{
    bootstrap_local_stack, build_project, dev_project, generate_docs, validate_environment,
    watch_project,
};
use crate::doctor::dispatch_doctor_command;
use crate::generator::dispatch_gen_resource;
use crate::governance::{
    dispatch_contract_action, dispatch_db_action, dispatch_release_action, dispatch_security_action,
};
use crate::project_template::generate_project_from_template;
use crate::topology::dispatch_topology_action;

#[derive(Parser)]
#[command(name = "krab")]
#[command(about = "Krab Framework CLI", long_about = None)]
// Reports the `krab_cli` package version, which is the workspace version. Users
// installing from crates.io need this to tell which release they have, and CI
// uses `krab --version` as the smoke test that `[[bin]] name = "krab"` took.
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Create a new krab project
    New {
        /// Name of the project
        name: String,
        /// Starter template to use
        #[arg(long, value_enum, default_value_t = ProjectTemplate::Default)]
        template: ProjectTemplate,
        /// Depend on a local Krab checkout instead of crates.io.
        ///
        /// Point this at the root of the krab repository. Used by the
        /// `generated-project` CI gate, which must build scaffolded output
        /// before that version exists on crates.io, and useful when testing a
        /// framework change against a fresh project.
        #[arg(long, value_name = "KRAB_REPO_ROOT")]
        path_deps: Option<PathBuf>,
    },
    /// Build the full stack application
    Build {
        /// Build in release mode
        #[arg(long)]
        release: bool,
        /// Selective rebuild target
        #[arg(long, value_enum, default_value_t = BuildTarget::All)]
        target: BuildTarget,
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
    },
    /// Run frontend dev workflow (build + run server)
    Dev {
        /// Build in release mode
        #[arg(long)]
        release: bool,
        /// Rebuild + restart frontend server automatically on file changes
        #[arg(long)]
        watch: bool,
        /// Polling interval in milliseconds while watching
        #[arg(long, default_value_t = 800)]
        poll_ms: u64,
        /// Debounce window in milliseconds before rebuild after file changes
        #[arg(long, default_value_t = 250)]
        settle_ms: u64,
    },
    /// Watch mode (build + restart frontend on changes)
    Watch {
        /// Build in release mode
        #[arg(long)]
        release: bool,
        /// Polling interval in milliseconds
        #[arg(long, default_value_t = 800)]
        poll_ms: u64,
        /// Debounce window in milliseconds before rebuild after file changes
        #[arg(long, default_value_t = 250)]
        settle_ms: u64,
    },
    /// Generate developer workflow documentation
    Docs {
        /// Output file path
        #[arg(long, default_value = "docs/guides/dev_workflow.md")]
        out: PathBuf,
    },
    /// Bootstrap full local stack in one command (build + orchestrator)
    Bootstrap {
        /// Build artifacts in release mode before starting stack
        #[arg(long)]
        release: bool,
        /// Skip build step and start orchestrator immediately
        #[arg(long)]
        skip_build: bool,
    },
    /// Validate common environment settings used by services
    EnvCheck {
        /// Fail with non-zero exit when warnings are found
        #[arg(long)]
        strict: bool,
    },
    /// Run API contract checks used by CI
    Contract {
        #[command(subcommand)]
        action: ContractAction,
    },
    /// Run database governance checks used by CI
    Db {
        #[command(subcommand)]
        action: DbAction,
    },
    /// Run security policy checks used by CI and local release gates
    Security {
        #[command(subcommand)]
        action: SecurityAction,
    },
    /// Authentication operator tooling
    Auth {
        #[command(subcommand)]
        action: AuthAction,
    },
    /// Release pipeline pre-flight checks
    Release {
        #[command(subcommand)]
        action: ReleaseAction,
    },
    /// Run aggregated workspace health checks
    Doctor {
        /// Emit richer check details
        #[arg(long)]
        diagnostics: bool,
        /// Treat warnings as failures
        #[arg(long)]
        strict: bool,
    },
    /// Topology governance checks and extraction scaffolding
    Topology {
        #[command(subcommand)]
        action: TopologyAction,
    },
    /// Generate resources
    Gen {
        #[command(subcommand)]
        resource: GenResource,
    },
}

#[derive(Subcommand)]
enum ContractAction {
    /// Run contract checks and schema snapshots
    Check {
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
    },
    /// Run protocol parity and resolver checks
    ProtocolCheck {
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
    },
}

#[derive(Subcommand)]
enum DbAction {
    /// Run migration lifecycle checks
    Lifecycle {
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
    },
    /// Run rollback simulation checks
    Rollback {
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
    },
    /// Run migration drift-detection checks
    Drift {
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
    },
    /// Run rollback rehearsal and capture evidence
    Rehearsal {
        /// Path for evidence output
        #[arg(
            long,
            default_value = "internal/audit/evidence/rollback-rehearsal-evidence.txt"
        )]
        out: PathBuf,
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
    },
}

#[derive(Subcommand)]
enum SecurityAction {
    /// Run dependency policy gate (cargo-deny advisories/licenses/bans/sources)
    DependencyGate {
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
    },
}

#[derive(Subcommand)]
enum AuthAction {
    /// Hash a password with Argon2id for KRAB_AUTH_LOGIN_USERS_JSON
    ///
    /// Prints a PHC string to stdout. Outside `local` environments the auth
    /// service refuses to start on any credential that is not one of these.
    HashPassword {
        /// The password to hash.
        ///
        /// Prefer omitting this and piping on stdin — an argument is visible in
        /// the process list and shell history to every user on the machine.
        #[arg(long, value_name = "PASSWORD")]
        password: Option<String>,
        /// Emit a ready-to-paste JSON credential map entry for this username
        #[arg(long, value_name = "USERNAME")]
        username: Option<String>,
    },
}

#[derive(Subcommand)]
enum ReleaseAction {
    /// Run comprehensive pre-flight release checklist
    Check {
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
        /// Output results as machine-readable JSON
        #[arg(long)]
        json: bool,
    },
    /// Run certification gates and write an evidence bundle
    Certify {
        /// Output directory for the evidence bundle
        #[arg(long, default_value = "release-evidence")]
        out: PathBuf,
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
        /// Output summary as machine-readable JSON
        #[arg(long)]
        json: bool,
    },
}

#[derive(Subcommand)]
enum TopologyAction {
    /// Validate topology hygiene and service-boundary contract rules
    Doctor {
        /// Emit richer command diagnostics
        #[arg(long)]
        diagnostics: bool,
    },
    /// Scaffold a split-service extraction skeleton for a domain
    Split {
        /// Domain name to extract (example: users, billing)
        domain: String,
        /// Protocol set for generated adapter stubs (CSV)
        #[arg(long, value_delimiter = ',')]
        protocols: Option<Vec<ServiceType>>,
        /// Register generated service in workspace Cargo.toml and krab.toml
        #[arg(long)]
        register: bool,
        /// Print planned files without writing
        #[arg(long)]
        dry_run: bool,
    },
}

#[derive(Subcommand)]
enum GenResource {
    /// Generate a new microservice
    Service {
        /// Name of the service (e.g., service_payment)
        name: String,
        /// Type of API to generate
        #[arg(long, value_enum)]
        r#type: ServiceType,
        /// Exposure mode (single/multi)
        #[arg(long, value_enum, default_value_t = ExposureMode::Single)]
        exposure_mode: ExposureMode,
        /// Protocol set for multi mode (CSV)
        #[arg(long, value_delimiter = ',')]
        protocols: Option<Vec<ServiceType>>,
        /// Deployment topology for generated services
        #[arg(long, value_enum, default_value_t = Topology::SingleService)]
        topology: Topology,
    },
    /// Generate a new component
    Component {
        /// Name of the component
        name: String,
    },
    /// Generate a new route
    Route {
        /// Name of the route
        name: String,
    },
    /// Generate a new server function
    ServerFunction {
        /// Name of the server function
        name: String,
    },
}

#[derive(Clone, ValueEnum, Debug, PartialEq, Eq)]
enum ServiceType {
    Rest,
    Graphql,
    Rpc,
    Grpc,
}

#[derive(Clone, ValueEnum, Debug, PartialEq, Eq)]
enum ExposureMode {
    Single,
    Multi,
}

#[derive(Clone, ValueEnum, Debug, PartialEq, Eq)]
enum Topology {
    SingleService,
    SplitServices,
}

#[derive(Clone, ValueEnum, Debug, PartialEq, Eq)]
enum ProjectTemplate {
    /// Minimal Krab service
    Default,
    /// SaaS service skeleton with auth-ready layers and tenant APIs
    Saas,
    /// Edge SSR policy skeleton with explicit render-policy metadata
    EdgeSsr,
    /// Event-stream dashboard with WebSocket and SSE
    EventStream,
}

#[derive(Clone, ValueEnum, Debug)]
enum BuildTarget {
    All,
    Frontend,
    Client,
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    dispatch_command(&cli.command)
}

fn dispatch_command(command: &Commands) -> Result<()> {
    match command {
        Commands::Build {
            release,
            target,
            diagnostics,
        } => {
            build_project(*release, target, *diagnostics)?;
        }
        Commands::Dev {
            release,
            watch,
            poll_ms,
            settle_ms,
        } => {
            if *watch {
                watch_project(*release, *poll_ms, *settle_ms)?;
            } else {
                dev_project(*release)?;
            }
        }
        Commands::Watch {
            release,
            poll_ms,
            settle_ms,
        } => {
            watch_project(*release, *poll_ms, *settle_ms)?;
        }
        Commands::Docs { out } => {
            generate_docs(out)?;
        }
        Commands::Bootstrap {
            release,
            skip_build,
        } => {
            bootstrap_local_stack(*release, *skip_build)?;
        }
        Commands::EnvCheck { strict } => {
            validate_environment(*strict)?;
        }
        Commands::Contract { action } => dispatch_contract_action(action)?,
        Commands::Db { action } => dispatch_db_action(action)?,
        Commands::Security { action } => dispatch_security_action(action)?,
        Commands::Auth { action } => dispatch_auth_action(action)?,
        Commands::Release { action } => dispatch_release_action(action)?,
        Commands::Doctor {
            diagnostics,
            strict,
        } => dispatch_doctor_command(*diagnostics, *strict)?,
        Commands::Topology { action } => dispatch_topology_action(action)?,
        Commands::Gen { resource } => dispatch_gen_resource(resource)?,
        Commands::New {
            name,
            template,
            path_deps,
        } => {
            generate_project_from_template(name, template, path_deps.as_deref())?;
        }
    }

    Ok(())
}