implement delete

This commit is contained in:
Cadey Ratio 2020-05-30 12:24:04 -04:00
parent 94a0431ed5
commit 7d4f184c33
8 changed files with 175 additions and 110 deletions

1
Cargo.lock generated
View File

@ -326,6 +326,7 @@ dependencies = [
"cli-table",
"comrak",
"git2",
"http",
"kankyo",
"reqwest",
"serde",

View File

@ -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"

View File

@ -1,7 +1,8 @@
#+TITLE: TODO
* Commands
** TODO delete
** DONE delete
CLOSED: [2020-05-30 Sat 12:23]
** TODO download
** TODO edit
** DONE info

34
src/cmd/delete.rs Normal file
View File

@ -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(())
}
}

97
src/cmd/info.rs Normal file
View File

@ -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<Release> = 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<Release> = 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<Row> = 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(())
}

2
src/cmd/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub(crate) mod delete;
pub(crate) mod info;

View File

@ -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<Release> {
let releases: Vec<Release> = cli
.get(format!("{}/api/v1/repos/{}/{}/releases", server, owner, repo).as_str())
.send()
.await?
.json()
.await?;
let mut release: Option<Release> = 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())
}

View File

@ -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<Client> {
pub(crate) fn client(c: &Common) -> Result<Client> {
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<Client> {
}
#[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<gitea::Release> = 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<gitea::Release> = 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<Row> = 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(())
}