Skip to main content

zoi_cli/cmd/
helper.rs

1use anyhow::Result;
2use clap::{Parser, Subcommand};
3
4#[derive(Parser, Debug)]
5pub struct HelperCommand {
6    #[command(subcommand)]
7    pub command: HelperCommands,
8}
9
10#[derive(Subcommand, Debug)]
11pub enum HelperCommands {
12    /// Get a hash of a local file or a file from a URL
13    GetHash(GetHashCommand),
14
15    /// Validate a Zoi specification file (e.g. registries.json, repo.yaml, advisories.json)
16    #[command(alias = "val")]
17    Validate(ValidateCommand),
18
19    /// Internal: Perform escalated installation of a package node (requires root)
20    #[command(hide = true)]
21    ElevateInstallNode(ElevateInstallNodeCommand),
22
23    /// Internal: Perform escalated uninstallation of a package (requires root)
24    #[command(hide = true)]
25    ElevateUninstall(ElevateUninstallCommand),
26}
27
28#[derive(Parser, Debug)]
29pub struct ElevateInstallNodeCommand {
30    /// Path to the JSON file containing the serialized InstallNode
31    #[arg(long)]
32    pub node_json: std::path::PathBuf,
33    /// Path to the package archive (.zpa)
34    #[arg(long)]
35    pub archive: std::path::PathBuf,
36    /// The install method used (e.g. "source", "pre-compiled")
37    #[arg(long)]
38    pub install_method: String,
39    /// Automatically answer yes to prompts
40    #[arg(long)]
41    pub yes: bool,
42    /// Whether to create shims for binaries
43    #[arg(long)]
44    pub link_bins: bool,
45}
46
47#[derive(Parser, Debug)]
48pub struct ElevateUninstallCommand {
49    /// Path to the JSON file containing the serialized InstallManifest
50    #[arg(long)]
51    pub manifest_json: std::path::PathBuf,
52    /// Automatically answer yes to prompts
53    #[arg(long)]
54    pub yes: bool,
55}
56
57#[derive(Parser, Debug)]
58pub struct GetHashCommand {
59    /// The local file path or URL to hash
60    #[arg(required = true)]
61    pub source: String,
62
63    /// The hash algorithm to use
64    #[arg(long, value_enum, default_value = "sha512")]
65    pub hash: HashAlgorithm,
66}
67
68#[derive(Parser, Debug)]
69pub struct ValidateCommand {
70    /// The local file path to validate
71    #[arg(required = true)]
72    pub file: std::path::PathBuf,
73}
74
75#[derive(clap::ValueEnum, Clone, Debug, Copy)]
76pub enum HashAlgorithm {
77    Sha512,
78    Sha256,
79}
80
81pub fn run(args: HelperCommand) -> Result<()> {
82    match args.command {
83        HelperCommands::GetHash(cmd) => {
84            let hash_type = match cmd.hash {
85                HashAlgorithm::Sha512 => crate::pkg::helper::HashType::Sha512,
86                HashAlgorithm::Sha256 => crate::pkg::helper::HashType::Sha256,
87            };
88            let hash = crate::pkg::helper::get_hash(&cmd.source, hash_type)?;
89            println!("{}", hash);
90            Ok(())
91        }
92        HelperCommands::Validate(cmd) => crate::pkg::helper::validate::run(&cmd.file),
93        HelperCommands::ElevateInstallNode(cmd) => crate::pkg::helper::elevate_install_node(&cmd),
94        HelperCommands::ElevateUninstall(cmd) => crate::pkg::helper::elevate_uninstall(&cmd),
95    }
96}