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 #[arg(long, global = true, env = "HARMONT_API_URL", hide = true)]
29 pub api_url: Option<String>,
30
31 #[arg(long, short, global = true)]
33 pub verbose: bool,
34
35 #[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(RunArgs),
47
48 Version,
50
51 #[command(subcommand)]
53 Plugin(PluginCommand),
54
55 #[command(subcommand)]
57 Cache(CacheCommand),
58
59 #[command(subcommand)]
61 Cloud(hm_plugin_cloud::cli::CloudCommand),
62}
63
64#[derive(Debug, Clone, Subcommand)]
65pub enum CacheCommand {
66 Save(CacheSaveArgs),
68 Restore(CacheRestoreArgs),
70 Clean,
72}
73
74#[derive(Debug, Clone, clap::Args)]
75pub struct CacheSaveArgs {
76 pub dir: PathBuf,
78}
79
80#[derive(Debug, Clone, clap::Args)]
81pub struct CacheRestoreArgs {
82 pub dir: PathBuf,
84}
85
86pub 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}