Compare commits

..

No commits in common. "0.3.0" and "master" have entirely different histories.

10 changed files with 112 additions and 1949 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
/target
.vscode/
config.json
.idea/
Cargo.lock

1459
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "gt"
version = "0.3.0"
version = "0.2.0"
authors = ["Wyatt J. Miller <wjmiller2016@gmail.com>"]
edition = "2018"
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
[dependencies]
base64 = "0.22.1"
clap = "2.33.1"
colored = "2.0.0"
config = "0.11.0"

View File

@ -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"),
}
}
}

View File

@ -2,12 +2,11 @@ use std::collections::HashMap;
use colored::*;
use reqwest::blocking::Client;
use reqwest::{Response, StatusCode};
use serde_derive::{Deserialize, Serialize};
use reqwest::{StatusCode, Response};
use serde_derive::{Serialize, Deserialize};
use serde_json::Value;
use crate::error;
use crate::request::{AuthenticationType, Request};
use crate::request::{Request, Authentication, AuthenticationType};
use crate::util;
pub struct Issue;
@ -45,12 +44,6 @@ pub struct IssueResponse {
pub repository: Repository,
}
#[derive(Deserialize)]
pub struct IssueErrorResponse {
pub message: String,
pub url: String,
}
#[derive(Serialize, Deserialize)]
pub struct User {
pub id: i64,
@ -74,18 +67,30 @@ pub struct Repository {
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 {
pub fn new() -> 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_description: String;
let _issue_assignee: Option<String>;
let _issue_milestone: Option<String>;
let _issue_tags: Option<String>;
let client = &request.client;
let arg_value: Vec<&str> = request
.arg_value
@ -114,60 +119,51 @@ impl Issue {
let response = client.post(&url).json(&map).send();
match response {
Ok(repo) => match repo.status() {
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
StatusCode::UNPROCESSABLE_ENTITY => {
println!("{}", "Issue input validation failed!".red())
Ok(repo) => {
match repo.status() {
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Issue input validation failed!".red()),
_ => println!(),
}
_ => println!(),
},
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 url = self.url_request_decision(&request);
let response = client.get(&url).send();
// TODO: fix this to match context
match response {
Ok(repo) => match repo.status() {
StatusCode::CREATED => {
println!("{}", "Issue successfully created!".green());
Ok(())
Ok(repo) => {
match repo.status() {
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
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),
}
}
pub fn search_issue(&self, request: &Request) -> Result<(), error::ErrorKind> {
pub fn search_issue(&self, request: &Request) {
let client = &request.client;
let url = self.url_request_decision(&request);
let response = client.get(&url).send();
// TODO: fix this to match context
match response {
Ok(repo) => match repo.status() {
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
StatusCode::UNPROCESSABLE_ENTITY => {
println!("{}", "Issue input validation failed!".red())
Ok(repo) => {
match repo.status() {
StatusCode::CREATED => println!("{}", "Issue successfully created!".green()),
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Issue input validation failed!".red()),
_ => println!(),
}
_ => println!(),
},
Err(e) => panic!("{}", e),
}
@ -183,7 +179,7 @@ impl Issue {
.values_of("search")
.unwrap()
.collect();
// TODO: fix the url formatting for basic auth
match &request.authentication.auth_type {
AuthenticationType::ApiToken => {
@ -194,7 +190,7 @@ impl Issue {
repo = arg_value[1],
api_token = request.authentication.credentials.1.as_ref().unwrap()
)
}
},
AuthenticationType::BasicAuth => {
format!(
"{request}/repos/{owner}/{repo}/issues?token={api_token}",
@ -203,9 +199,10 @@ impl Issue {
repo = arg_value[1],
api_token = request.authentication.credentials.1.as_ref().unwrap()
)
}
},
// 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 🤷")
}
}
}

View File

@ -5,7 +5,6 @@
mod arg;
mod config;
mod error;
mod generate;
mod issue;
mod pr;
@ -29,7 +28,7 @@ fn main() {
}
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() {
("", None) => println!("No subcommand was given!"),
@ -77,26 +76,6 @@ fn main() {
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?"),
}
}

View File

@ -1,16 +1,11 @@
use std::{collections::HashMap, path::Path};
use std::{collections::HashMap, path::{Path, self}, file};
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 serde_derive::{Deserialize, Serialize};
use crate::{
config::Configuration,
error,
request::Request,
util::{self, ErrorKind},
};
use crate::{request::Request, config::Configuration};
pub struct Repository;
@ -63,12 +58,6 @@ pub struct RepositoryResponse {
pub website: String,
}
#[derive(Deserialize)]
pub struct RepositoryErrorResponse {
pub message: String,
pub url: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ExternalTracker {
pub external_tracker_format: String,
@ -113,7 +102,7 @@ impl Repository {
Repository {}
}
pub fn create_repo(&self, request: &Request) -> Result<(), error::ErrorKind> {
pub fn create_repo(&self, request: &Request) {
let client = &request.client;
let arg_value = request
.arg_value
@ -138,34 +127,24 @@ impl Repository {
match response {
Ok(repo) => match repo.status() {
StatusCode::CREATED => {
// TODO: implement error handling for the deserialization
let deserialized: RepositoryResponse = repo.json().unwrap();
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);
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 => {
let deserialized: RepositoryErrorResponse = repo.json().unwrap();
Err(error::ErrorKind::UnprocessiableRequest(
deserialized.message,
))
println!("{}", "Repository input validation failed!".red())
}
_ => 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 arg_value: Vec<&str> = request
.arg_value
@ -187,13 +166,8 @@ impl Repository {
match response {
Ok(repo) => match repo.status() {
StatusCode::NO_CONTENT => {
println!("{}", "Respository successfully deleted!".green())
}
StatusCode::FORBIDDEN => println!(
"{}",
"Unable to authorize deletion. Please report this to the webmaster!".red()
),
StatusCode::NO_CONTENT => println!("{}", "Respository successfully deleted!".green()),
StatusCode::FORBIDDEN => println!("{}", "Repository deletion forbidden!".red()),
_ => println!(
"Repository deletion failed! Does the repository exist? HTTP status code: {}",
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 arg_item: Vec<&str> = request
.arg_value
@ -217,7 +191,6 @@ impl Repository {
let mut map: HashMap<&str, &str> = HashMap::new();
let user = request.authentication.credentials.0.as_ref().unwrap();
// TODO: use new url formatter
let url = format!(
"{request}/repos/{owner}/{repo}/forks?token={api_token}",
request = request.url.as_ref().unwrap(),
@ -237,27 +210,17 @@ impl Repository {
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);
}
StatusCode::INTERNAL_SERVER_ERROR => {
println!("{}", "Repository already forked!".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::INTERNAL_SERVER_ERROR => println!("{}", "Repository already forked!".red()),
StatusCode::FORBIDDEN => println!("{}", "Repository fork forbidden!".red()),
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Repository fork input validation failed!".red()),
StatusCode::NOT_FOUND => println!("{}", "Repository not found!"),
_ => println!(
"Repository creation failed! HTTP status code: {}",
repo.status().as_str()
),
_ => println!("Repository creation failed! HTTP status code: {}", repo.status().as_str()),
},
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 arg_value = request
.arg_value
@ -266,7 +229,6 @@ impl Repository {
.unwrap()
.value_of("search")
.unwrap();
// TODO: use new url formatter
let url = format!(
"{request}/repos/search?q={query}&token={api_token}",
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!(
"Total number of repositories indexed: {}",
deserialized.data.iter().count()
)
println!("Total number of repositories indexed: {}", deserialized.data.iter().count())
}
false => println!("{}", "Repository searched doesn't exist!".red()),
}
}
StatusCode::NOT_FOUND => println!("{}", "Repository searched doesn't exist!".red()),
StatusCode::UNPROCESSABLE_ENTITY => {
println!("{}", "Repository input validation failed!".red())
}
_ => println!(
"Repository search failed! HTTP status code: {}",
repo.status().as_str()
),
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Repository input validation failed!".red()),
_ => println!("Repository search failed! HTTP status code: {}", repo.status().as_str()),
},
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 url = format!(
"{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())
}
}
StatusCode::UNPROCESSABLE_ENTITY => {
println!("{}", "Repository input validation failed!".red())
}
_ => println!(
"Repository search failed! HTTP status code: {}",
repo.status().as_str()
),
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Repository input validation failed!".red()),
_ => println!("Repository search failed! HTTP status code: {}",repo.status().as_str()),
},
Err(e) => panic!("{}", e),
}
@ -353,6 +302,7 @@ impl Repository {
pub fn push_to_remote(&self, request: &Request, config: &Configuration) {
// code to push to the remote server goes here
// this is based on the user, the remote, and the branch
}
pub fn pull_from_remote(&self, request: &Request, config: &Configuration) {
@ -380,15 +330,15 @@ impl Repository {
let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(|_url, _user_from_url, _allowed_types| {
let user = _user_from_url.unwrap_or("git");
if _allowed_types.contains(CredentialType::USERNAME) {
return Cred::username(user);
if _allowed_types.contains(CredentialType::USERNAME) {
return Cred::username(user)
}
// if request.url_request() {
// Cred::ssh_key(
// user,
// Some(Path::new(&format!("{}/.ssh/id_ed25519.pub", std::env::var("HOME").unwrap()).as_str())),
// user,
// 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()),
// None
// )
@ -399,31 +349,24 @@ impl Repository {
// )
// }
Cred::userpass_plaintext(
request
.authentication
.credentials
.0
.as_ref()
.unwrap()
.as_str(),
config.password.as_ref().unwrap().as_str(),
request.authentication.credentials.0.as_ref().unwrap().as_str(),
config.password.as_ref().unwrap().as_str()
)
});
builder.branch("master");
match builder.clone(url.as_str(), Path::new(arg_value[1])) {
Ok(_r) => Ok(println!("Repository cloned successfully!")),
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()
}
}

View File

@ -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 reqwest::blocking::Client;
use crate::config::Configuration;
use crate::util;
pub enum RequestType {
Repository,
Issue,
PullRequest,
User,
}
pub enum AuthenticationType {
BasicAuth,
@ -20,14 +11,12 @@ pub enum AuthenticationType {
None,
}
#[derive()]
pub struct Request<'a> {
pub client: Client,
pub arg_value: &'a ArgMatches<'a>,
pub arg_value: ArgMatches<'a>,
pub map: HashMap<String, String>,
pub url: Option<String>,
pub authentication: Authentication,
pub request_type: RequestType,
}
pub struct Authentication {
@ -41,49 +30,32 @@ impl<'a> Request<'a> {
/// Public constructor for a request with a simple username and password
pub fn with_basic_request(
config: Configuration,
arg: &'a ArgMatches,
arg: ArgMatches,
auth: Authentication,
) -> Request<'a> {
) -> Request {
Request {
client: Client::new(),
arg_value: &arg,
arg_value: arg,
map: HashMap::new(),
url: Some(format!("{}{}", config.base_url, config.base_api)),
authentication: auth,
request_type: util::get_request_type(&arg).unwrap(),
}
}
/// Public constructor for a request with an API token
pub fn with_api_request(
config: Configuration,
arg: &'a ArgMatches,
arg: ArgMatches,
auth: Authentication,
) -> Request<'a> {
) -> Request {
Request {
client: Client::new(),
arg_value: &arg,
arg_value: arg,
map: HashMap::new(),
url: Some(format!("{}{}", config.base_url, config.base_api)),
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 {
@ -142,22 +114,14 @@ impl Authentication {
/// 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
/// structure for more details.
pub fn request_chooser<'a>(
self,
config: Configuration,
arg: &'a ArgMatches<'static>,
) -> Request<'a> {
pub fn request_chooser(self, config: Configuration, arg: ArgMatches<'static>) -> Request {
if let true = self.api_token {
Request::with_api_request(config, &arg, self)
Request::with_api_request(config, arg.to_owned(), self)
} else {
match self.basic {
true => Request::with_basic_request(config, &arg, self),
true => Request::with_basic_request(config, arg.to_owned(), self),
false => panic!(),
}
}
}
}
impl Default for Authentication {
fn default() -> Self {}
}

View File

@ -1,18 +1,11 @@
use std::collections::HashMap;
use crate::{
error,
request::Request,
util::{self, ErrorKind},
};
use colored::*;
use reqwest::StatusCode;
use crate::{request::Request, util};
use serde_derive::{Deserialize, Serialize};
pub struct User;
#[derive(Deserialize)]
pub struct MultipleUsers {
pub struct MutlipleUsers {
pub data: Vec<UserResponse>,
pub ok: bool,
}
@ -36,180 +29,33 @@ pub struct UserResponse {
pub restricted: bool,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserErrorResponse {
pub message: String,
pub url: String,
}
impl User {
pub fn new() -> User {
User {}
}
pub fn create_user(&self, request: &Request) -> Result<(), error::ErrorKind> {
pub fn create_user(request: &Request) {
let client = &request.client;
let mut user_create_input: HashMap<String, String> = HashMap::new();
let url = format!(
"{request}/admin/users?token={api_token}",
request = request.url.as_ref().unwrap(),
api_token = request.authentication.credentials.1.as_ref().unwrap()
);
let arg_value = &request
.arg_value
.subcommand()
.1
.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 email = util::get_input(String::from("Please enter a email address:"));
let password = util::get_input(String::from("Please enter a password:"));
user_create_input.insert("login".to_string(), username);
user_create_input.insert("email".to_string(), email);
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> {
let client = &request.client;
pub fn list_user() {}
let url = format!(
"{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();
pub fn search_user() {}
match deserialized.data.len() != 0 {
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()
}
pub fn delete_user() {}
}

View File

@ -1,17 +1,5 @@
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
// 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
@ -33,57 +21,6 @@ pub fn get_input(question: String) -> String {
break;
}
}
}
}
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,
)),
}
}
}