Skip to main content

xianyun_cli/commands/
mod.rs

1//! Subcommand implementations for the `caproni` CLI.
2//!
3//! Every module here corresponds to a `cmd/<name>` package in the Go
4//! implementation.
5
6pub mod ca;
7pub mod config;
8pub mod deploy;
9pub mod destroy;
10pub mod doctor;
11pub mod down;
12pub mod editmodeprocesssupervisor;
13pub mod k9s;
14pub mod kubectl;
15pub mod logs;
16pub mod registry;
17pub mod run;
18pub mod setkubecontext;
19pub mod status;
20pub mod stern;
21pub mod tool;
22pub mod up;
23pub mod update;
24pub mod updateetchosts;
25pub mod version;
26
27use xianyun_core::config::global::{Context, with_context};
28
29use crate::{Cli, Commands};
30
31/// Returns the Go-equivalent command name for [`crate::hooks::should_load_config`],
32/// matching `cmd/factory.go`'s subcommand names.
33fn command_name(cli: &Cli) -> &'static str {
34    match &cli.command {
35        Commands::Up(_) => "up",
36        Commands::Down(_) => "down",
37        Commands::Destroy(_) => "destroy",
38        Commands::Status(_) => "status",
39        Commands::Update(_) => "update",
40        Commands::Deploy(_) => "deploy",
41        Commands::UpdateEtcHosts(_) => "update-etc-hosts",
42        Commands::SetKubeContext(_) => "set-kube-context",
43        Commands::Run(_) => "run",
44        Commands::Config(_) => "config",
45        Commands::Version(_) => "version",
46        Commands::EditModeProcessSupervisor(_) => "edit-mode-process-supervisor",
47        Commands::Ca(_) => "ca",
48        Commands::Kubectl(_) => "kubectl",
49        Commands::K9s(_) => "k9s",
50        Commands::Stern(_) => "stern",
51        Commands::Registry(_) => "registry",
52        Commands::Logs(_) => "logs",
53        Commands::Doctor(_) => "doctor",
54        Commands::Tool(_) => "tool",
55    }
56}
57
58/// Dispatches a parsed [`Cli`] to the matching subcommand handler.
59///
60/// Ported from `cmd/factory.go`'s `Before` hook running ahead of every
61/// subcommand's `Action`: installs a logger and resolves the config file
62/// path before matching on the subcommand, mirroring `createBeforeHook`.
63/// The resolved [`GlobalConfig`](xianyun_core::config::global::GlobalConfig)
64/// is threaded into a single [`Context`] built once here and passed down
65/// to every subcommand handler that needs it (commands that don't load
66/// configuration, like `version`/`registry`, ignore it). This used to be
67/// rebuilt from [`Context::default`] inside each handler, which silently
68/// discarded `before_hook`'s resolved config path -- every config-reading
69/// real binary invocation failed with "no global config in context"
70/// despite each handler's own unit tests passing (they call the inner
71/// `run_*` functions directly with a manually-built `Context`, bypassing
72/// this wrapper). Caught by the `nix flake check` NixOS VM test added in
73/// `nix/tests/e2e-mock.nix`; see `book/src/project/testing.md`.
74pub fn dispatch(cli: &Cli) -> anyhow::Result<()> {
75    // A logging misconfiguration (an unparseable `--log-level`, say) is
76    // reported rather than swallowed: running with no logger and no
77    // explanation is worse than stopping.
78    crate::hooks::init_logging(cli)?;
79
80    let ctx = if crate::hooks::should_load_config(Some(command_name(cli))) {
81        let global_config = crate::hooks::before_hook(cli, Some(command_name(cli)))?;
82        with_context(Context::default(), global_config)
83    } else {
84        Context::default()
85    };
86
87    match &cli.command {
88        Commands::Up(args) => up::run(args, &ctx),
89        Commands::Down(args) => down::run(args, &ctx),
90        Commands::Destroy(args) => destroy::run(args, &ctx),
91        Commands::Status(args) => status::run(args, &ctx),
92        Commands::Update(args) => update::run(args, &ctx),
93        Commands::Deploy(args) => deploy::run(args, &ctx),
94        Commands::UpdateEtcHosts(args) => updateetchosts::run(args, &ctx),
95        Commands::SetKubeContext(args) => setkubecontext::run(args, &ctx),
96        Commands::Run(args) => run::run(args, &ctx, cli),
97        Commands::Config(args) => config::run(args, &ctx),
98        Commands::Version(args) => version::run(args),
99        Commands::EditModeProcessSupervisor(args) => editmodeprocesssupervisor::run(args, &ctx),
100        Commands::Ca(args) => ca::run(args, &ctx),
101        Commands::Kubectl(args) => kubectl::run(args, &ctx),
102        Commands::K9s(args) => k9s::run(args, &ctx),
103        Commands::Stern(args) => stern::run(args, &ctx),
104        Commands::Registry(args) => registry::run(args),
105        Commands::Logs(args) => logs::run(args, &ctx),
106        Commands::Doctor(args) => doctor::run(args, &ctx),
107        Commands::Tool(args) => tool::run(args, &ctx),
108    }
109}