zoi_cli/cmd/registry.rs
1//! Logic for the `registry` command.
2//!
3//! This module provides commands for managing Zoi registries, including
4//! initialization, metadata generation, and package/advisory management.
5
6use anyhow::Result;
7use clap::{Parser, Subcommand};
8
9/// The root registry management command.
10#[derive(Parser, Debug)]
11pub struct RegistryCommand {
12 /// The specific registry subcommand to execute.
13 #[command(subcommand)]
14 pub command: RegistryCommands
15}
16
17/// Available registry subcommands.
18#[derive(Subcommand, Debug)]
19pub enum RegistryCommands {
20 /// Initialize a new Zoi registry
21 Init {
22 /// Path where the registry should be initialized
23 #[arg(default_value = ".")]
24 path: std::path::PathBuf
25 },
26 /// Generate metadata files (packages.json and advisories.json)
27 #[command(alias = "gen-meta")]
28 GenerateMetadata,
29 /// Check registry integrity and validate packages
30 #[command(aliases = ["lint", "audit"])]
31 Check,
32 /// Add a new package to the registry
33 #[command(alias = "add-pkg")]
34 AddPackage {
35 /// Name of the package to add
36 name: Option<String>,
37 /// Repository tier (e.g. community, main)
38 #[arg(long, short)]
39 repo: Option<String>
40 },
41 /// Add a new security advisory for a package
42 #[command(alias = "sec")]
43 AddAdvisory {
44 /// Package name to add an advisory for
45 package: Option<String>,
46 /// Repository tier (e.g. community, main)
47 #[arg(long, short)]
48 repo: Option<String>
49 }
50}
51
52/// Run the registry management command.
53///
54/// # Errors
55///
56/// This function returns an error if any of the underlying registry operations
57/// (initialization, metadata generation, checking, or adding
58/// packages/advisories) fail. # Errors
59///
60/// Returns an error if the registry operation fails.
61pub fn run(args: RegistryCommand) -> Result<()> {
62 let registry_root = std::path::Path::new(".");
63 match args.command {
64 RegistryCommands::Init { path } => crate::pkg::registry::init(&path),
65 RegistryCommands::GenerateMetadata => {
66 crate::pkg::registry::generate_metadata(registry_root)
67 }
68 RegistryCommands::Check => crate::pkg::registry::check(registry_root),
69 RegistryCommands::AddPackage { name, repo } => {
70 crate::pkg::registry::add_package(
71 registry_root,
72 name.as_deref(),
73 repo.as_deref()
74 )
75 }
76 RegistryCommands::AddAdvisory { package, repo } => {
77 crate::pkg::registry::add_advisory(
78 registry_root,
79 package.as_deref(),
80 repo.as_deref()
81 )
82 }
83 }
84}