Compare commits
No commits in common. "0.3.0" and "master" have entirely different histories.
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,4 @@
|
|||||||
/target
|
/target
|
||||||
.vscode/
|
.vscode/
|
||||||
config.json
|
config.json
|
||||||
.idea/
|
Cargo.lock
|
1459
Cargo.lock
generated
1459
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "gt"
|
name = "gt"
|
||||||
version = "0.3.0"
|
version = "0.2.0"
|
||||||
authors = ["Wyatt J. Miller <wjmiller2016@gmail.com>"]
|
authors = ["Wyatt J. Miller <wjmiller2016@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "A Gitea CLI client"
|
description = "A Gitea CLI client"
|
||||||
@ -11,7 +11,6 @@ license-file = "LICENSE"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = "0.22.1"
|
|
||||||
clap = "2.33.1"
|
clap = "2.33.1"
|
||||||
colored = "2.0.0"
|
colored = "2.0.0"
|
||||||
config = "0.11.0"
|
config = "0.11.0"
|
||||||
|
43
src/error.rs
43
src/error.rs
@ -1,43 +0,0 @@
|
|||||||
use std::error::Error;
|
|
||||||
use std::fmt;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum ErrorKind {
|
|
||||||
BadRequest(String),
|
|
||||||
Conflict(String),
|
|
||||||
ForbiddenRequest(String),
|
|
||||||
NotFound(String),
|
|
||||||
UnprocessiableRequest(String),
|
|
||||||
JsonError(String),
|
|
||||||
Other,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error for ErrorKind {}
|
|
||||||
|
|
||||||
impl fmt::Display for ErrorKind {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
ErrorKind::BadRequest(message) => {
|
|
||||||
write!(f, "Client error - please try again! {}", message)
|
|
||||||
}
|
|
||||||
ErrorKind::Conflict(message) => {
|
|
||||||
write!(f, "Client error - task is already underway! {}", message)
|
|
||||||
}
|
|
||||||
ErrorKind::ForbiddenRequest(message) => write!(
|
|
||||||
f,
|
|
||||||
"Client error - unauthorized. Please try again! {}",
|
|
||||||
message
|
|
||||||
),
|
|
||||||
ErrorKind::NotFound(message) => write!(f, "Client error - not found! {}", message),
|
|
||||||
ErrorKind::UnprocessiableRequest(message) => write!(
|
|
||||||
f,
|
|
||||||
"Client error - the request can't be processed. Please try again! {}",
|
|
||||||
message
|
|
||||||
),
|
|
||||||
ErrorKind::JsonError(message) => {
|
|
||||||
write!(f, "Client error - can't parse command! {}", message)
|
|
||||||
}
|
|
||||||
ErrorKind::Other => write!(f, "Other error that I did not anticipate"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
87
src/issue.rs
87
src/issue.rs
@ -2,12 +2,11 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use colored::*;
|
use colored::*;
|
||||||
use reqwest::blocking::Client;
|
use reqwest::blocking::Client;
|
||||||
use reqwest::{Response, StatusCode};
|
use reqwest::{StatusCode, Response};
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Serialize, Deserialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::error;
|
use crate::request::{Request, Authentication, AuthenticationType};
|
||||||
use crate::request::{AuthenticationType, Request};
|
|
||||||
use crate::util;
|
use crate::util;
|
||||||
|
|
||||||
pub struct Issue;
|
pub struct Issue;
|
||||||
@ -45,12 +44,6 @@ pub struct IssueResponse {
|
|||||||
pub repository: Repository,
|
pub repository: Repository,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct IssueErrorResponse {
|
|
||||||
pub message: String,
|
|
||||||
pub url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
@ -74,18 +67,30 @@ pub struct Repository {
|
|||||||
pub full_name: String,
|
pub full_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// trait IssueRequest {
|
||||||
|
// fn client() -> &'static Client;
|
||||||
|
// fn url_request_decision() -> String;
|
||||||
|
// fn arg_value_decision() -> Vec<&'static str>;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// impl IssueRequest for Issue {
|
||||||
|
// fn client() -> &'static Client {
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
impl Issue {
|
impl Issue {
|
||||||
pub fn new() -> Issue {
|
pub fn new() -> Issue {
|
||||||
Issue {}
|
Issue {}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_issue(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn create_issue(&self, request: &Request) {
|
||||||
let issue_title: String;
|
let issue_title: String;
|
||||||
let issue_description: String;
|
let issue_description: String;
|
||||||
let _issue_assignee: Option<String>;
|
let _issue_assignee: Option<String>;
|
||||||
let _issue_milestone: Option<String>;
|
let _issue_milestone: Option<String>;
|
||||||
let _issue_tags: Option<String>;
|
let _issue_tags: Option<String>;
|
||||||
|
|
||||||
let client = &request.client;
|
let client = &request.client;
|
||||||
let arg_value: Vec<&str> = request
|
let arg_value: Vec<&str> = request
|
||||||
.arg_value
|
.arg_value
|
||||||
@ -114,60 +119,51 @@ impl Issue {
|
|||||||
let response = client.post(&url).json(&map).send();
|
let response = client.post(&url).json(&map).send();
|
||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(repo) => match repo.status() {
|
Ok(repo) => {
|
||||||
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
|
match repo.status() {
|
||||||
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
|
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
|
||||||
StatusCode::UNPROCESSABLE_ENTITY => {
|
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
|
||||||
println!("{}", "Issue input validation failed!".red())
|
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Issue input validation failed!".red()),
|
||||||
|
_ => println!(),
|
||||||
}
|
}
|
||||||
_ => println!(),
|
|
||||||
},
|
},
|
||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_issue(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn list_issue(&self, request: &Request) {
|
||||||
let client = &request.client;
|
let client = &request.client;
|
||||||
let url = self.url_request_decision(&request);
|
let url = self.url_request_decision(&request);
|
||||||
let response = client.get(&url).send();
|
let response = client.get(&url).send();
|
||||||
|
|
||||||
// TODO: fix this to match context
|
// TODO: fix this to match context
|
||||||
match response {
|
match response {
|
||||||
Ok(repo) => match repo.status() {
|
Ok(repo) => {
|
||||||
StatusCode::CREATED => {
|
match repo.status() {
|
||||||
println!("{}", "Issue successfully created!".green());
|
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
|
||||||
Ok(())
|
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Issue input validation failed!".red()),
|
||||||
|
_ => println!(),
|
||||||
}
|
}
|
||||||
StatusCode::FORBIDDEN => {
|
|
||||||
let deserialized = repo.json().unwrap();
|
|
||||||
Err(error::ErrorKind::ForbiddenRequest(deserialized.message))
|
|
||||||
}
|
|
||||||
StatusCode::UNPROCESSABLE_ENTITY => {
|
|
||||||
let deserialized = repo.json().unwrap();
|
|
||||||
Err(error::ErrorKind::UnprocessiableRequest(
|
|
||||||
deserialized.message,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
_ => Err(error::ErrorKind::Other),
|
|
||||||
},
|
},
|
||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_issue(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn search_issue(&self, request: &Request) {
|
||||||
let client = &request.client;
|
let client = &request.client;
|
||||||
let url = self.url_request_decision(&request);
|
let url = self.url_request_decision(&request);
|
||||||
let response = client.get(&url).send();
|
let response = client.get(&url).send();
|
||||||
|
|
||||||
// TODO: fix this to match context
|
// TODO: fix this to match context
|
||||||
match response {
|
match response {
|
||||||
Ok(repo) => match repo.status() {
|
Ok(repo) => {
|
||||||
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
|
match repo.status() {
|
||||||
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
|
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
|
||||||
StatusCode::UNPROCESSABLE_ENTITY => {
|
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
|
||||||
println!("{}", "Issue input validation failed!".red())
|
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Issue input validation failed!".red()),
|
||||||
|
_ => println!(),
|
||||||
}
|
}
|
||||||
_ => println!(),
|
|
||||||
},
|
},
|
||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
}
|
}
|
||||||
@ -183,7 +179,7 @@ impl Issue {
|
|||||||
.values_of("search")
|
.values_of("search")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// TODO: fix the url formatting for basic auth
|
// TODO: fix the url formatting for basic auth
|
||||||
match &request.authentication.auth_type {
|
match &request.authentication.auth_type {
|
||||||
AuthenticationType::ApiToken => {
|
AuthenticationType::ApiToken => {
|
||||||
@ -194,7 +190,7 @@ impl Issue {
|
|||||||
repo = arg_value[1],
|
repo = arg_value[1],
|
||||||
api_token = request.authentication.credentials.1.as_ref().unwrap()
|
api_token = request.authentication.credentials.1.as_ref().unwrap()
|
||||||
)
|
)
|
||||||
}
|
},
|
||||||
AuthenticationType::BasicAuth => {
|
AuthenticationType::BasicAuth => {
|
||||||
format!(
|
format!(
|
||||||
"{request}/repos/{owner}/{repo}/issues?token={api_token}",
|
"{request}/repos/{owner}/{repo}/issues?token={api_token}",
|
||||||
@ -203,9 +199,10 @@ impl Issue {
|
|||||||
repo = arg_value[1],
|
repo = arg_value[1],
|
||||||
api_token = request.authentication.credentials.1.as_ref().unwrap()
|
api_token = request.authentication.credentials.1.as_ref().unwrap()
|
||||||
)
|
)
|
||||||
}
|
},
|
||||||
// this case _shouldn't_ happen but ya know
|
// this case _shouldn't_ happen but ya know
|
||||||
AuthenticationType::None => panic!("idk what happened man you wrote the code 🤷"),
|
AuthenticationType::None => panic!("idk what happened man you wrote the code 🤷")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
23
src/main.rs
23
src/main.rs
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
mod arg;
|
mod arg;
|
||||||
mod config;
|
mod config;
|
||||||
mod error;
|
|
||||||
mod generate;
|
mod generate;
|
||||||
mod issue;
|
mod issue;
|
||||||
mod pr;
|
mod pr;
|
||||||
@ -29,7 +28,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let auth = request::Authentication::new(&config);
|
let auth = request::Authentication::new(&config);
|
||||||
let request = auth.request_chooser(config.clone(), &matches); // TODO: get rid of the clone
|
let request = auth.request_chooser(config.clone(), matches);
|
||||||
|
|
||||||
match request.arg_value.subcommand() {
|
match request.arg_value.subcommand() {
|
||||||
("", None) => println!("No subcommand was given!"),
|
("", None) => println!("No subcommand was given!"),
|
||||||
@ -77,26 +76,6 @@ fn main() {
|
|||||||
issue.create_issue(&request);
|
issue.create_issue(&request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
("user", Some(user_matches)) => {
|
|
||||||
let user = user::User::new();
|
|
||||||
|
|
||||||
// TODO: match expression should be here
|
|
||||||
if user_matches.is_present("create") {
|
|
||||||
user.create_user(&request);
|
|
||||||
}
|
|
||||||
|
|
||||||
if user_matches.is_present("list") {
|
|
||||||
user.list_user(&request);
|
|
||||||
}
|
|
||||||
|
|
||||||
if user_matches.is_present("search") {
|
|
||||||
user.search_user(&request);
|
|
||||||
}
|
|
||||||
|
|
||||||
if user_matches.is_present("delete") {
|
|
||||||
user.delete_user(&request);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => println!("Huh?"),
|
_ => println!("Huh?"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
137
src/repo.rs
137
src/repo.rs
@ -1,16 +1,11 @@
|
|||||||
use std::{collections::HashMap, path::Path};
|
use std::{collections::HashMap, path::{Path, self}, file};
|
||||||
|
|
||||||
use colored::*;
|
use colored::*;
|
||||||
use git2::{build::RepoBuilder, Cred, CredentialType, RemoteCallbacks};
|
use git2::{Repository as Repo, build::RepoBuilder, Credentials, Cred, CredentialType, RemoteCallbacks, FetchOptions};
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::{request::Request, config::Configuration};
|
||||||
config::Configuration,
|
|
||||||
error,
|
|
||||||
request::Request,
|
|
||||||
util::{self, ErrorKind},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Repository;
|
pub struct Repository;
|
||||||
|
|
||||||
@ -63,12 +58,6 @@ pub struct RepositoryResponse {
|
|||||||
pub website: String,
|
pub website: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct RepositoryErrorResponse {
|
|
||||||
pub message: String,
|
|
||||||
pub url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct ExternalTracker {
|
pub struct ExternalTracker {
|
||||||
pub external_tracker_format: String,
|
pub external_tracker_format: String,
|
||||||
@ -113,7 +102,7 @@ impl Repository {
|
|||||||
Repository {}
|
Repository {}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_repo(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn create_repo(&self, request: &Request) {
|
||||||
let client = &request.client;
|
let client = &request.client;
|
||||||
let arg_value = request
|
let arg_value = request
|
||||||
.arg_value
|
.arg_value
|
||||||
@ -138,34 +127,24 @@ impl Repository {
|
|||||||
match response {
|
match response {
|
||||||
Ok(repo) => match repo.status() {
|
Ok(repo) => match repo.status() {
|
||||||
StatusCode::CREATED => {
|
StatusCode::CREATED => {
|
||||||
// TODO: implement error handling for the deserialization
|
|
||||||
let deserialized: RepositoryResponse = repo.json().unwrap();
|
let deserialized: RepositoryResponse = repo.json().unwrap();
|
||||||
println!("{}", "Repository successfully created!".green());
|
println!("{}", "Repository successfully created!".green());
|
||||||
println!("\tRepository name: {:0}\n\tRepository owner: {:1}\n\tRepository description: {:2}", deserialized.name, deserialized.owner.unwrap().full_name, deserialized.description);
|
println!("\tRepository name: {:0}\n\tRepository owner: {:1}\n\tRepository description: {:2}", deserialized.name, deserialized.owner.unwrap().full_name, deserialized.description);
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
StatusCode::BAD_REQUEST => {
|
|
||||||
// TODO: implement error handling for the deserialization
|
|
||||||
let deserialized: RepositoryErrorResponse = repo.json().unwrap();
|
|
||||||
Err(error::ErrorKind::BadRequest(deserialized.message))
|
|
||||||
}
|
|
||||||
StatusCode::CONFLICT => {
|
|
||||||
let deserialized: RepositoryErrorResponse = repo.json().unwrap();
|
|
||||||
Err(error::ErrorKind::Conflict(deserialized.message))
|
|
||||||
}
|
}
|
||||||
|
StatusCode::CONFLICT => println!("{}", "Repository already exists!".red()),
|
||||||
StatusCode::UNPROCESSABLE_ENTITY => {
|
StatusCode::UNPROCESSABLE_ENTITY => {
|
||||||
let deserialized: RepositoryErrorResponse = repo.json().unwrap();
|
println!("{}", "Repository input validation failed!".red())
|
||||||
Err(error::ErrorKind::UnprocessiableRequest(
|
|
||||||
deserialized.message,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
_ => Err(error::ErrorKind::Other),
|
_ => println!(
|
||||||
|
"Repository creation failed! HTTP status code: {}",
|
||||||
|
repo.status().as_str()
|
||||||
|
),
|
||||||
},
|
},
|
||||||
Err(_e) => Err(error::ErrorKind::Other),
|
Err(e) => panic!("{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_repo(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn delete_repo(&self, request: &Request) {
|
||||||
let client = &request.client;
|
let client = &request.client;
|
||||||
let arg_value: Vec<&str> = request
|
let arg_value: Vec<&str> = request
|
||||||
.arg_value
|
.arg_value
|
||||||
@ -187,13 +166,8 @@ impl Repository {
|
|||||||
|
|
||||||
match response {
|
match response {
|
||||||
Ok(repo) => match repo.status() {
|
Ok(repo) => match repo.status() {
|
||||||
StatusCode::NO_CONTENT => {
|
StatusCode::NO_CONTENT => println!("{}", "Respository successfully deleted!".green()),
|
||||||
println!("{}", "Respository successfully deleted!".green())
|
StatusCode::FORBIDDEN => println!("{}", "Repository deletion forbidden!".red()),
|
||||||
}
|
|
||||||
StatusCode::FORBIDDEN => println!(
|
|
||||||
"{}",
|
|
||||||
"Unable to authorize deletion. Please report this to the webmaster!".red()
|
|
||||||
),
|
|
||||||
_ => println!(
|
_ => println!(
|
||||||
"Repository deletion failed! Does the repository exist? HTTP status code: {}",
|
"Repository deletion failed! Does the repository exist? HTTP status code: {}",
|
||||||
repo.status().as_str()
|
repo.status().as_str()
|
||||||
@ -203,7 +177,7 @@ impl Repository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fork_repo(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn fork_repo(&self, request: &Request) {
|
||||||
let client = &request.client;
|
let client = &request.client;
|
||||||
let arg_item: Vec<&str> = request
|
let arg_item: Vec<&str> = request
|
||||||
.arg_value
|
.arg_value
|
||||||
@ -217,7 +191,6 @@ impl Repository {
|
|||||||
let mut map: HashMap<&str, &str> = HashMap::new();
|
let mut map: HashMap<&str, &str> = HashMap::new();
|
||||||
let user = request.authentication.credentials.0.as_ref().unwrap();
|
let user = request.authentication.credentials.0.as_ref().unwrap();
|
||||||
|
|
||||||
// TODO: use new url formatter
|
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{request}/repos/{owner}/{repo}/forks?token={api_token}",
|
"{request}/repos/{owner}/{repo}/forks?token={api_token}",
|
||||||
request = request.url.as_ref().unwrap(),
|
request = request.url.as_ref().unwrap(),
|
||||||
@ -237,27 +210,17 @@ impl Repository {
|
|||||||
println!("{}", "Repository forked successfully".green());
|
println!("{}", "Repository forked successfully".green());
|
||||||
println!("\tOriginal repository name: {:0?}\n\tOriginal repository owner: {:1?}\n\tForked repository name: {:2?}\n\tForked repository owner: {:3?}", deserialized.name, arg_item[0], deserialized.name, deserialized.owner.unwrap().full_name);
|
println!("\tOriginal repository name: {:0?}\n\tOriginal repository owner: {:1?}\n\tForked repository name: {:2?}\n\tForked repository owner: {:3?}", deserialized.name, arg_item[0], deserialized.name, deserialized.owner.unwrap().full_name);
|
||||||
}
|
}
|
||||||
StatusCode::INTERNAL_SERVER_ERROR => {
|
StatusCode::INTERNAL_SERVER_ERROR => println!("{}", "Repository already forked!".red()),
|
||||||
println!("{}", "Repository already forked!".red())
|
StatusCode::FORBIDDEN => println!("{}", "Repository fork forbidden!".red()),
|
||||||
}
|
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Repository fork input validation failed!".red()),
|
||||||
StatusCode::FORBIDDEN => println!(
|
|
||||||
"{}",
|
|
||||||
"Unable to authorize fork. Please report this to the webmaster!".red()
|
|
||||||
),
|
|
||||||
StatusCode::UNPROCESSABLE_ENTITY => {
|
|
||||||
println!("{}", "Repository fork input validation failed!".red())
|
|
||||||
}
|
|
||||||
StatusCode::NOT_FOUND => println!("{}", "Repository not found!"),
|
StatusCode::NOT_FOUND => println!("{}", "Repository not found!"),
|
||||||
_ => println!(
|
_ => println!("Repository creation failed! HTTP status code: {}", repo.status().as_str()),
|
||||||
"Repository creation failed! HTTP status code: {}",
|
|
||||||
repo.status().as_str()
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_repo(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn search_repo(&self, request: &Request) {
|
||||||
let client = &request.client;
|
let client = &request.client;
|
||||||
let arg_value = request
|
let arg_value = request
|
||||||
.arg_value
|
.arg_value
|
||||||
@ -266,7 +229,6 @@ impl Repository {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.value_of("search")
|
.value_of("search")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// TODO: use new url formatter
|
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{request}/repos/search?q={query}&token={api_token}",
|
"{request}/repos/search?q={query}&token={api_token}",
|
||||||
request = request.url.as_ref().unwrap(),
|
request = request.url.as_ref().unwrap(),
|
||||||
@ -289,28 +251,20 @@ impl Repository {
|
|||||||
println!("{}.\tRepository name: {:1}\n\tRepository owner: {:2}\n\tRepository description: {:3}\n", i + 1, data.name, data.owner.as_ref().unwrap().full_name, data.description)
|
println!("{}.\tRepository name: {:1}\n\tRepository owner: {:2}\n\tRepository description: {:3}\n", i + 1, data.name, data.owner.as_ref().unwrap().full_name, data.description)
|
||||||
}
|
}
|
||||||
|
|
||||||
println!(
|
println!("Total number of repositories indexed: {}", deserialized.data.iter().count())
|
||||||
"Total number of repositories indexed: {}",
|
|
||||||
deserialized.data.iter().count()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
false => println!("{}", "Repository searched doesn't exist!".red()),
|
false => println!("{}", "Repository searched doesn't exist!".red()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCode::NOT_FOUND => println!("{}", "Repository searched doesn't exist!".red()),
|
StatusCode::NOT_FOUND => println!("{}", "Repository searched doesn't exist!".red()),
|
||||||
StatusCode::UNPROCESSABLE_ENTITY => {
|
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Repository input validation failed!".red()),
|
||||||
println!("{}", "Repository input validation failed!".red())
|
_ => println!("Repository search failed! HTTP status code: {}", repo.status().as_str()),
|
||||||
}
|
|
||||||
_ => println!(
|
|
||||||
"Repository search failed! HTTP status code: {}",
|
|
||||||
repo.status().as_str()
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_repo(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn list_repo(&self, request: &Request) {
|
||||||
let client = &request.client;
|
let client = &request.client;
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{request}/repos/search?token={api_token}",
|
"{request}/repos/search?token={api_token}",
|
||||||
@ -338,13 +292,8 @@ impl Repository {
|
|||||||
false => println!("{}", "The authenticated user doesn't have any repositories. Why not create one?".yellow())
|
false => println!("{}", "The authenticated user doesn't have any repositories. Why not create one?".yellow())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatusCode::UNPROCESSABLE_ENTITY => {
|
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Repository input validation failed!".red()),
|
||||||
println!("{}", "Repository input validation failed!".red())
|
_ => println!("Repository search failed! HTTP status code: {}",repo.status().as_str()),
|
||||||
}
|
|
||||||
_ => println!(
|
|
||||||
"Repository search failed! HTTP status code: {}",
|
|
||||||
repo.status().as_str()
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
}
|
}
|
||||||
@ -353,6 +302,7 @@ impl Repository {
|
|||||||
pub fn push_to_remote(&self, request: &Request, config: &Configuration) {
|
pub fn push_to_remote(&self, request: &Request, config: &Configuration) {
|
||||||
// code to push to the remote server goes here
|
// code to push to the remote server goes here
|
||||||
// this is based on the user, the remote, and the branch
|
// this is based on the user, the remote, and the branch
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pull_from_remote(&self, request: &Request, config: &Configuration) {
|
pub fn pull_from_remote(&self, request: &Request, config: &Configuration) {
|
||||||
@ -380,15 +330,15 @@ impl Repository {
|
|||||||
let mut callbacks = RemoteCallbacks::new();
|
let mut callbacks = RemoteCallbacks::new();
|
||||||
callbacks.credentials(|_url, _user_from_url, _allowed_types| {
|
callbacks.credentials(|_url, _user_from_url, _allowed_types| {
|
||||||
let user = _user_from_url.unwrap_or("git");
|
let user = _user_from_url.unwrap_or("git");
|
||||||
|
|
||||||
if _allowed_types.contains(CredentialType::USERNAME) {
|
if _allowed_types.contains(CredentialType::USERNAME) {
|
||||||
return Cred::username(user);
|
return Cred::username(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if request.url_request() {
|
// if request.url_request() {
|
||||||
// Cred::ssh_key(
|
// Cred::ssh_key(
|
||||||
// user,
|
// user,
|
||||||
// Some(Path::new(&format!("{}/.ssh/id_ed25519.pub", std::env::var("HOME").unwrap()).as_str())),
|
// Some(Path::new(&format!("{}/.ssh/id_ed25519.pub", std::env::var("HOME").unwrap()).as_str())),
|
||||||
// Path::new(&format!("{}/.ssh/id_ed25519", std::env::var("HOME").unwrap()).as_str()),
|
// Path::new(&format!("{}/.ssh/id_ed25519", std::env::var("HOME").unwrap()).as_str()),
|
||||||
// None
|
// None
|
||||||
// )
|
// )
|
||||||
@ -399,31 +349,24 @@ impl Repository {
|
|||||||
// )
|
// )
|
||||||
// }
|
// }
|
||||||
Cred::userpass_plaintext(
|
Cred::userpass_plaintext(
|
||||||
request
|
request.authentication.credentials.0.as_ref().unwrap().as_str(),
|
||||||
.authentication
|
config.password.as_ref().unwrap().as_str()
|
||||||
.credentials
|
|
||||||
.0
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.as_str(),
|
|
||||||
config.password.as_ref().unwrap().as_str(),
|
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
builder.branch("master");
|
builder.branch("master");
|
||||||
match builder.clone(url.as_str(), Path::new(arg_value[1])) {
|
match builder.clone(url.as_str(), Path::new(arg_value[1])) {
|
||||||
Ok(_r) => Ok(println!("Repository cloned successfully!")),
|
Ok(_r) => Ok(println!("Repository cloned successfully!")),
|
||||||
Err(_e) => Err(println!("Repository clone failed!")),
|
Err(_e) => Err(println!("Repository clone failed!")),
|
||||||
_ => panic!("agh"),
|
_ => panic!("agh")
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_to_staging(&self, request: &Request, config: Configuration) {}
|
pub fn add_to_staging(&self, request: &Request, config: Configuration) {
|
||||||
|
|
||||||
pub fn create_commit(&self, request: &Request, config: Configuration) {}
|
}
|
||||||
}
|
|
||||||
|
pub fn create_commit(&self, request: &Request, config: Configuration) {
|
||||||
|
|
||||||
impl Default for Repository {
|
|
||||||
fn default() -> Self {
|
|
||||||
Repository::new()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,9 @@
|
|||||||
use std::{collections::HashMap, process, string::String};
|
use std::{collections::HashMap, process};
|
||||||
|
|
||||||
use base64::{engine::general_purpose::URL_SAFE, Engine as _};
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use reqwest::blocking::Client;
|
use reqwest::blocking::Client;
|
||||||
|
|
||||||
use crate::config::Configuration;
|
use crate::config::Configuration;
|
||||||
use crate::util;
|
|
||||||
|
|
||||||
pub enum RequestType {
|
|
||||||
Repository,
|
|
||||||
Issue,
|
|
||||||
PullRequest,
|
|
||||||
User,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum AuthenticationType {
|
pub enum AuthenticationType {
|
||||||
BasicAuth,
|
BasicAuth,
|
||||||
@ -20,14 +11,12 @@ pub enum AuthenticationType {
|
|||||||
None,
|
None,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive()]
|
|
||||||
pub struct Request<'a> {
|
pub struct Request<'a> {
|
||||||
pub client: Client,
|
pub client: Client,
|
||||||
pub arg_value: &'a ArgMatches<'a>,
|
pub arg_value: ArgMatches<'a>,
|
||||||
pub map: HashMap<String, String>,
|
pub map: HashMap<String, String>,
|
||||||
pub url: Option<String>,
|
pub url: Option<String>,
|
||||||
pub authentication: Authentication,
|
pub authentication: Authentication,
|
||||||
pub request_type: RequestType,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Authentication {
|
pub struct Authentication {
|
||||||
@ -41,49 +30,32 @@ impl<'a> Request<'a> {
|
|||||||
/// Public constructor for a request with a simple username and password
|
/// Public constructor for a request with a simple username and password
|
||||||
pub fn with_basic_request(
|
pub fn with_basic_request(
|
||||||
config: Configuration,
|
config: Configuration,
|
||||||
arg: &'a ArgMatches,
|
arg: ArgMatches,
|
||||||
auth: Authentication,
|
auth: Authentication,
|
||||||
) -> Request<'a> {
|
) -> Request {
|
||||||
Request {
|
Request {
|
||||||
client: Client::new(),
|
client: Client::new(),
|
||||||
arg_value: &arg,
|
arg_value: arg,
|
||||||
map: HashMap::new(),
|
map: HashMap::new(),
|
||||||
url: Some(format!("{}{}", config.base_url, config.base_api)),
|
url: Some(format!("{}{}", config.base_url, config.base_api)),
|
||||||
authentication: auth,
|
authentication: auth,
|
||||||
request_type: util::get_request_type(&arg).unwrap(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Public constructor for a request with an API token
|
/// Public constructor for a request with an API token
|
||||||
pub fn with_api_request(
|
pub fn with_api_request(
|
||||||
config: Configuration,
|
config: Configuration,
|
||||||
arg: &'a ArgMatches,
|
arg: ArgMatches,
|
||||||
auth: Authentication,
|
auth: Authentication,
|
||||||
) -> Request<'a> {
|
) -> Request {
|
||||||
Request {
|
Request {
|
||||||
client: Client::new(),
|
client: Client::new(),
|
||||||
arg_value: &arg,
|
arg_value: arg,
|
||||||
map: HashMap::new(),
|
map: HashMap::new(),
|
||||||
url: Some(format!("{}{}", config.base_url, config.base_api)),
|
url: Some(format!("{}{}", config.base_url, config.base_api)),
|
||||||
authentication: auth,
|
authentication: auth,
|
||||||
request_type: util::get_request_type(&arg).unwrap(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn auth_type(&self) -> String {
|
|
||||||
let key: &String = self.authentication.credentials.1.as_ref().unwrap();
|
|
||||||
match self.authentication.auth_type {
|
|
||||||
AuthenticationType::BasicAuth => URL_SAFE.encode(key),
|
|
||||||
AuthenticationType::ApiToken => key.to_string(),
|
|
||||||
AuthenticationType::None => {
|
|
||||||
// TODO: make a custom error and error here instead of panicing
|
|
||||||
// this shouldn't happen, ever
|
|
||||||
panic!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn url_builder(&self) -> String {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Authentication {
|
impl Authentication {
|
||||||
@ -142,22 +114,14 @@ impl Authentication {
|
|||||||
/// Public method that based on the what kind of authentication is being used, it can
|
/// Public method that based on the what kind of authentication is being used, it can
|
||||||
/// determine what kind of requesting method is going to be used. See the Request
|
/// determine what kind of requesting method is going to be used. See the Request
|
||||||
/// structure for more details.
|
/// structure for more details.
|
||||||
pub fn request_chooser<'a>(
|
pub fn request_chooser(self, config: Configuration, arg: ArgMatches<'static>) -> Request {
|
||||||
self,
|
|
||||||
config: Configuration,
|
|
||||||
arg: &'a ArgMatches<'static>,
|
|
||||||
) -> Request<'a> {
|
|
||||||
if let true = self.api_token {
|
if let true = self.api_token {
|
||||||
Request::with_api_request(config, &arg, self)
|
Request::with_api_request(config, arg.to_owned(), self)
|
||||||
} else {
|
} else {
|
||||||
match self.basic {
|
match self.basic {
|
||||||
true => Request::with_basic_request(config, &arg, self),
|
true => Request::with_basic_request(config, arg.to_owned(), self),
|
||||||
false => panic!(),
|
false => panic!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Authentication {
|
|
||||||
fn default() -> Self {}
|
|
||||||
}
|
|
||||||
|
182
src/user.rs
182
src/user.rs
@ -1,18 +1,11 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{request::Request, util};
|
||||||
error,
|
|
||||||
request::Request,
|
|
||||||
util::{self, ErrorKind},
|
|
||||||
};
|
|
||||||
use colored::*;
|
|
||||||
use reqwest::StatusCode;
|
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub struct User;
|
pub struct User;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
pub struct MutlipleUsers {
|
||||||
pub struct MultipleUsers {
|
|
||||||
pub data: Vec<UserResponse>,
|
pub data: Vec<UserResponse>,
|
||||||
pub ok: bool,
|
pub ok: bool,
|
||||||
}
|
}
|
||||||
@ -36,180 +29,33 @@ pub struct UserResponse {
|
|||||||
pub restricted: bool,
|
pub restricted: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct UserErrorResponse {
|
|
||||||
pub message: String,
|
|
||||||
pub url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl User {
|
impl User {
|
||||||
pub fn new() -> User {
|
pub fn new() -> User {
|
||||||
User {}
|
User {}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_user(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn create_user(request: &Request) {
|
||||||
let client = &request.client;
|
let client = &request.client;
|
||||||
let mut user_create_input: HashMap<String, String> = HashMap::new();
|
let arg_value = &request
|
||||||
let url = format!(
|
.arg_value
|
||||||
"{request}/admin/users?token={api_token}",
|
.subcommand()
|
||||||
request = request.url.as_ref().unwrap(),
|
.1
|
||||||
api_token = request.authentication.credentials.1.as_ref().unwrap()
|
.unwrap()
|
||||||
);
|
.value_of("create")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut user_create_input: HashMap<String, String> = HashMap::new();
|
||||||
let username = util::get_input(String::from("Please enter a username:"));
|
let username = util::get_input(String::from("Please enter a username:"));
|
||||||
let email = util::get_input(String::from("Please enter a email address:"));
|
let email = util::get_input(String::from("Please enter a email address:"));
|
||||||
let password = util::get_input(String::from("Please enter a password:"));
|
let password = util::get_input(String::from("Please enter a password:"));
|
||||||
|
|
||||||
user_create_input.insert("login".to_string(), username);
|
user_create_input.insert("login".to_string(), username);
|
||||||
user_create_input.insert("email".to_string(), email);
|
user_create_input.insert("email".to_string(), email);
|
||||||
user_create_input.insert("password".to_string(), password);
|
user_create_input.insert("password".to_string(), password);
|
||||||
|
|
||||||
let response = client.post(url.as_str()).json(&user_create_input).send();
|
|
||||||
// TODO: match return type
|
|
||||||
match response {
|
|
||||||
Ok(repo) => match repo.status() {
|
|
||||||
StatusCode::CREATED => {
|
|
||||||
let deserialized: UserResponse = repo.json().unwrap();
|
|
||||||
println!("{}", "User successfully created!".green());
|
|
||||||
println!(
|
|
||||||
"\tFull Name: {:0}\n\tUsername: {:1}\n\tEmail: {:2}\n",
|
|
||||||
deserialized.full_name, deserialized.login, deserialized.email
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
StatusCode::BAD_REQUEST => {
|
|
||||||
let deserialized: UserErrorResponse = repo.json().unwrap();
|
|
||||||
Err(error::ErrorKind::BadRequest(deserialized.message))
|
|
||||||
}
|
|
||||||
StatusCode::FORBIDDEN => {
|
|
||||||
let deserialized: UserErrorResponse = repo.json().unwrap();
|
|
||||||
Err(error::ErrorKind::ForbiddenRequest(deserialized.message))
|
|
||||||
}
|
|
||||||
_ => Err(error::ErrorKind::Other),
|
|
||||||
},
|
|
||||||
Err(_e) => Err(error::ErrorKind::Other),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_user(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
pub fn list_user() {}
|
||||||
let client = &request.client;
|
|
||||||
|
|
||||||
let url = format!(
|
pub fn search_user() {}
|
||||||
"{request}/users?token={api_token}",
|
|
||||||
request = request.url.as_ref().unwrap(),
|
|
||||||
api_token = request.authentication.credentials.1.as_ref().unwrap(),
|
|
||||||
);
|
|
||||||
let response = client.get(url).send();
|
|
||||||
match response {
|
|
||||||
Ok(repo) => {
|
|
||||||
match repo.status() {
|
|
||||||
StatusCode::OK => {
|
|
||||||
let deserialized: MultipleUsers = repo.json().unwrap();
|
|
||||||
|
|
||||||
match deserialized.data.len() != 0 {
|
pub fn delete_user() {}
|
||||||
true => {
|
|
||||||
println!("{}", "List of users found:");
|
|
||||||
|
|
||||||
for (i, data) in deserialized.data.iter().enumerate() {
|
|
||||||
println!("{}.\nUsername: {:1}\n\tEmail address: {:2}\n\tAdmin?: {:3}", i + 1, data.login, data.email, data.is_admin);
|
|
||||||
}
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"Total number of users indexed: {}",
|
|
||||||
deserialized.data.len()
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
false => {
|
|
||||||
println!("{}", "No users found :(");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
StatusCode::BAD_REQUEST => {
|
|
||||||
let deserialized: UserErrorResponse = repo.json().unwrap(); // TODO: handle
|
|
||||||
Err(error::ErrorKind::BadRequest(deserialized.message))
|
|
||||||
}
|
|
||||||
StatusCode::FORBIDDEN => {
|
|
||||||
let deserialzed: UserErrorResponse = repo.json().unwrap(); // TODO: handle errs
|
|
||||||
Err(error::ErrorKind::ForbiddenRequest(deserialzed.message))
|
|
||||||
}
|
|
||||||
_ => Err(error::ErrorKind::Other),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_e) => Err(error::ErrorKind::Other),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn search_user(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
|
||||||
let client = &request.client;
|
|
||||||
let arg_value = request
|
|
||||||
.arg_value
|
|
||||||
.subcommand()
|
|
||||||
.1
|
|
||||||
.unwrap()
|
|
||||||
.value_of("search")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let url = format!(
|
|
||||||
"{request}/users?q={query}&token={api_token}",
|
|
||||||
request = request.url.as_ref().unwrap(),
|
|
||||||
query = arg_value,
|
|
||||||
api_token = request.authentication.credentials.1.as_ref().unwrap(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let response = client.get(url.as_str()).send();
|
|
||||||
|
|
||||||
match response {
|
|
||||||
Ok(repo) => match repo.status() {
|
|
||||||
StatusCode::OK => Ok(()),
|
|
||||||
_ => Err(error::ErrorKind::Other),
|
|
||||||
},
|
|
||||||
Err(_e) => Err(error::ErrorKind::Other),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delete_user(&self, request: &Request) -> Result<(), error::ErrorKind> {
|
|
||||||
let client = &request.client;
|
|
||||||
let arg_value = request
|
|
||||||
.arg_value
|
|
||||||
.subcommand()
|
|
||||||
.1
|
|
||||||
.unwrap()
|
|
||||||
.value_of("search")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let url = format!(
|
|
||||||
"{request}/admin/users/{username}",
|
|
||||||
request = request.url.as_ref().unwrap(),
|
|
||||||
username = arg_value,
|
|
||||||
);
|
|
||||||
let response = client.delete(url.as_str()).send();
|
|
||||||
|
|
||||||
match response {
|
|
||||||
Ok(repo) => match repo.status() {
|
|
||||||
StatusCode::NO_CONTENT => {
|
|
||||||
println!("User successfully deleted!");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
StatusCode::FORBIDDEN => {
|
|
||||||
let deserialized: UserErrorResponse = repo.json().unwrap();
|
|
||||||
Err(error::ErrorKind::ForbiddenRequest(deserialized.message))
|
|
||||||
}
|
|
||||||
_ => Err(error::ErrorKind::Other),
|
|
||||||
},
|
|
||||||
Err(_e) => Err(error::ErrorKind::Other),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn authed_user(&self, request: &Request) {
|
|
||||||
let client = &request.client;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for User {
|
|
||||||
fn default() -> Self {
|
|
||||||
User::new()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
67
src/util.rs
67
src/util.rs
@ -1,17 +1,5 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use clap::{ArgMatches, Error};
|
|
||||||
|
|
||||||
use crate::request::RequestType;
|
|
||||||
|
|
||||||
pub enum ErrorKind {
|
|
||||||
BadRequest,
|
|
||||||
ForbiddenRequest,
|
|
||||||
NotFound,
|
|
||||||
UnprocessiableRequest,
|
|
||||||
JsonError,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Can't get this function to properly work
|
// TODO: Can't get this function to properly work
|
||||||
// Currently, I'm grabbing input and have the server tell me whether or
|
// Currently, I'm grabbing input and have the server tell me whether or
|
||||||
// not input was validated. What I want to have happen is if there's
|
// not input was validated. What I want to have happen is if there's
|
||||||
@ -33,57 +21,6 @@ pub fn get_input(question: String) -> String {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// When you get a bad response from your Gitea server (any HTTP response that has
|
|
||||||
/// a status code not 2xx), and given the kind of response a user receives, the
|
|
||||||
/// appropriate message should be returned
|
|
||||||
pub fn bad_response_message(message: &String, error_kind: ErrorKind) -> String {
|
|
||||||
let final_message: String;
|
|
||||||
match error_kind {
|
|
||||||
ErrorKind::BadRequest => {
|
|
||||||
final_message = format!(
|
|
||||||
"Client error - please try again!\nError message: {}",
|
|
||||||
message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
ErrorKind::ForbiddenRequest => {
|
|
||||||
final_message = format!(
|
|
||||||
"Client error - unauthorized. Please try again!\nError message: {}",
|
|
||||||
message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
ErrorKind::NotFound => {
|
|
||||||
final_message = format!("Client error - not found!\nError message: {}", message);
|
|
||||||
}
|
|
||||||
ErrorKind::UnprocessiableRequest => {
|
|
||||||
final_message = format!("Client error - the request can't be processed. Please try again!\nError message: {}", message);
|
|
||||||
}
|
|
||||||
ErrorKind::JsonError => {
|
|
||||||
final_message = format!(
|
|
||||||
"Client error - can't parse command!\nError message: {}",
|
|
||||||
message
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String::from(final_message)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Based on a subcommand that is passed, return the appropriate request
|
|
||||||
/// type
|
|
||||||
pub fn get_request_type(args: &ArgMatches) -> Result<RequestType, clap::Error> {
|
|
||||||
let request_type: &str = args.subcommand().0;
|
|
||||||
match request_type {
|
|
||||||
"repo" => Ok(RequestType::Repository),
|
|
||||||
"issue" => Ok(RequestType::Issue),
|
|
||||||
"pr" => Ok(RequestType::PullRequest),
|
|
||||||
"user" => Ok(RequestType::User),
|
|
||||||
_ => Err(clap::Error::with_description(
|
|
||||||
"Unknown or invalid command",
|
|
||||||
clap::ErrorKind::InvalidSubcommand,
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user