feat: enable upload of templates
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
@@ -10,3 +10,6 @@ tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
clap.workspace = true
|
||||
dotenv.workspace = true
|
||||
reqwest = { version = "0.12.2", features = ["json"]}
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
serde_json = "1.0.115"
|
||||
|
@@ -1,4 +1,6 @@
|
||||
use anyhow::Context;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
#[derive(Parser)]
|
||||
@@ -10,7 +12,19 @@ struct Command {
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Hello {},
|
||||
Upload {
|
||||
#[arg(long = "template")]
|
||||
template: String,
|
||||
|
||||
#[arg(long = "drone-token", env = "DRONE_TOKEN")]
|
||||
drone_token: String,
|
||||
|
||||
#[arg(long = "drone-host", env = "DRONE_HOST")]
|
||||
drone_host: String,
|
||||
|
||||
#[arg(long = "drone-user", env = "DRONE_USER")]
|
||||
drone_user: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@@ -19,10 +33,86 @@ async fn main() -> anyhow::Result<()> {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let cli = Command::parse();
|
||||
tracing::debug!("Starting cli");
|
||||
tracing::debug!("starting cli");
|
||||
|
||||
if let Some(Commands::Hello { }) = cli.command {
|
||||
println!("Hello!")
|
||||
if let Some(Commands::Upload {
|
||||
template,
|
||||
drone_token,
|
||||
drone_host,
|
||||
drone_user,
|
||||
}) = cli.command
|
||||
{
|
||||
tracing::info!(template, drone_host, "executing drone upload");
|
||||
|
||||
let template_path = PathBuf::from(&template);
|
||||
if !template_path.exists() {
|
||||
anyhow::bail!("template: {} doesn't exist", template)
|
||||
}
|
||||
|
||||
let template_name = template_path
|
||||
.file_name()
|
||||
.ok_or(anyhow!(
|
||||
"template: {} didn't include a proper filename",
|
||||
&template
|
||||
))?
|
||||
.to_str()
|
||||
.expect("filename to be utf8");
|
||||
let template_data = tokio::fs::read_to_string(&template_path).await?;
|
||||
|
||||
let template_url = format!(
|
||||
"{}/api/templates/{}/{}",
|
||||
drone_host.trim_end_matches('/'),
|
||||
drone_user,
|
||||
template_name
|
||||
);
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let res = client
|
||||
.put(&template_url)
|
||||
.bearer_auth(&drone_token)
|
||||
.json(&serde_json::json!({
|
||||
"data": &template_data,
|
||||
}))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if let Err(e) = res.error_for_status() {
|
||||
match e.status() {
|
||||
Some(reqwest::StatusCode::NOT_FOUND) => {
|
||||
tracing::debug!("template was not found attemping creation instead");
|
||||
|
||||
let template_url = format!(
|
||||
"{}/api/templates/{}",
|
||||
drone_host.trim_end_matches('/'),
|
||||
drone_user,
|
||||
);
|
||||
|
||||
let res = client
|
||||
.post(&template_url)
|
||||
.bearer_auth(&drone_token)
|
||||
.json(&serde_json::json!({
|
||||
"name": &template_name,
|
||||
"data": &template_data,
|
||||
}))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if let Err(e) = res.error_for_status() {
|
||||
anyhow::bail!(e)
|
||||
}
|
||||
|
||||
tracing::debug!("template creation successful");
|
||||
}
|
||||
Some(status) => {
|
||||
anyhow::bail!("error: {}, failed to upload template: {}", e, status)
|
||||
}
|
||||
None => {
|
||||
anyhow::bail!(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!(template, "successfully updated template");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
Reference in New Issue
Block a user