1use crate::cli::globals::{DISPATCH_ARGS, environment_arg, icp_arg};
8use clap::{Arg, ColorChoice, Command};
9use std::ffi::OsString;
10
11const TOP_LEVEL_HELP_TEMPLATE: &str = "Canic Operator CLI v{version}\n{about-with-newline}\n{usage-heading} {usage}\n\n{before-help}\x1b[1mOptions:\x1b[0m\n{options}{after-help}\n";
12const COLOR_RESET: &str = "\x1b[0m";
13const COLOR_HEADING: &str = "\x1b[1m";
14const COLOR_COMMAND: &str = "\x1b[38;5;109m";
15const COLOR_TIP: &str = "\x1b[38;5;245m";
16
17#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20pub(super) struct CommandSpec {
21 pub(super) name: &'static str,
22 about: &'static str,
23}
24
25pub(super) const COMMAND_SPECS: &[CommandSpec] = &[
26 CommandSpec {
27 name: "admission",
28 about: "Plan, apply, and inspect Fleet ingress admission",
29 },
30 CommandSpec {
31 name: "app",
32 about: "Manage Canic source apps and roles",
33 },
34 CommandSpec {
35 name: "auth",
36 about: "Inspect delegated-auth operation state",
37 },
38 CommandSpec {
39 name: "backup",
40 about: "Plan, inspect, and verify backups",
41 },
42 CommandSpec {
43 name: "blob-storage",
44 about: "Inspect and manage blob-storage billing",
45 },
46 CommandSpec {
47 name: "build",
48 about: "Build Canic App and infrastructure artifacts",
49 },
50 CommandSpec {
51 name: "cycles",
52 about: "Inspect and transfer cycles for current Fleets",
53 },
54 CommandSpec {
55 name: "diagnostic",
56 about: "Look up one compact Canic diagnostic code",
57 },
58 CommandSpec {
59 name: "evidence",
60 about: "Evaluate stable evidence envelopes",
61 },
62 CommandSpec {
63 name: "fleet",
64 about: "Converge one Fleet from current desired state",
65 },
66 CommandSpec {
67 name: "info",
68 about: "Inspect one terminal current Fleet",
69 },
70 CommandSpec {
71 name: "inspect",
72 about: "Inspect one current Fleet canister runtime",
73 },
74 CommandSpec {
75 name: "medic",
76 about: "Diagnose workspace and current-Fleet readiness",
77 },
78 CommandSpec {
79 name: "network",
80 about: "Enroll canonical network trust identities",
81 },
82 CommandSpec {
83 name: "replica",
84 about: "Manage the local ICP replica",
85 },
86 CommandSpec {
87 name: "restore",
88 about: "Plan or run snapshot restores",
89 },
90 CommandSpec {
91 name: "scaffold",
92 about: "Scaffold Canic source roles",
93 },
94 CommandSpec {
95 name: "state",
96 about: "Audit declared Canic state metadata",
97 },
98 CommandSpec {
99 name: "status",
100 about: "Show quick local workspace status",
101 },
102 CommandSpec {
103 name: "token",
104 about: "Wrap ICP token balance and transfer commands",
105 },
106 CommandSpec {
107 name: "toolchain",
108 about: "Install checksum-authoritative release tools",
109 },
110];
111
112fn is_help_arg(arg: &OsString) -> bool {
113 arg.to_str()
114 .is_some_and(|arg| matches!(arg, "--help" | "-h"))
115}
116
117fn is_version_arg(arg: &OsString) -> bool {
118 arg.to_str()
119 .is_some_and(|arg| matches!(arg, "--version" | "-V"))
120}
121
122pub fn first_arg_is_help(args: &[OsString]) -> bool {
124 args.first().is_some_and(is_help_arg)
125}
126
127fn first_arg_is_version(args: &[OsString]) -> bool {
128 args.first().is_some_and(is_version_arg)
129}
130
131pub fn print_help_or_version(
135 args: &[OsString],
136 usage: impl FnOnce() -> String,
137 version_text: &str,
138) -> bool {
139 if first_arg_is_help(args) {
140 println!("{}", usage());
141 return true;
142 }
143 if first_arg_is_version(args) {
144 println!("{version_text}");
145 return true;
146 }
147 false
148}
149
150#[must_use]
151pub fn top_level_command() -> Command {
153 let command = Command::new("canic")
154 .version(env!("CARGO_PKG_VERSION"))
155 .about("Operator CLI for current Canic Apps and Fleets")
156 .color(ColorChoice::Always)
157 .subcommand_required(true)
158 .arg(icp_arg())
159 .arg(environment_arg())
160 .subcommand_help_heading("Commands")
161 .help_template(TOP_LEVEL_HELP_TEMPLATE)
162 .before_help(format!(
163 "{}Commands:{}\n{}",
164 COLOR_HEADING,
165 COLOR_RESET,
166 command_section(COMMAND_SPECS).join("\n")
167 ))
168 .after_help(format!(
169 "\n{}Tip:{} Run {} for command-specific help.",
170 COLOR_TIP,
171 COLOR_RESET,
172 color(COLOR_COMMAND, "`canic <command> --help`")
173 ));
174
175 COMMAND_SPECS.iter().fold(command, |command, spec| {
176 command.subcommand(
177 Command::new(spec.name)
178 .about(spec.about)
179 .disable_help_flag(true)
180 .disable_version_flag(true)
181 .arg(
182 Arg::new(DISPATCH_ARGS)
183 .num_args(0..)
184 .allow_hyphen_values(true)
185 .trailing_var_arg(true)
186 .value_parser(clap::value_parser!(OsString))
187 .hide(true),
188 ),
189 )
190 })
191}
192
193#[cfg(test)]
195pub fn usage() -> String {
196 let help = top_level_command().render_help();
197 help.ansi().to_string()
198}
199
200fn command_section(specs: &[CommandSpec]) -> Vec<String> {
201 specs
202 .iter()
203 .map(|spec| {
204 let command = format!("{:<12}", spec.name);
205 format!(" {} {}", color(COLOR_COMMAND, &command), spec.about)
206 })
207 .collect()
208}
209
210fn color(code: &str, text: &str) -> String {
211 format!("{code}{text}{COLOR_RESET}")
212}
213
214#[cfg(test)]
218mod tests {
219 use super::*;
220
221 #[test]
223 fn usage_contains_help_colors() {
224 let text = usage();
225
226 assert!(text.contains(COLOR_HEADING));
227 assert!(text.contains(COLOR_COMMAND));
228 }
229
230 #[test]
231 fn first_arg_help_and_version_detection_accepts_flags() {
232 assert!(first_arg_is_help(&[OsString::from("--help")]));
233 assert!(first_arg_is_help(&[OsString::from("-h")]));
234 assert!(first_arg_is_version(&[OsString::from("--version")]));
235 assert!(first_arg_is_version(&[OsString::from("-V")]));
236 }
237}