use anyhow::Result;
use clap::{Parser, Subcommand};
#[derive(Parser, Debug)]
pub struct HelperCommand {
#[command(subcommand)]
pub command: HelperCommands,
}
#[derive(Subcommand, Debug)]
pub enum HelperCommands {
GetHash(GetHashCommand),
#[command(alias = "val")]
Validate(ValidateCommand),
#[command(hide = true)]
ElevateInstallNode(ElevateInstallNodeCommand),
#[command(hide = true)]
ElevateUninstall(ElevateUninstallCommand),
}
#[derive(Parser, Debug)]
pub struct ElevateInstallNodeCommand {
#[arg(long)]
pub node_json: std::path::PathBuf,
#[arg(long)]
pub archive: std::path::PathBuf,
#[arg(long)]
pub install_method: String,
#[arg(long)]
pub yes: bool,
#[arg(long)]
pub link_bins: bool,
}
#[derive(Parser, Debug)]
pub struct ElevateUninstallCommand {
#[arg(long)]
pub manifest_json: std::path::PathBuf,
#[arg(long)]
pub yes: bool,
}
#[derive(Parser, Debug)]
pub struct GetHashCommand {
#[arg(required = true)]
pub source: String,
#[arg(long, value_enum, default_value = "sha512")]
pub hash: HashAlgorithm,
}
#[derive(Parser, Debug)]
pub struct ValidateCommand {
#[arg(required = true)]
pub file: std::path::PathBuf,
}
#[derive(clap::ValueEnum, Clone, Debug, Copy)]
pub enum HashAlgorithm {
Sha512,
Sha256,
}
pub fn run(args: HelperCommand) -> Result<()> {
match args.command {
HelperCommands::GetHash(cmd) => {
let hash_type = match cmd.hash {
HashAlgorithm::Sha512 => crate::pkg::helper::HashType::Sha512,
HashAlgorithm::Sha256 => crate::pkg::helper::HashType::Sha256,
};
let hash = crate::pkg::helper::get_hash(&cmd.source, hash_type)?;
println!("{}", hash);
Ok(())
}
HelperCommands::Validate(cmd) => crate::pkg::helper::validate::run(&cmd.file),
HelperCommands::ElevateInstallNode(cmd) => crate::pkg::helper::elevate_install_node(&cmd),
HelperCommands::ElevateUninstall(cmd) => crate::pkg::helper::elevate_uninstall(&cmd),
}
}