1#![forbid(unsafe_code)]
2
3use std::{convert::TryFrom, path::PathBuf};
4
5use anyhow::Result;
6use clap::{Parser, Subcommand};
7use greentic_types::{EnvId, TenantCtx, TenantId};
8use tokio::runtime::Runtime;
9
10pub mod add_extension;
11pub mod components;
12pub mod config;
13pub mod extensions_lock;
14pub mod gui;
15pub mod input;
16pub mod inspect;
17pub mod inspect_lock;
18pub mod lint;
19pub mod plan;
20pub mod providers;
21pub mod qa;
22pub mod resolve;
23pub mod sign;
24pub mod update;
25pub mod verify;
26pub mod wizard;
27mod wizard_catalog;
28mod wizard_i18n;
29mod wizard_ui;
30
31use crate::telemetry::set_current_tenant_ctx;
32use crate::{build, new, runtime};
33
34#[derive(Debug, Parser)]
35#[command(name = "greentic-pack", about = "Greentic pack CLI", version)]
36pub struct Cli {
37 #[arg(long = "log", default_value = "info", global = true)]
39 pub verbosity: String,
40
41 #[arg(long, global = true)]
43 pub offline: bool,
44
45 #[arg(long = "cache-dir", global = true)]
47 pub cache_dir: Option<PathBuf>,
48
49 #[arg(long = "config-override", value_name = "FILE", global = true)]
51 pub config_override: Option<PathBuf>,
52
53 #[arg(long, global = true)]
55 pub json: bool,
56
57 #[arg(long, global = true)]
59 pub locale: Option<String>,
60
61 #[command(subcommand)]
62 pub command: Command,
63}
64
65#[allow(clippy::large_enum_variant)]
66#[derive(Debug, Subcommand)]
67pub enum Command {
68 Build(BuildArgs),
70 Lint(self::lint::LintArgs),
72 Components(self::components::ComponentsArgs),
74 Update(self::update::UpdateArgs),
76 New(new::NewArgs),
78 Sign(self::sign::SignArgs),
80 Verify(self::verify::VerifyArgs),
82 #[command(subcommand)]
84 Gui(self::gui::GuiCommand),
85 Doctor(self::inspect::InspectArgs),
87 Inspect(self::inspect::InspectArgs),
89 InspectLock(self::inspect_lock::InspectLockArgs),
91 Qa(self::qa::QaArgs),
93 Config(self::config::ConfigArgs),
95 Plan(self::plan::PlanArgs),
97 #[command(subcommand)]
99 Providers(self::providers::ProvidersCommand),
100 #[command(subcommand)]
102 AddExtension(self::add_extension::AddExtensionCommand),
103 ExtensionsLock(self::extensions_lock::ExtensionsLockArgs),
105 Wizard(self::wizard::WizardArgs),
107 Resolve(self::resolve::ResolveArgs),
109}
110
111#[derive(Debug, Clone, Parser)]
112pub struct BuildArgs {
113 #[arg(long = "in", value_name = "DIR")]
115 pub input: PathBuf,
116
117 #[arg(long = "no-update", default_value_t = false)]
119 pub no_update: bool,
120
121 #[arg(long = "out", value_name = "FILE")]
123 pub component_out: Option<PathBuf>,
124
125 #[arg(long, value_name = "FILE")]
127 pub manifest: Option<PathBuf>,
128
129 #[arg(long, value_name = "FILE")]
131 pub sbom: Option<PathBuf>,
132
133 #[arg(long = "gtpack-out", value_name = "FILE")]
135 pub gtpack_out: Option<PathBuf>,
136
137 #[arg(long = "lock", value_name = "FILE")]
139 pub lock: Option<PathBuf>,
140
141 #[arg(long = "bundle", value_enum, default_value = "cache")]
143 pub bundle: crate::build::BundleMode,
144
145 #[arg(long)]
147 pub dry_run: bool,
148
149 #[arg(long = "secrets-req", value_name = "FILE")]
151 pub secrets_req: Option<PathBuf>,
152
153 #[arg(long = "default-secret-scope", value_name = "ENV/TENANT[/TEAM]")]
155 pub default_secret_scope: Option<String>,
156
157 #[arg(long = "allow-oci-tags", default_value_t = false)]
159 pub allow_oci_tags: bool,
160
161 #[arg(long, default_value_t = false)]
163 pub require_component_manifests: bool,
164
165 #[arg(long = "no-extra-dirs", default_value_t = false)]
167 pub no_extra_dirs: bool,
168
169 #[arg(long = "dev", default_value_t = false)]
171 pub dev: bool,
172
173 #[arg(long = "allow-pack-schema", default_value_t = false)]
175 pub allow_pack_schema: bool,
176}
177
178pub fn run() -> Result<()> {
179 Runtime::new()?.block_on(run_with_cli(Cli::parse(), false))
180}
181
182pub fn print_top_level_help() {
183 println!("{}", crate::cli_i18n::t("cli.help.title"));
184 println!();
185 println!("{}", crate::cli_i18n::t("cli.help.usage"));
186 println!();
187 println!("{}", crate::cli_i18n::t("cli.help.commands_header"));
188 println!("{}", crate::cli_i18n::t("cli.help.command.build"));
189 println!("{}", crate::cli_i18n::t("cli.help.command.lint"));
190 println!("{}", crate::cli_i18n::t("cli.help.command.components"));
191 println!("{}", crate::cli_i18n::t("cli.help.command.update"));
192 println!("{}", crate::cli_i18n::t("cli.help.command.new"));
193 println!("{}", crate::cli_i18n::t("cli.help.command.sign"));
194 println!("{}", crate::cli_i18n::t("cli.help.command.verify"));
195 println!("{}", crate::cli_i18n::t("cli.help.command.gui"));
196 println!("{}", crate::cli_i18n::t("cli.help.command.doctor"));
197 println!("{}", crate::cli_i18n::t("cli.help.command.inspect"));
198 println!("{}", crate::cli_i18n::t("cli.help.command.inspect_lock"));
199 println!("{}", crate::cli_i18n::t("cli.help.command.qa"));
200 println!("{}", crate::cli_i18n::t("cli.help.command.config"));
201 println!("{}", crate::cli_i18n::t("cli.help.command.plan"));
202 println!("{}", crate::cli_i18n::t("cli.help.command.providers"));
203 println!("{}", crate::cli_i18n::t("cli.help.command.add_extension"));
204 println!("{}", crate::cli_i18n::t("cli.help.command.extensions_lock"));
205 println!("{}", crate::cli_i18n::t("cli.help.command.wizard"));
206 println!("{}", crate::cli_i18n::t("cli.help.command.resolve"));
207 println!("{}", crate::cli_i18n::t("cli.help.command.help"));
208 println!();
209 println!("{}", crate::cli_i18n::t("cli.help.options_header"));
210 println!("{}", crate::cli_i18n::t("cli.help.option.log"));
211 println!("{}", crate::cli_i18n::t("cli.help.option.offline"));
212 println!("{}", crate::cli_i18n::t("cli.help.option.cache_dir"));
213 println!("{}", crate::cli_i18n::t("cli.help.option.config_override"));
214 println!("{}", crate::cli_i18n::t("cli.help.option.json"));
215 println!("{}", crate::cli_i18n::t("cli.help.option.locale"));
216 println!("{}", crate::cli_i18n::t("cli.help.option.help"));
217 println!("{}", crate::cli_i18n::t("cli.help.option.version"));
218}
219
220pub fn print_help_for_path(path: &[String]) -> bool {
221 let key = match path {
222 [] => "cli.help.page.root",
223 [a] if a == "build" => "cli.help.page.build",
224 [a] if a == "lint" => "cli.help.page.lint",
225 [a] if a == "components" => "cli.help.page.components",
226 [a] if a == "update" => "cli.help.page.update",
227 [a] if a == "new" => "cli.help.page.new",
228 [a] if a == "sign" => "cli.help.page.sign",
229 [a] if a == "verify" => "cli.help.page.verify",
230 [a] if a == "gui" => "cli.help.page.gui",
231 [a] if a == "doctor" => "cli.help.page.doctor",
232 [a] if a == "inspect" => "cli.help.page.inspect",
233 [a] if a == "inspect-lock" => "cli.help.page.inspect_lock",
234 [a] if a == "qa" => "cli.help.page.qa",
235 [a] if a == "config" => "cli.help.page.config",
236 [a] if a == "plan" => "cli.help.page.plan",
237 [a] if a == "providers" => "cli.help.page.providers",
238 [a] if a == "add-extension" => "cli.help.page.add_extension",
239 [a] if a == "extensions-lock" => "cli.help.page.extensions_lock",
240 [a] if a == "wizard" => "cli.help.page.wizard",
241 [a, b] if a == "wizard" && b == "run" => "cli.help.page.wizard_run",
242 [a, b] if a == "wizard" && b == "validate" => "cli.help.page.wizard_validate",
243 [a, b] if a == "wizard" && b == "apply" => "cli.help.page.wizard_apply",
244 [a] if a == "resolve" => "cli.help.page.resolve",
245 [a, b] if a == "gui" && b == "loveable-convert" => "cli.help.page.gui_loveable_convert",
246 [a, b] if a == "providers" && b == "list" => "cli.help.page.providers_list",
247 [a, b] if a == "providers" && b == "info" => "cli.help.page.providers_info",
248 [a, b] if a == "providers" && b == "validate" => "cli.help.page.providers_validate",
249 [a, b] if a == "add-extension" && b == "provider" => "cli.help.page.add_extension_provider",
250 [a, b] if a == "add-extension" && b == "capability" => {
251 "cli.help.page.add_extension_capability"
252 }
253 [a, b] if a == "add-extension" && b == "deployer" => "cli.help.page.add_extension_deployer",
254 [a, b] if a == "add-extension" && b == "dependency" => {
255 "cli.help.page.add_extension_dependency"
256 }
257 _ => return false,
258 };
259
260 if !crate::cli_i18n::has(key) {
261 return false;
262 }
263 println!("{}", crate::cli_i18n::t(key));
264 true
265}
266
267pub fn resolve_env_filter(cli: &Cli) -> String {
269 std::env::var("PACKC_LOG").unwrap_or_else(|_| cli.verbosity.clone())
270}
271
272pub async fn run_with_cli(cli: Cli, warn_inspect_alias: bool) -> Result<()> {
274 let wizard_locale = cli.locale.clone();
275 crate::cli_i18n::init_locale(cli.locale.as_deref());
276
277 let runtime = runtime::resolve_runtime(
278 Some(std::env::current_dir()?.as_path()),
279 cli.cache_dir.as_deref(),
280 cli.offline,
281 cli.config_override.as_deref(),
282 )?;
283
284 crate::telemetry::install_with_config("packc", &runtime.resolved.config.telemetry)?;
286
287 set_current_tenant_ctx(&TenantCtx::new(
288 EnvId::try_from("local").expect("static env id"),
289 TenantId::try_from("packc").expect("static tenant id"),
290 ));
291
292 match cli.command {
293 Command::Build(args) => {
294 build::run(&build::BuildOptions::from_args(args, &runtime)?).await?
295 }
296 Command::Lint(args) => self::lint::handle(args, cli.json)?,
297 Command::Components(args) => self::components::handle(args, cli.json)?,
298 Command::Update(args) => self::update::handle(args, cli.json)?,
299 Command::New(args) => new::handle(args, cli.json, &runtime).await?,
300 Command::Sign(args) => self::sign::handle(args, cli.json)?,
301 Command::Verify(args) => self::verify::handle(args, cli.json)?,
302 Command::Gui(cmd) => self::gui::handle(cmd, cli.json, &runtime).await?,
303 Command::Inspect(args) | Command::Doctor(args) => {
304 if warn_inspect_alias {
305 eprintln!("{}", crate::cli_i18n::t("cli.warn.inspect_deprecated"));
306 }
307 self::inspect::handle(args, cli.json, &runtime).await?
308 }
309 Command::InspectLock(args) => self::inspect_lock::handle(args)?,
310 Command::Qa(args) => self::qa::handle(args, &runtime)?,
311 Command::Config(args) => self::config::handle(args, cli.json, &runtime)?,
312 Command::Plan(args) => self::plan::handle(&args)?,
313 Command::Providers(cmd) => self::providers::run(cmd)?,
314 Command::AddExtension(cmd) => self::add_extension::handle(cmd)?,
315 Command::ExtensionsLock(args) => {
316 self::extensions_lock::handle(args, &runtime, true).await?
317 }
318 Command::Wizard(args) => self::wizard::handle(args, &runtime, wizard_locale.as_deref())?,
319 Command::Resolve(args) => self::resolve::handle(args, &runtime, true).await?,
320 }
321
322 Ok(())
323}