Skip to main content

packc/cli/
mod.rs

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    /// Logging filter (overrides PACKC_LOG)
38    #[arg(long = "log", default_value = "info", global = true)]
39    pub verbosity: String,
40
41    /// Force offline mode (disables any network activity)
42    #[arg(long, global = true)]
43    pub offline: bool,
44
45    /// Override cache directory (defaults to pack_dir/.packc or GREENTIC_PACK_CACHE_DIR)
46    #[arg(long = "cache-dir", global = true)]
47    pub cache_dir: Option<PathBuf>,
48
49    /// Optional config overrides in TOML/JSON (greentic-config layer)
50    #[arg(long = "config-override", value_name = "FILE", global = true)]
51    pub config_override: Option<PathBuf>,
52
53    /// Emit machine-readable JSON output where applicable
54    #[arg(long, global = true)]
55    pub json: bool,
56
57    /// Locale used for CLI messages (fallback: LC_ALL/LC_MESSAGES/LANG/system/en)
58    #[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 a pack component and supporting artifacts
69    Build(BuildArgs),
70    /// Lint a pack manifest, flows, and templates
71    Lint(self::lint::LintArgs),
72    /// Sync pack.yaml components with files under components/
73    Components(self::components::ComponentsArgs),
74    /// Sync pack.yaml components and flows with files under the pack root
75    Update(self::update::UpdateArgs),
76    /// Scaffold a new pack directory
77    New(new::NewArgs),
78    /// Sign a pack manifest using an Ed25519 private key
79    Sign(self::sign::SignArgs),
80    /// Verify a pack's manifest signature
81    Verify(self::verify::VerifyArgs),
82    /// GUI-related tooling
83    #[command(subcommand)]
84    Gui(self::gui::GuiCommand),
85    /// Diagnose a pack archive (.gtpack) or source directory (runs validation)
86    Doctor(self::inspect::InspectArgs),
87    /// Deprecated alias for `doctor`
88    Inspect(self::inspect::InspectArgs),
89    /// Inspect pack.lock.cbor (stable JSON to stdout)
90    InspectLock(self::inspect_lock::InspectLockArgs),
91    /// Run component QA and store answers.
92    Qa(self::qa::QaArgs),
93    /// Inspect resolved configuration (provenance and warnings)
94    Config(self::config::ConfigArgs),
95    /// Generate a DeploymentPlan from a pack archive or source directory.
96    Plan(self::plan::PlanArgs),
97    /// Legacy provider-extension helpers (schema-core path).
98    #[command(subcommand)]
99    Providers(self::providers::ProvidersCommand),
100    /// Add data to pack extensions (provider extension path is legacy/schema-core).
101    #[command(subcommand)]
102    AddExtension(self::add_extension::AddExtensionCommand),
103    /// Resolve extension dependency refs into pack.extensions.lock.json
104    ExtensionsLock(self::extensions_lock::ExtensionsLockArgs),
105    /// Interactive pack wizard.
106    Wizard(self::wizard::WizardArgs),
107    /// Resolve component references and write pack.lock.cbor
108    Resolve(self::resolve::ResolveArgs),
109}
110
111#[derive(Debug, Clone, Parser)]
112pub struct BuildArgs {
113    /// Root directory of the pack (must contain pack.yaml)
114    #[arg(long = "in", value_name = "DIR")]
115    pub input: PathBuf,
116
117    /// Skip running `packc update` before building (default: update first)
118    #[arg(long = "no-update", default_value_t = false)]
119    pub no_update: bool,
120
121    /// Output path for the built Wasm component (legacy; writes a stub)
122    #[arg(long = "out", value_name = "FILE")]
123    pub component_out: Option<PathBuf>,
124
125    /// Output path for the generated manifest (CBOR); defaults to dist/manifest.cbor
126    #[arg(long, value_name = "FILE")]
127    pub manifest: Option<PathBuf>,
128
129    /// Output path for the generated SBOM (legacy; writes a stub JSON)
130    #[arg(long, value_name = "FILE")]
131    pub sbom: Option<PathBuf>,
132
133    /// Output path for the generated & canonical .gtpack archive (default: dist/<pack_dir>.gtpack)
134    #[arg(long = "gtpack-out", value_name = "FILE")]
135    pub gtpack_out: Option<PathBuf>,
136
137    /// Optional path to pack.lock.cbor (default: <pack_dir>/pack.lock.cbor)
138    #[arg(long = "lock", value_name = "FILE")]
139    pub lock: Option<PathBuf>,
140
141    /// Bundle strategy for component artifacts (cache=embed wasm, none=refs only)
142    #[arg(long = "bundle", value_enum, default_value = "cache")]
143    pub bundle: crate::build::BundleMode,
144
145    /// When set, the command validates input without writing artifacts
146    #[arg(long)]
147    pub dry_run: bool,
148
149    /// Optional JSON file with additional secret requirements (migration bridge)
150    #[arg(long = "secrets-req", value_name = "FILE")]
151    pub secrets_req: Option<PathBuf>,
152
153    /// Default secret scope to apply when missing (dev-only), format: env/tenant[/team]
154    #[arg(long = "default-secret-scope", value_name = "ENV/TENANT[/TEAM]")]
155    pub default_secret_scope: Option<String>,
156
157    /// Allow OCI component refs in extensions to be tag-based (default requires sha256 digest)
158    #[arg(long = "allow-oci-tags", default_value_t = false)]
159    pub allow_oci_tags: bool,
160
161    /// Require manifest metadata for flow-referenced components
162    #[arg(long, default_value_t = false)]
163    pub require_component_manifests: bool,
164
165    /// Skip auto-including extra directories (e.g. schemas/, templates/)
166    #[arg(long = "no-extra-dirs", default_value_t = false)]
167    pub no_extra_dirs: bool,
168
169    /// Include source files (pack.yaml, flows) inside the generated .gtpack for debugging
170    #[arg(long = "dev", default_value_t = false)]
171    pub dev: bool,
172
173    /// Migration-only escape hatch: allow deriving component manifest/schema from pack.yaml.
174    #[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
267/// Resolve the logging filter to use for telemetry initialisation.
268pub fn resolve_env_filter(cli: &Cli) -> String {
269    std::env::var("PACKC_LOG").unwrap_or_else(|_| cli.verbosity.clone())
270}
271
272/// Execute the CLI using a pre-parsed argument set.
273pub 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    // Install telemetry according to resolved config.
285    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}