feat: Initial import
This commit is contained in:
commit
5cd0635832
5 changed files with 2112 additions and 0 deletions
124
src/main.rs
Normal file
124
src/main.rs
Normal file
|
@ -0,0 +1,124 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
use forgejo_api::Auth::Token;
|
||||
use forgejo_api::Forgejo;
|
||||
use forgejo_api::structs::CreateReleaseOption;
|
||||
use url::Url;
|
||||
|
||||
pub mod config;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[clap(
|
||||
name = "forge-auto",
|
||||
version = "1.0",
|
||||
about = "Automate routine tasks in Forgejo"
|
||||
)]
|
||||
struct Cli {
|
||||
#[clap(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum Commands {
|
||||
/// A subcommand with its own subcommands
|
||||
#[clap(subcommand)]
|
||||
Release(ReleaseSubCommands),
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum ReleaseSubCommands {
|
||||
/// A sub-subcommand
|
||||
NewRelease {
|
||||
alias: String,
|
||||
tag: String,
|
||||
#[clap(long, short)]
|
||||
target: Option<String>,
|
||||
},
|
||||
|
||||
/// Another sub-subcommand
|
||||
ListReleases {
|
||||
#[clap(short, long)]
|
||||
output: String,
|
||||
},
|
||||
}
|
||||
|
||||
async fn handle_new_release(
|
||||
config: &config::Config,
|
||||
alias: &str,
|
||||
tag: &str,
|
||||
target: &Option<String>,
|
||||
) -> Result<(), String> {
|
||||
println!("Creating new release with alias: {}, tag: {}", alias, tag);
|
||||
|
||||
let repo = config.repos.get(alias).map_or_else(
|
||||
|| Err(format!("No repository found with alias: {}", alias)),
|
||||
|repo| {
|
||||
println!("Using repository: {} owned by {}", repo.repo, repo.owner);
|
||||
// Here you would typically call the Forgejo API to create a new release
|
||||
// For example:
|
||||
Ok(repo)
|
||||
},
|
||||
)?;
|
||||
|
||||
let token = Token(&repo.token);
|
||||
let url = Url::parse(&repo.hostname).map_err(|e| {
|
||||
eprintln!("Failed to parse URL: {}", e);
|
||||
e.to_string()
|
||||
})?;
|
||||
let forgejo = Forgejo::new(token, url).map_err(|e| {
|
||||
eprintln!("Failed to create Forgejo client: {}", e);
|
||||
e.to_string()
|
||||
})?;
|
||||
|
||||
let option = CreateReleaseOption {
|
||||
tag_name: tag.to_string(),
|
||||
name: Some(format!("Release {}", tag)),
|
||||
body: Some(format!("Release notes for {}", tag)),
|
||||
hide_archive_links: Some(false),
|
||||
target_commitish: Some(tag.to_string()),
|
||||
draft: None,
|
||||
prerelease: None,
|
||||
};
|
||||
|
||||
forgejo
|
||||
.repo_create_release(&repo.owner, &repo.repo, option)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
eprintln!("Failed to create release: {}", e);
|
||||
e.to_string()
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_another_sub_sub_command(output: &str) {
|
||||
println!("Executing AnotherSubSubCommand with output: {}", output);
|
||||
}
|
||||
|
||||
fn handle_another_command(option: &str) {
|
||||
println!("Executing AnotherCommand with option: {}", option);
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = Cli::parse();
|
||||
|
||||
let config = config::load_config("config.ini").expect("Failed to load config");
|
||||
|
||||
match args.command {
|
||||
Commands::Release(subcommand) => {
|
||||
match subcommand {
|
||||
ReleaseSubCommands::NewRelease { alias, tag, target } => {
|
||||
handle_new_release(&config, &alias, &tag, &target).await?
|
||||
}
|
||||
ReleaseSubCommands::ListReleases { output } => {
|
||||
handle_another_sub_sub_command(&output)
|
||||
}
|
||||
}
|
||||
// Uncomment the following lines if you want to use the dispatch macro
|
||||
// dispatch!(subcommand,
|
||||
// SubCommands::SubSubCommand { input } => handle_sub_sub_command(&input),
|
||||
// SubCommands::AnotherSubSubCommand { output } => handle_another_sub_sub_command(&output),
|
||||
// );
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue