wip: added json feature
This commit is contained in:
		@@ -7,6 +7,7 @@ pub fn get_args() -> ArgMatches<'static> {
 | 
				
			|||||||
                        .version("0.0.2")
 | 
					                        .version("0.0.2")
 | 
				
			||||||
                        .author("Wyatt J. Miller <wjmiller2016@gmail.com>")
 | 
					                        .author("Wyatt J. Miller <wjmiller2016@gmail.com>")
 | 
				
			||||||
                        .about("It's a Gitea CLI client!")
 | 
					                        .about("It's a Gitea CLI client!")
 | 
				
			||||||
 | 
					                        .arg(Arg::with_name("--json").help("Print out results in JSON"))
 | 
				
			||||||
                        .subcommand(SubCommand::with_name("repo")
 | 
					                        .subcommand(SubCommand::with_name("repo")
 | 
				
			||||||
                                .about("Create, delete, list, search, or fork a repository")
 | 
					                                .about("Create, delete, list, search, or fork a repository")
 | 
				
			||||||
                                .arg(Arg::with_name("create")
 | 
					                                .arg(Arg::with_name("create")
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										93
									
								
								src/repo.rs
									
									
									
									
									
								
							
							
						
						
									
										93
									
								
								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 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 reqwest::StatusCode;
 | 
				
			||||||
use serde_derive::{Deserialize, Serialize};
 | 
					use serde_derive::{Deserialize, Serialize};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::{request::Request, config::Configuration};
 | 
					use crate::{config::Configuration, request::Request};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Repository;
 | 
					pub struct Repository;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -117,6 +124,7 @@ impl Repository {
 | 
				
			|||||||
            request = request.url.as_ref().unwrap(),
 | 
					            request = request.url.as_ref().unwrap(),
 | 
				
			||||||
            api_token = request.authentication.credentials.1.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("name", arg_value);
 | 
				
			||||||
        map.insert("readme", arg_value);
 | 
					        map.insert("readme", arg_value);
 | 
				
			||||||
@@ -127,6 +135,9 @@ impl Repository {
 | 
				
			|||||||
        match response {
 | 
					        match response {
 | 
				
			||||||
            Ok(repo) => match repo.status() {
 | 
					            Ok(repo) => match repo.status() {
 | 
				
			||||||
                StatusCode::CREATED => {
 | 
					                StatusCode::CREATED => {
 | 
				
			||||||
 | 
					                    if is_json {
 | 
				
			||||||
 | 
					                        println!("{}", repo)
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    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);
 | 
				
			||||||
@@ -145,6 +156,9 @@ impl Repository {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn delete_repo(&self, request: &Request) {
 | 
					    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 client = &request.client;
 | 
				
			||||||
        let arg_value: Vec<&str> = request
 | 
					        let arg_value: Vec<&str> = request
 | 
				
			||||||
            .arg_value
 | 
					            .arg_value
 | 
				
			||||||
@@ -166,7 +180,9 @@ impl Repository {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        match response {
 | 
					        match response {
 | 
				
			||||||
            Ok(repo) => match repo.status() {
 | 
					            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()),
 | 
					                StatusCode::FORBIDDEN => println!("{}", "Repository deletion forbidden!".red()),
 | 
				
			||||||
                _ => println!(
 | 
					                _ => println!(
 | 
				
			||||||
                    "Repository deletion failed! Does the repository exist? HTTP status code: {}",
 | 
					                    "Repository deletion failed! Does the repository exist? HTTP status code: {}",
 | 
				
			||||||
@@ -206,15 +222,25 @@ impl Repository {
 | 
				
			|||||||
        match response {
 | 
					        match response {
 | 
				
			||||||
            Ok(repo) => match repo.status() {
 | 
					            Ok(repo) => match repo.status() {
 | 
				
			||||||
                StatusCode::ACCEPTED => {
 | 
					                StatusCode::ACCEPTED => {
 | 
				
			||||||
 | 
					                    if (request.is_json) {
 | 
				
			||||||
 | 
					                        println!("{}", repo)
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    let deserialized: RepositoryResponse = repo.json().unwrap();
 | 
					                    let deserialized: RepositoryResponse = repo.json().unwrap();
 | 
				
			||||||
                    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 => println!("{}", "Repository already forked!".red()),
 | 
					                StatusCode::INTERNAL_SERVER_ERROR => {
 | 
				
			||||||
 | 
					                    println!("{}", "Repository already forked!".red())
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                StatusCode::FORBIDDEN => println!("{}", "Repository fork forbidden!".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!"),
 | 
					                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),
 | 
					            Err(e) => panic!("{}", e),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -241,6 +267,9 @@ impl Repository {
 | 
				
			|||||||
        match response {
 | 
					        match response {
 | 
				
			||||||
            Ok(repo) => match repo.status() {
 | 
					            Ok(repo) => match repo.status() {
 | 
				
			||||||
                StatusCode::OK => {
 | 
					                StatusCode::OK => {
 | 
				
			||||||
 | 
					                    if (request.is_json) {
 | 
				
			||||||
 | 
					                        println!("{}", repo)
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    let deserialized: MultipleRepositories = repo.json().unwrap();
 | 
					                    let deserialized: MultipleRepositories = repo.json().unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    match deserialized.data.len() != 0 {
 | 
					                    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!("{}.\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()),
 | 
					                        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 => println!("{}", "Repository input validation failed!".red()),
 | 
					                StatusCode::UNPROCESSABLE_ENTITY => {
 | 
				
			||||||
                _ => println!("Repository search failed! HTTP status code: {}", repo.status().as_str()),
 | 
					                    println!("{}", "Repository input validation failed!".red())
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                _ => println!(
 | 
				
			||||||
 | 
					                    "Repository search failed! HTTP status code: {}",
 | 
				
			||||||
 | 
					                    repo.status().as_str()
 | 
				
			||||||
 | 
					                ),
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            Err(e) => panic!("{}", e),
 | 
					            Err(e) => panic!("{}", e),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -277,6 +314,9 @@ impl Repository {
 | 
				
			|||||||
        match response {
 | 
					        match response {
 | 
				
			||||||
            Ok(repo) => match repo.status() {
 | 
					            Ok(repo) => match repo.status() {
 | 
				
			||||||
                StatusCode::OK => {
 | 
					                StatusCode::OK => {
 | 
				
			||||||
 | 
					                    if (request.is_json) {
 | 
				
			||||||
 | 
					                        println!("{}", repo)
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    let deserialized: MultipleRepositories = repo.json().unwrap();
 | 
					                    let deserialized: MultipleRepositories = repo.json().unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    match deserialized.data.len() != 0 {
 | 
					                    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())
 | 
					                                false => println!("{}", "The authenticated user doesn't have any repositories. Why not create one?".yellow())
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Repository input validation failed!".red()),
 | 
					                StatusCode::UNPROCESSABLE_ENTITY => {
 | 
				
			||||||
                _ => println!("Repository search failed! HTTP status code: {}",repo.status().as_str()),
 | 
					                    println!("{}", "Repository input validation failed!".red())
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                _ => println!(
 | 
				
			||||||
 | 
					                    "Repository search failed! HTTP status code: {}",
 | 
				
			||||||
 | 
					                    repo.status().as_str()
 | 
				
			||||||
 | 
					                ),
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            Err(e) => panic!("{}", e),
 | 
					            Err(e) => panic!("{}", e),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -302,7 +347,6 @@ 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) {
 | 
				
			||||||
@@ -332,7 +376,7 @@ impl Repository {
 | 
				
			|||||||
            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() {
 | 
				
			||||||
@@ -349,24 +393,25 @@ impl Repository {
 | 
				
			|||||||
            //     )
 | 
					            //     )
 | 
				
			||||||
            // }
 | 
					            // }
 | 
				
			||||||
            Cred::userpass_plaintext(
 | 
					            Cred::userpass_plaintext(
 | 
				
			||||||
                request.authentication.credentials.0.as_ref().unwrap().as_str(),
 | 
					                request
 | 
				
			||||||
                config.password.as_ref().unwrap().as_str()
 | 
					                    .authentication
 | 
				
			||||||
 | 
					                    .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) {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,6 +17,7 @@ pub struct Request<'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 is_json: bool,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Authentication {
 | 
					pub struct Authentication {
 | 
				
			||||||
@@ -39,6 +40,7 @@ impl<'a> Request<'a> {
 | 
				
			|||||||
            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,
 | 
				
			||||||
 | 
					            is_json: arg.is_present("--json"),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -54,6 +56,7 @@ impl<'a> Request<'a> {
 | 
				
			|||||||
            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,
 | 
				
			||||||
 | 
					            is_json: arg.is_present("--json"),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user