Skip to main content

harmont_cli/cli/
mod.rs

1pub mod plugin;
2pub mod run;
3pub mod version;
4
5pub use plugin::PluginCommand;
6pub use run::RunArgs;
7
8use std::path::PathBuf;
9
10use anyhow::Result;
11use clap::{Parser, Subcommand};
12
13use crate::context::RunContext;
14
15#[derive(Debug, Parser)]
16#[command(
17    name = "hm",
18    version,
19    about = "hm — CLI for the Harmont CI platform",
20    long_about = "hm is the command-line interface for Harmont.\n\n\
21                   Run `hm run` to push local code through a pipeline without committing.",
22    propagate_version = true,
23    arg_required_else_help = true,
24    disable_help_subcommand = true
25)]
26pub struct Cli {
27    /// Override the API base URL. Hidden flag — set `HARMONT_API_URL` instead.
28    #[arg(long, global = true, env = "HARMONT_API_URL", hide = true)]
29    pub api_url: Option<String>,
30
31    /// Enable verbose/debug logging.
32    #[arg(long, short, global = true)]
33    pub verbose: bool,
34
35    /// Disable colored output.
36    #[arg(long, global = true)]
37    pub no_color: bool,
38
39    #[command(subcommand)]
40    pub command: Command,
41}
42
43#[derive(Debug, Clone, Subcommand)]
44pub enum Command {
45    /// Run a pipeline locally via Docker.
46    Run(RunArgs),
47
48    /// Show hm version.
49    Version,
50
51    /// Manage plugins.
52    #[command(subcommand)]
53    Plugin(PluginCommand),
54
55    /// Manage harmont Docker image cache.
56    #[command(subcommand)]
57    Cache(CacheCommand),
58
59    /// Interact with the Harmont cloud API.
60    #[command(subcommand)]
61    Cloud(hm_plugin_cloud::cli::CloudCommand),
62}
63
64#[derive(Debug, Clone, Subcommand)]
65pub enum CacheCommand {
66    /// Save harmont Docker images to a cache directory.
67    Save(CacheSaveArgs),
68    /// Restore harmont Docker images from a cache directory.
69    Restore(CacheRestoreArgs),
70    /// Remove all cached workspaces and Docker images.
71    Clean,
72}
73
74#[derive(Debug, Clone, clap::Args)]
75pub struct CacheSaveArgs {
76    /// Directory to save image tars into.
77    pub dir: PathBuf,
78}
79
80#[derive(Debug, Clone, clap::Args)]
81pub struct CacheRestoreArgs {
82    /// Directory containing cached image tars.
83    pub dir: PathBuf,
84}
85
86/// Dispatch a parsed CLI command to the appropriate handler. Returns an exit code.
87///
88/// # Errors
89///
90/// Returns an error if the dispatched handler fails.
91pub async fn dispatch(command: Command, ctx: RunContext) -> Result<i32> {
92    match command {
93        Command::Run(args) => crate::commands::run::handle(args, ctx).await,
94        Command::Cache(cmd) => match cmd {
95            CacheCommand::Save(args) => crate::commands::cache::handle_save(&args.dir).await,
96            CacheCommand::Restore(args) => crate::commands::cache::handle_restore(&args.dir).await,
97            CacheCommand::Clean => crate::commands::cache::handle_clean().await,
98        },
99        Command::Version => version::run().await.map(|()| 0),
100        Command::Plugin(cmd) => plugin::run(cmd).await.map(|()| 0),
101        Command::Cloud(_cmd) => {
102            tracing::info!(
103                "Harmont Cloud is not yet available.\n\
104                 \n\
105                 Sign up for the waitlist at https://harmont.dev to get early access."
106            );
107            Ok(0)
108        }
109    }
110}