turn it into subcommands

This commit is contained in:
tahahawa 2025-06-10 15:35:04 -04:00
parent 0f968000c3
commit b6f252e344
5 changed files with 2341 additions and 108 deletions

2220
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,7 @@ members = [
"opnsense-config",
"opnsense-config-xml",
"harmony_cli",
"k3d", "harmony_ci_cli",
"k3d", "harmony_composer",
]
[workspace.package]

View File

@ -1,79 +0,0 @@
use std::path::Path;
use std::process::Command;
use clap::Parser;
use clap::builder::ArgPredicate;
use inquire::Confirm;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
#[arg(short, long, default_value = "./harmony")]
harmony_path: String,
#[arg(short, long, default_value_t = false, group = "check")]
check: bool,
#[arg(short, long, default_value_t = false, group = "package")]
package: bool,
#[arg(short, long, default_value_t = false, group = "package")]
publish: bool,
#[arg(short, long, default_value_t = false, group = "deploy")]
deploy: bool,
#[arg(short, long, default_value_t = false, group = "deploy")]
staging: bool,
#[arg(short, long, default_value_t = false, group = "deploy")]
prod: bool,
#[arg(short, long, default_value_t = false, group = "deploy")]
smoke_test: bool,
}
fn main() {
let args = Args::parse();
let harmony_path = Path::new(&args.harmony_path).try_exists();
match harmony_path {
Ok(_path) => {
todo!("implement cargo check and compile harmony_cli/UI binary in harmony dir")
}
Err(_) => todo!("implement autodetect code"),
}
if args.check {
let check_script =
match Path::new(&format!("{}/{}", args.harmony_path, "check.sh")).try_exists() {
Ok(path) => path,
Err(_) => todo!("implement logic when couldn't find check script"),
};
let check_output = Command::new("sh")
.arg("-c")
.arg(check_script.to_string())
.output()
.expect("failed to run check script");
}
if args.package {
if args.publish {
todo!("implement publish logic");
}
todo!("implement packaging logic");
}
if args.deploy {
if args.staging {
todo!("implement staging deployment");
}
if args.prod {
todo!("implement prod deployment");
}
todo!("implement deployment logic");
}
}

View File

@ -1,5 +1,5 @@
[package]
name = "harmony_ci_cli"
name = "harmony_composer"
edition = "2024"
version.workspace = true
readme.workspace = true
@ -11,3 +11,5 @@ clap = { version = "4.5.35", features = ["derive"] }
inquire.workspace = true
tokio.workspace = true
env_logger.workspace = true
cargo = "0.88.0"
docker_utils = "0.2.3"

View File

@ -0,0 +1,144 @@
use std::path::Path;
use std::process::Command;
use clap::builder::ArgPredicate;
use clap::{Args, Parser, Subcommand};
use inquire::Confirm;
#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(propagate_version = true)]
struct GlobalArgs {
#[arg(long, default_value = "./harmony")]
harmony_path: String,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Check(CheckArgs),
Package(PackageArgs),
Deploy(DeployArgs),
All(AllArgs),
}
#[derive(Args)]
struct CheckArgs {
#[arg(long, default_value = "check.sh")]
check_script_path: String,
}
#[derive(Args)]
struct PackageArgs {
#[arg(long, default_value_t = false)]
package: bool,
#[arg(long, default_value_t = false)]
publish: bool,
}
#[derive(Args)]
struct DeployArgs {
#[arg(long, default_value_t = false)]
staging: bool,
#[arg(long, default_value_t = false)]
prod: bool,
#[arg(long, default_value_t = false)]
smoke_test: bool,
}
#[derive(Args)]
struct AllArgs {
#[arg(long, default_value = "check.sh")]
check_script_path: String,
#[arg(long, default_value_t = false)]
package: bool,
#[arg(long, default_value_t = false)]
publish: bool,
#[arg(long, default_value_t = false)]
staging: bool,
#[arg(long, default_value_t = false)]
prod: bool,
#[arg(long, default_value_t = false)]
smoke_test: bool,
}
fn main() {
let cli_args = GlobalArgs::parse();
let harmony_path = Path::new(&args.harmony_path).try_exists();
match harmony_path {
Ok(_path) => compile_harmony(args.harmony_path.clone()),
Err(_) => todo!("implement autodetect code"),
}
match cli_args.command {
Commands::Check(args) => {
let check_script = match Path::new(&format!("{}/{}", cli_args.harmony_path, "check.sh"))
.try_exists()
{
Ok(path) => path,
Err(_) => todo!("implement logic when couldn't find check script"),
};
let check_output = Command::new("sh")
.arg("-c")
.arg(check_script.to_string())
.output()
.expect("failed to run check script");
}
Commands::Package(args) => {
if args.publish {
todo!("implement publish logic");
}
todo!("implement packaging logic");
}
Commands::Deploy(args) => {
if args.staging {
todo!("implement staging deployment");
}
if args.prod {
todo!("implement prod deployment");
}
todo!("implement deployment logic");
}
Commands::All(args) => todo!(
"take all previous match arms and turn them into separate functions, and call them all one after the other"
),
}
}
fn compile_harmony(harmony_location: String) {
let gctx = cargo::util::context::GlobalContext::default().unwrap();
let cargo_exists = gctx.cargo_exe().unwrap().exists();
if cargo_exists {
let _cargo_build = cargo::ops::compile(
&cargo::core::Workspace::new(
&Path::new(&format!("{}/Cargo.toml", harmony_location))
.canonicalize()
.unwrap(),
&gctx,
)
.unwrap(),
&cargo::ops::CompileOptions::new(&gctx, cargo::core::compiler::CompileMode::Build)
.unwrap(),
)
.expect("build success");
return;
}
let _docker_util = docker_utils::DockerUtil::new().expect("Failed to create DockerUtil");
todo!("implement docker build container");
}