feat: generate config, load shell env vars

This commit is contained in:
2023-01-29 19:19:50 -05:00
parent 58b4b00311
commit 7c5c0c13fb
8 changed files with 275 additions and 56 deletions
+89 -2
View File
@@ -1,11 +1,12 @@
use std::collections::HashMap;
use colored::*;
use reqwest::StatusCode;
use reqwest::blocking::Client;
use reqwest::{StatusCode, Response};
use serde_derive::{Serialize, Deserialize};
use serde_json::Value;
use crate::request::Request;
use crate::request::{Request, Authentication, AuthenticationType};
use crate::util;
pub struct Issue;
@@ -66,6 +67,17 @@ 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 {
@@ -118,4 +130,79 @@ impl Issue {
Err(e) => panic!("{}", e),
}
}
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()),
StatusCode::FORBIDDEN => println!("{}", "Issue creation forbidden!".red()),
StatusCode::UNPROCESSABLE_ENTITY => println!("{}", "Issue input validation failed!".red()),
_ => println!(),
}
},
Err(e) => panic!("{}", e),
}
}
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()),
_ => println!(),
}
},
Err(e) => panic!("{}", e),
}
}
fn url_request_decision(&self, request: &Request) -> String {
let arg_value: Vec<&str> = request
.arg_value
.subcommand()
.1
.unwrap()
// TODO: fix the command line variable listed below
.values_of("search")
.unwrap()
.collect();
// TODO: fix the url formatting for basic auth
match &request.authentication.auth_type {
AuthenticationType::ApiToken => {
format!(
"{request}/repos/{owner}/{repo}/issues?token={api_token}",
request = request.url.as_ref().unwrap(),
owner = arg_value[0],
repo = arg_value[1],
api_token = request.authentication.credentials.1.as_ref().unwrap()
)
},
AuthenticationType::BasicAuth => {
format!(
"{request}/repos/{owner}/{repo}/issues?token={api_token}",
request = request.url.as_ref().unwrap(),
owner = arg_value[0],
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 🤷")
}
}
}