From 7d4f184c33bb9105b34e284658355d6ca6f3dad8 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 30 May 2020 12:24:04 -0400 Subject: [PATCH] implement delete --- Cargo.lock | 1 + Cargo.toml | 1 + TODO.org | 3 +- src/cmd/delete.rs | 34 ++++++++++++++ src/cmd/info.rs | 97 ++++++++++++++++++++++++++++++++++++++ src/cmd/mod.rs | 2 + src/gitea.rs | 30 ++++++++++++ src/main.rs | 117 ++++------------------------------------------ 8 files changed, 175 insertions(+), 110 deletions(-) create mode 100644 src/cmd/delete.rs create mode 100644 src/cmd/info.rs create mode 100644 src/cmd/mod.rs diff --git a/Cargo.lock b/Cargo.lock index f0e7a61..3b18a13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -326,6 +326,7 @@ dependencies = [ "cli-table", "comrak", "git2", + "http", "kankyo", "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index 8ef25f9..d4637dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ anyhow = "1.0" cli-table = "0.3" comrak = "0.7" git2 = "0.13" +http = "0.2" kankyo = "0.3" reqwest = { version = "0.10", features = ["json"] } serde_json = "1.0" diff --git a/TODO.org b/TODO.org index 47f13f1..213e5be 100644 --- a/TODO.org +++ b/TODO.org @@ -1,7 +1,8 @@ #+TITLE: TODO * Commands -** TODO delete +** DONE delete + CLOSED: [2020-05-30 Sat 12:23] ** TODO download ** TODO edit ** DONE info diff --git a/src/cmd/delete.rs b/src/cmd/delete.rs new file mode 100644 index 0000000..f5520d1 --- /dev/null +++ b/src/cmd/delete.rs @@ -0,0 +1,34 @@ +use crate::{gitea::*, *}; +use anyhow::{anyhow, Result}; + +pub(crate) async fn run(common: Common) -> Result<()> { + if common.tag.is_none() { + return Err(anyhow!("requires --tag")); + } + let cli = client(&common)?; + let release = get_release_by_tag( + &cli, + &common.server, + &common.owner, + &common.repo, + &common.tag.unwrap(), + ) + .await?; + + let resp = cli + .delete( + format!( + "{}/api/v1/repos/{}/{}/releases/{}", + &common.server, &common.owner, &common.repo, release.id + ) + .as_str(), + ) + .send() + .await?; + + if resp.status() != http::StatusCode::from_u16(204)? { + Err(anyhow!("wanted 204, got {}", resp.status())) + } else { + Ok(()) + } +} diff --git a/src/cmd/info.rs b/src/cmd/info.rs new file mode 100644 index 0000000..c920d91 --- /dev/null +++ b/src/cmd/info.rs @@ -0,0 +1,97 @@ +use crate::{gitea::*, *}; +use anyhow::{anyhow, Result}; +use cli_table::{Cell, Row, Table}; + +pub(crate) async fn run(common: Common, json: bool) -> Result<()> { + let cli = client(&common)?; + + let releases: Vec = cli + .get( + format!( + "{}/api/v1/repos/{}/{}/releases", + &common.server, &common.owner, &common.repo + ) + .as_str(), + ) + .send() + .await? + .json() + .await?; + + match common.tag { + Some(tag) => { + let mut release: Option = None; + + for rls in releases { + if tag == rls.tag_name { + release = Some(rls); + } + } + + if release.is_none() { + return Err(anyhow!("tag {} not found", tag)); + } + + if json { + println!("{}", serde_json::to_string_pretty(&release)?); + } else { + let rls = release.unwrap(); + let table = Table::new( + vec![ + Row::new(vec![ + Cell::new(&"id", Default::default()), + Cell::new(&rls.id, Default::default()), + ]), + Row::new(vec![ + Cell::new(&"author", Default::default()), + Cell::new( + &format!("{} - {}", rls.author.full_name, rls.author.username), + Default::default(), + ), + ]), + Row::new(vec![ + Cell::new(&"tag", Default::default()), + Cell::new(&rls.tag_name, Default::default()), + ]), + Row::new(vec![ + Cell::new(&"created at", Default::default()), + Cell::new(&rls.created_at, Default::default()), + ]), + Row::new(vec![ + Cell::new(&"name", Default::default()), + Cell::new(&rls.name, Default::default()), + ]), + Row::new(vec![ + Cell::new(&"body", Default::default()), + Cell::new(&rls.body, Default::default()), + ]), + ], + Default::default(), + )?; + table.print_stdout()?; + } + } + None => { + if json { + println!("{}", serde_json::to_string_pretty(&releases)?); + } else { + let mut rows: Vec = vec![Row::new(vec![ + Cell::new(&"id", Default::default()), + Cell::new(&"tag", Default::default()), + Cell::new(&"created at", Default::default()), + Cell::new(&"commit", Default::default()), + Cell::new(&"author", Default::default()), + Cell::new(&"name", Default::default()), + ])]; + for release in releases { + rows.push(release.row()) + } + + let table = Table::new(rows, Default::default())?; + table.print_stdout()?; + } + } + } + + Ok(()) +} diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs new file mode 100644 index 0000000..2a2ce3a --- /dev/null +++ b/src/cmd/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod delete; +pub(crate) mod info; diff --git a/src/gitea.rs b/src/gitea.rs index 7e1b1ec..030a552 100644 --- a/src/gitea.rs +++ b/src/gitea.rs @@ -1,3 +1,4 @@ +use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -55,3 +56,32 @@ pub struct CreateRelease { pub tag_name: String, pub target_commitish: String, } + +pub(crate) async fn get_release_by_tag( + cli: &reqwest::Client, + server: &String, + owner: &String, + repo: &String, + tag: &String, +) -> Result { + let releases: Vec = cli + .get(format!("{}/api/v1/repos/{}/{}/releases", server, owner, repo).as_str()) + .send() + .await? + .json() + .await?; + + let mut release: Option = None; + + for rls in releases { + if *tag == rls.tag_name { + release = Some(rls); + } + } + + if release.is_none() { + return Err(anyhow!("tag {} not found", tag)); + } + + Ok(release.unwrap()) +} diff --git a/src/main.rs b/src/main.rs index 597ad61..007240b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,11 @@ use reqwest::{header, Client}; use std::path::PathBuf; use structopt::StructOpt; +mod cmd; mod gitea; #[derive(StructOpt, Debug)] -struct Common { +pub(crate) struct Common { /// The gitea server to connect to #[structopt(short, long, env = "GITEA_SERVER")] server: String, @@ -30,7 +31,7 @@ struct Common { // Name your user agent after your app? static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),); -fn client(c: &Common) -> Result { +pub(crate) fn client(c: &Common) -> Result { let mut headers = header::HeaderMap::new(); let auth = format!("token {}", &c.token); let auth = auth.as_str(); @@ -42,7 +43,7 @@ fn client(c: &Common) -> Result { } #[derive(StructOpt, Debug)] -struct ReleaseMeta { +pub(crate) struct ReleaseMeta { /// Release name #[structopt(short, long, default_value = "")] name: String, @@ -56,7 +57,7 @@ struct ReleaseMeta { #[derive(StructOpt, Debug)] #[structopt(about = "Gitea release assistant")] -enum Cmd { +pub(crate) enum Cmd { /// Delete a given release from Gitea Delete { #[structopt(flatten)] @@ -122,111 +123,9 @@ async fn main() -> Result<()> { let cmd = Cmd::from_args(); match cmd { - Cmd::Delete { common } => { - let cli = client(&common); - } + Cmd::Delete { common } => cmd::delete::run(common).await, + Cmd::Info { common, json } => cmd::info::run(common, json).await, - Cmd::Info { common, json } => { - use cli_table::{Cell, Row, Table}; - let cli = client(&common)?; - - let releases: Vec = cli - .get( - format!( - "{}/api/v1/repos/{}/{}/releases", - &common.server, &common.owner, &common.repo - ) - .as_str(), - ) - .send() - .await? - .json() - .await?; - - match common.tag { - Some(tag) => { - let mut release: Option = None; - - for rls in releases { - if tag == rls.tag_name { - release = Some(rls); - } - } - - if release.is_none() { - return Err(anyhow!("tag {} not found", tag)); - } - - if json { - println!("{}", serde_json::to_string_pretty(&release)?); - } else { - let rls = release.unwrap(); - let table = Table::new( - vec![ - Row::new(vec![ - Cell::new(&"id", Default::default()), - Cell::new(&rls.id, Default::default()), - ]), - Row::new(vec![ - Cell::new(&"author", Default::default()), - Cell::new( - &format!( - "{} - {}", - rls.author.full_name, rls.author.username - ), - Default::default(), - ), - ]), - Row::new(vec![ - Cell::new(&"tag", Default::default()), - Cell::new(&rls.tag_name, Default::default()), - ]), - Row::new(vec![ - Cell::new(&"created at", Default::default()), - Cell::new(&rls.created_at, Default::default()), - ]), - Row::new(vec![ - Cell::new(&"name", Default::default()), - Cell::new(&rls.name, Default::default()), - ]), - Row::new(vec![ - Cell::new(&"body", Default::default()), - Cell::new(&rls.body, Default::default()), - ]), - ], - Default::default(), - )?; - table.print_stdout()?; - } - } - None => { - if json { - println!("{}", serde_json::to_string_pretty(&releases)?); - } else { - let mut rows: Vec = vec![Row::new(vec![ - Cell::new(&"id", Default::default()), - Cell::new(&"tag", Default::default()), - Cell::new(&"created at", Default::default()), - Cell::new(&"commit", Default::default()), - Cell::new(&"author", Default::default()), - Cell::new(&"name", Default::default()), - ])]; - for release in releases { - rows.push(release.row()) - } - - let table = Table::new(rows, Default::default())?; - table.print_stdout()?; - } - } - } - } - - _ => { - println!("{:?}", cmd); - println!("not implemented yet") - } + _ => Err(anyhow!("not implemented yet")), } - - Ok(()) }