Rewrite rust (#38)
Co-authored-by: kjuulh <contact@kjuulh.io> Reviewed-on: #38
This commit is contained in:
2
crates/octopush_cli/.gitignore
vendored
Normal file
2
crates/octopush_cli/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/target
|
||||
/Cargo.lock
|
16
crates/octopush_cli/Cargo.toml
Normal file
16
crates/octopush_cli/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "octopush_cli"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
octopush_infra = { path = "../octopush_infra" }
|
||||
octopush_core = { path = "../octopush_core" }
|
||||
|
||||
eyre = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
|
||||
clap = { version = "4.0.18", features = ["env"] }
|
100
crates/octopush_cli/src/commands/execute.rs
Normal file
100
crates/octopush_cli/src/commands/execute.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{Arg, ArgAction, ArgMatches, Command};
|
||||
use octopush_core::{
|
||||
git::{git::LocalGitProviderOptions, gitea::client::DefaultGiteaClientOptions},
|
||||
schema,
|
||||
};
|
||||
use octopush_infra::service_register::ServiceRegister;
|
||||
|
||||
pub fn execute_cmd() -> Command {
|
||||
Command::new("execute")
|
||||
.about("execute a certain action")
|
||||
.arg(
|
||||
Arg::new("action")
|
||||
.long("action")
|
||||
.short('a')
|
||||
.action(ArgAction::Set)
|
||||
.help("action path to your local octopush.yaml file")
|
||||
.long_help("action path to your local octopush.yaml file")
|
||||
.default_value(".")
|
||||
.required(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("gitea-api-token")
|
||||
.long("gitea-api-token")
|
||||
.action(ArgAction::Set)
|
||||
.env("GITEA_API_TOKEN")
|
||||
.required(false),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("gitea-username")
|
||||
.long("gitea-username")
|
||||
.action(ArgAction::Set)
|
||||
.env("GITEA_USERNAME")
|
||||
.required(false),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("gitea-url")
|
||||
.long("gitea-url")
|
||||
.action(ArgAction::Set)
|
||||
.env("GITEA_URL")
|
||||
.required(false),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn execute_subcommand(args: &ArgMatches) -> eyre::Result<()> {
|
||||
let action = args
|
||||
.get_one::<String>("action")
|
||||
.ok_or(eyre::anyhow!("--action is required"))?;
|
||||
|
||||
let gitea_http_token = args.get_one::<String>("gitea-api-token");
|
||||
let gitea_username = args.get_one::<String>("gitea-username");
|
||||
let gitea_url = args.get_one::<String>("gitea-url");
|
||||
|
||||
let service_register = ServiceRegister::new(
|
||||
LocalGitProviderOptions { http_auth: None },
|
||||
DefaultGiteaClientOptions {
|
||||
url: gitea_url.map(|g| g.clone()).unwrap_or("".into()),
|
||||
basicauth: gitea_username
|
||||
.zip(gitea_http_token)
|
||||
.map(|(u, ht)| format!("{}:{}", u, ht))
|
||||
.map(|t| t.clone()),
|
||||
},
|
||||
);
|
||||
|
||||
let action_path: PathBuf = action.into();
|
||||
|
||||
let schema = service_register
|
||||
.schema_parser
|
||||
.parse_file(action_path.join("octopush.yml"))
|
||||
.await?;
|
||||
|
||||
match schema {
|
||||
schema::models::Schema::Action {
|
||||
name,
|
||||
select,
|
||||
action,
|
||||
} => {
|
||||
tracing::debug!(name, "running action");
|
||||
|
||||
if let Some(git) = &select.git {
|
||||
service_register
|
||||
.git_selector
|
||||
.run(git, &action_path, &action)
|
||||
.await?;
|
||||
}
|
||||
|
||||
if let Some(gitea) = &select.gitea {
|
||||
service_register
|
||||
.gitea_selector
|
||||
.run(gitea, &action_path, &action)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
service_register.cleanup().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
1
crates/octopush_cli/src/commands/mod.rs
Normal file
1
crates/octopush_cli/src/commands/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod execute;
|
39
crates/octopush_cli/src/lib.rs
Normal file
39
crates/octopush_cli/src/lib.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
mod commands;
|
||||
|
||||
use clap::Command;
|
||||
|
||||
const VERSION: &str = "1.0.0";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OctopushCli {
|
||||
cmd: clap::Command,
|
||||
}
|
||||
|
||||
impl OctopushCli {
|
||||
pub fn new() -> Self {
|
||||
let cmd = Command::new("octopush")
|
||||
.version(VERSION)
|
||||
.author("Kasper J. Hermansen <contact@kjuulh.io>")
|
||||
.about("Your cute action executor")
|
||||
.propagate_version(true)
|
||||
.subcommand_required(true)
|
||||
.subcommand(commands::execute::execute_cmd());
|
||||
|
||||
Self { cmd }
|
||||
}
|
||||
|
||||
pub async fn execute(self) -> eyre::Result<()> {
|
||||
let matches = self.cmd.get_matches();
|
||||
|
||||
match matches.subcommand() {
|
||||
Some(("execute", execute_sub)) => {
|
||||
tracing::debug!("executing subcommand 'execute'");
|
||||
commands::execute::execute_subcommand(execute_sub).await?;
|
||||
}
|
||||
Some(_) => return Err(eyre::anyhow!("unknown subcommand, please see --help")),
|
||||
None => return Err(eyre::anyhow!("no subcommand specified")),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user