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];
107
108fn is_help_arg(arg: &OsString) -> bool {
109 arg.to_str()
110 .is_some_and(|arg| matches!(arg, "--help" | "-h"))
111}
112
113fn is_version_arg(arg: &OsString) -> bool {
114 arg.to_str()
115 .is_some_and(|arg| matches!(arg, "--version" | "-V"))
116}
117
118pub fn first_arg_is_help(args: &[OsString]) -> bool {
120 args.first().is_some_and(is_help_arg)
121}
122
123fn first_arg_is_version(args: &[OsString]) -> bool {
124 args.first().is_some_and(is_version_arg)
125}
126
127pub fn print_help_or_version(
131 args: &[OsString],
132 usage: impl FnOnce() -> String,
133 version_text: &str,
134) -> bool {
135 if first_arg_is_help(args) {
136 println!("{}", usage());
137 return true;
138 }
139 if first_arg_is_version(args) {
140 println!("{version_text}");
141 return true;
142 }
143 false
144}
145
146#[must_use]
147pub fn top_level_command() -> Command {
149 let command = Command::new("canic")
150 .version(env!("CARGO_PKG_VERSION"))
151 .about("Operator CLI for current Canic Apps and Fleets")
152 .color(ColorChoice::Always)
153 .subcommand_required(true)
154 .arg(icp_arg())
155 .arg(environment_arg())
156 .subcommand_help_heading("Commands")
157 .help_template(TOP_LEVEL_HELP_TEMPLATE)
158 .before_help(format!(
159 "{}Commands:{}\n{}",
160 COLOR_HEADING,
161 COLOR_RESET,
162 command_section(COMMAND_SPECS).join("\n")
163 ))
164 .after_help(format!(
165 "\n{}Tip:{} Run {} for command-specific help.",
166 COLOR_TIP,
167 COLOR_RESET,
168 color(COLOR_COMMAND, "`canic <command> --help`")
169 ));
170
171 COMMAND_SPECS.iter().fold(command, |command, spec| {
172 command.subcommand(
173 Command::new(spec.name)
174 .about(spec.about)
175 .disable_help_flag(true)
176 .disable_version_flag(true)
177 .arg(
178 Arg::new(DISPATCH_ARGS)
179 .num_args(0..)
180 .allow_hyphen_values(true)
181 .trailing_var_arg(true)
182 .value_parser(clap::value_parser!(OsString))
183 .hide(true),
184 ),
185 )
186 })
187}
188
189#[cfg(test)]
191pub fn usage() -> String {
192 let help = top_level_command().render_help();
193 help.ansi().to_string()
194}
195
196fn command_section(specs: &[CommandSpec]) -> Vec<String> {
197 specs
198 .iter()
199 .map(|spec| {
200 let command = format!("{:<12}", spec.name);
201 format!(" {} {}", color(COLOR_COMMAND, &command), spec.about)
202 })
203 .collect()
204}
205
206fn color(code: &str, text: &str) -> String {
207 format!("{code}{text}{COLOR_RESET}")
208}
209
210#[cfg(test)]
214mod tests {
215 use super::*;
216
217 #[test]
219 fn usage_contains_help_colors() {
220 let text = usage();
221
222 assert!(text.contains(COLOR_HEADING));
223 assert!(text.contains(COLOR_COMMAND));
224 }
225
226 #[test]
227 fn first_arg_help_and_version_detection_accepts_flags() {
228 assert!(first_arg_is_help(&[OsString::from("--help")]));
229 assert!(first_arg_is_help(&[OsString::from("-h")]));
230 assert!(first_arg_is_version(&[OsString::from("--version")]));
231 assert!(first_arg_is_version(&[OsString::from("-V")]));
232 }
233}