gitea-release/src/main.rs

144 lines
4.2 KiB
Rust
Raw Normal View History

2020-05-30 14:57:18 +00:00
use anyhow::{anyhow, Result};
use reqwest::{header, Client};
use std::path::PathBuf;
use structopt::StructOpt;
2020-05-31 16:29:43 +00:00
mod changelog;
2020-05-30 16:24:04 +00:00
mod cmd;
2020-05-30 14:57:18 +00:00
mod gitea;
#[derive(StructOpt, Debug)]
2020-05-30 16:24:04 +00:00
pub(crate) struct Common {
2020-05-30 14:57:18 +00:00
/// The gitea server to connect to
#[structopt(short, long, env = "GITEA_SERVER")]
server: String,
/// The gitea token to authenticate with
#[structopt(long, env = "GITEA_TOKEN")]
token: String,
/// The gitea user to authenticate as
#[structopt(short, long, env = "GITEA_AUTH_USER")]
auth_user: String,
/// The owner of the gitea repo
#[structopt(short, long, env = "GITEA_OWNER")]
owner: String,
/// The gitea repo to operate on
#[structopt(short, long, env = "GITEA_REPO")]
repo: String,
}
// Name your user agent after your app?
2020-05-30 18:27:49 +00:00
static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
2020-05-30 14:57:18 +00:00
2020-05-30 16:24:04 +00:00
pub(crate) fn client(c: &Common) -> Result<Client> {
2020-05-30 14:57:18 +00:00
let mut headers = header::HeaderMap::new();
let auth = format!("token {}", &c.token);
let auth = auth.as_str();
headers.insert(header::AUTHORIZATION, header::HeaderValue::from_str(auth)?);
Ok(Client::builder()
.user_agent(APP_USER_AGENT)
.default_headers(headers)
.build()?)
}
#[derive(StructOpt, Debug)]
2020-05-30 16:24:04 +00:00
pub(crate) struct ReleaseMeta {
2020-05-30 14:57:18 +00:00
/// Release name
2020-05-30 18:50:21 +00:00
#[structopt(short, long)]
name: Option<String>,
2020-05-30 14:57:18 +00:00
/// Draft release
#[structopt(long)]
draft: bool,
/// Pre-release (not suitable for production)
#[structopt(short, long)]
pre_release: bool,
}
#[derive(StructOpt, Debug)]
#[structopt(about = "Gitea release assistant")]
2020-05-30 16:24:04 +00:00
pub(crate) enum Cmd {
2020-05-30 14:57:18 +00:00
/// Delete a given release from Gitea
Delete {
#[structopt(flatten)]
common: Common,
2020-05-30 18:50:21 +00:00
/// The version tag to operate on
#[structopt(short, long)]
tag: String,
2020-05-30 14:57:18 +00:00
},
/// Downloads release artifacts
Download {
#[structopt(flatten)]
common: Common,
/// Folder to download release artifacts to
#[structopt(short, long)]
2020-05-30 18:27:49 +00:00
fname: Option<PathBuf>,
2020-05-30 18:50:21 +00:00
/// The version tag to operate on
#[structopt(short, long)]
tag: String,
2020-05-30 14:57:18 +00:00
},
/// Edits a release's description, name and other flags
Edit {
#[structopt(flatten)]
common: Common,
/// Release description
2020-05-30 18:50:21 +00:00
#[structopt(short, long)]
description: Option<String>,
2020-05-30 14:57:18 +00:00
#[structopt(flatten)]
release_meta: ReleaseMeta,
2020-05-30 18:50:21 +00:00
/// The version tag to operate on
#[structopt(short, long)]
tag: String,
2020-05-30 14:57:18 +00:00
},
/// Gets release info
Info {
#[structopt(flatten)]
common: Common,
#[structopt(long, short)]
json: bool,
2020-05-30 18:50:21 +00:00
/// The version tag to operate on
#[structopt(short, long)]
tag: Option<String>,
2020-05-30 14:57:18 +00:00
},
/// Create a new tag and release on Gitea
Release {
#[structopt(flatten)]
common: Common,
/// Changelog file to read from to create the release description
#[structopt(short, long, default_value = "./CHANGELOG.md")]
changelog: PathBuf,
#[structopt(flatten)]
release_meta: ReleaseMeta,
},
/// Uploads release artifacts to Gitea
Upload {
#[structopt(flatten)]
common: Common,
/// The location of the file on the disk
#[structopt(short, long)]
fname: PathBuf,
2020-05-30 19:15:18 +00:00
/// The version tag to operate on
#[structopt(short, long)]
tag: String,
2020-05-30 14:57:18 +00:00
},
}
#[tokio::main]
async fn main() -> Result<()> {
let _ = kankyo::init();
let cmd = Cmd::from_args();
match cmd {
2020-05-30 18:50:21 +00:00
Cmd::Delete { common, tag } => cmd::delete::run(common, tag).await,
Cmd::Download { common, fname, tag } => cmd::download::run(common, fname, tag).await,
Cmd::Edit {
common,
description,
release_meta,
tag,
} => cmd::edit::run(common, description, release_meta, tag).await,
Cmd::Info { common, json, tag } => cmd::info::run(common, json, tag).await,
2020-05-30 19:17:08 +00:00
Cmd::Upload { common, fname, tag } => cmd::upload::run(common, fname, tag).await,
2020-05-30 14:57:18 +00:00
2020-05-30 16:24:04 +00:00
_ => Err(anyhow!("not implemented yet")),
2020-05-30 14:57:18 +00:00
}
}