Compare commits

..

1 Commits

Author SHA1 Message Date
df302b3050 wip: added json feature
Some checks failed
Rust / build (push) Failing after 8m9s
2024-08-23 16:36:39 -04:00
7 changed files with 77 additions and 158 deletions

1
.envrc
View File

@ -1 +0,0 @@
use flake

View File

@ -1,61 +0,0 @@
{
"nodes": {
"flake-schemas": {
"locked": {
"lastModified": 1721999734,
"narHash": "sha256-G5CxYeJVm4lcEtaO87LKzOsVnWeTcHGKbKxNamNWgOw=",
"rev": "0a5c42297d870156d9c57d8f99e476b738dcd982",
"revCount": 75,
"type": "tarball",
"url": "https://api.flakehub.com/f/pinned/DeterminateSystems/flake-schemas/0.1.5/0190ef2f-61e0-794b-ba14-e82f225e55e6/source.tar.gz"
},
"original": {
"type": "tarball",
"url": "https://flakehub.com/f/DeterminateSystems/flake-schemas/%2A.tar.gz"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1722372011,
"narHash": "sha256-B2xRiC3NEJy/82ugtareBkRqEkPGpMyjaLxaR8LBxNs=",
"rev": "cf05eeada35e122770c5c14add958790fcfcbef5",
"revCount": 633614,
"type": "tarball",
"url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.2405.633614%2Brev-cf05eeada35e122770c5c14add958790fcfcbef5/01910e77-e048-77f4-b2d4-a69cacb697db/source.tar.gz"
},
"original": {
"type": "tarball",
"url": "https://flakehub.com/f/NixOS/nixpkgs/%2A.tar.gz"
}
},
"root": {
"inputs": {
"flake-schemas": "flake-schemas",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1722548948,
"narHash": "sha256-ddR/6wbOzRXijcOAEgeaIlbskky51eJdZhYPwoErs3s=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "0b973d3e54e497e518515c05a6e99ec6c03be2ce",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View File

@ -1,61 +0,0 @@
# This flake was initially generated by fh, the CLI for FlakeHub (version 0.1.10)
{
# A helpful description of your flake
description = "A Gitea command line application";
# Flake inputs
inputs = {
flake-schemas.url = "https://flakehub.com/f/DeterminateSystems/flake-schemas/*.tar.gz";
nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/*.tar.gz";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
# Flake outputs that other flakes can use
outputs = { self, flake-schemas, nixpkgs, rust-overlay }:
let
# Nixpkgs overlays
overlays = [
rust-overlay.overlays.default
(final: prev: {
rustToolchain = final.rust-bin.stable.latest.default.override { extensions = [ "rust-src"]; };
})
];
# Helpers for producing system-specific outputs
supportedSystems = [ "x86_64-linux" "aarch64-darwin" "x86_64-darwin" "aarch64-linux" ];
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
pkgs = import nixpkgs { inherit overlays system; };
});
in {
# Schemas tell Nix about the structure of your flake's outputs
schemas = flake-schemas.schemas;
# Development environments
devShells = forEachSupportedSystem ({ pkgs }: {
default = pkgs.mkShell {
# Pinned packages available in the environment
packages = with pkgs; [
rustToolchain
rust-analyzer
curl
git
jq
wget
nixpkgs-fmt
openssl
];
# Environment variables
env = {
RUST_BACKTRACE = "1";
RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library";
};
};
});
};
}

View File

@ -1,7 +0,0 @@
{
"rust-analyzer": {
"files": {
"excludeDirs": [".direnv"]
}
}
}

View File

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

View File

@ -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) {
}
} }

View File

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