From df302b3050a5346ca574fce8f270d8b0d4afad74 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Fri, 23 Aug 2024 16:36:39 -0400 Subject: [PATCH] wip: added json feature --- src/arg.rs | 1 + src/repo.rs | 101 +++++++++++++++++++++++++++++++++++-------------- src/request.rs | 3 ++ 3 files changed, 77 insertions(+), 28 deletions(-) diff --git a/src/arg.rs b/src/arg.rs index e4fdb0e..96d9c67 100644 --- a/src/arg.rs +++ b/src/arg.rs @@ -7,6 +7,7 @@ pub fn get_args() -> ArgMatches<'static> { .version("0.0.2") .author("Wyatt J. Miller ") .about("It's a Gitea CLI client!") + .arg(Arg::with_name("--json").help("Print out results in JSON")) .subcommand(SubCommand::with_name("repo") .about("Create, delete, list, search, or fork a repository") .arg(Arg::with_name("create") diff --git a/src/repo.rs b/src/repo.rs index 105eea5..2f32101 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -1,11 +1,18 @@ -use std::{collections::HashMap, path::{Path, self}, file}; +use std::{ + collections::HashMap, + file, + path::{self, Path}, +}; use colored::*; -use git2::{Repository as Repo, build::RepoBuilder, Credentials, Cred, CredentialType, RemoteCallbacks, FetchOptions}; +use git2::{ + build::RepoBuilder, Cred, CredentialType, Credentials, FetchOptions, RemoteCallbacks, + Repository as Repo, +}; use reqwest::StatusCode; use serde_derive::{Deserialize, Serialize}; -use crate::{request::Request, config::Configuration}; +use crate::{config::Configuration, request::Request}; pub struct Repository; @@ -117,6 +124,7 @@ impl Repository { request = request.url.as_ref().unwrap(), api_token = request.authentication.credentials.1.as_ref().unwrap() ); + let is_json = request.is_json; map.insert("name", arg_value); map.insert("readme", arg_value); @@ -127,6 +135,9 @@ impl Repository { match response { Ok(repo) => match repo.status() { StatusCode::CREATED => { + if is_json { + println!("{}", repo) + } 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); @@ -145,6 +156,9 @@ impl Repository { } pub fn delete_repo(&self, request: &Request) { + if (request.is_json) { + println!("uh oh json"); // TODO: fix me, replace line with util::bad_response_message + } let client = &request.client; let arg_value: Vec<&str> = request .arg_value @@ -166,7 +180,9 @@ impl Repository { match response { Ok(repo) => match repo.status() { - StatusCode::NO_CONTENT => println!("{}", "Respository successfully deleted!".green()), + 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: {}", @@ -206,15 +222,25 @@ impl Repository { match response { Ok(repo) => match repo.status() { StatusCode::ACCEPTED => { + if (request.is_json) { + println!("{}", repo) + } let deserialized: RepositoryResponse = repo.json().unwrap(); 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::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::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), } @@ -241,6 +267,9 @@ impl Repository { match response { Ok(repo) => match repo.status() { StatusCode::OK => { + if (request.is_json) { + println!("{}", repo) + } let deserialized: MultipleRepositories = repo.json().unwrap(); match deserialized.data.len() != 0 { @@ -251,14 +280,22 @@ 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), } @@ -277,6 +314,9 @@ impl Repository { match response { Ok(repo) => match repo.status() { StatusCode::OK => { + if (request.is_json) { + println!("{}", repo) + } let deserialized: MultipleRepositories = repo.json().unwrap(); match deserialized.data.len() != 0 { @@ -292,8 +332,13 @@ 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), } @@ -302,7 +347,6 @@ 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) { @@ -330,15 +374,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 // ) @@ -349,24 +393,25 @@ 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) {} } diff --git a/src/request.rs b/src/request.rs index d2e57c1..a7227a5 100644 --- a/src/request.rs +++ b/src/request.rs @@ -17,6 +17,7 @@ pub struct Request<'a> { pub map: HashMap, pub url: Option, pub authentication: Authentication, + pub is_json: bool, } pub struct Authentication { @@ -39,6 +40,7 @@ impl<'a> Request<'a> { map: HashMap::new(), url: Some(format!("{}{}", config.base_url, config.base_api)), authentication: auth, + is_json: arg.is_present("--json"), } } @@ -54,6 +56,7 @@ impl<'a> Request<'a> { map: HashMap::new(), url: Some(format!("{}{}", config.base_url, config.base_api)), authentication: auth, + is_json: arg.is_present("--json"), } } }