Skip to content

Commit 8934513

Browse files
committed
implemented user command & fixed json writing in releases command
1 parent 6258125 commit 8934513

4 files changed

Lines changed: 98 additions & 5 deletions

File tree

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod all;
22
pub mod releases;
3+
pub mod user;

src/commands/releases.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ pub fn releases_command(
174174
fn write_json(json_string: String, output: Option<PathBuf>) {
175175
match output {
176176
Some(path) => {
177-
let result = write_to_file(serde_json::to_string_pretty(&json_string).unwrap(), path);
177+
let result = write_to_file(json_string, path);
178178
match result {
179179
Ok(_) => {}
180180
Err(err) => println!("{}", err),

src/commands/user.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use crate::utils::{pretty_dates, request, write_to_file};
2+
use std::path::PathBuf;
3+
4+
pub fn user_command(user: String, output: Option<PathBuf>, display: bool) {
5+
let url = format!("https://api.github.com/users/{}", user);
6+
7+
let json = request(url).expect("Failed to request data");
8+
9+
if &json["message"] == "Not Found" {
10+
println!("User not found.");
11+
std::process::exit(0)
12+
}
13+
14+
if display {
15+
// About
16+
let name = json["name"].as_str().unwrap_or("");
17+
let email = json["email"].as_str().unwrap_or("None");
18+
let bio = json["bio"].as_str().unwrap_or("None");
19+
let account_type = json["type"].as_str().unwrap_or("None");
20+
let url = json["html_url"].as_str().unwrap_or("None");
21+
let blog = json["blog"].as_str().unwrap_or("None");
22+
let company = json["company"].as_str().unwrap_or("None");
23+
let location = json["location"].as_str().unwrap_or("None");
24+
// User Stats
25+
let public_repos = json["public_repos"].as_i64().unwrap_or(0);
26+
let public_gists = json["public_gists"].as_i64().unwrap_or(0);
27+
let followers = json["followers"].as_i64().unwrap_or(0);
28+
let following = json["following"].as_i64().unwrap_or(0);
29+
// Important Dates
30+
let created_at = json["created_at"].as_str().unwrap_or("None");
31+
let updated_at = json["updated_at"].as_str().unwrap_or("None");
32+
33+
println!(
34+
"ABOUT: {} {}",
35+
user,
36+
if name == "" {
37+
"".to_string()
38+
} else {
39+
format!("({})", name)
40+
}
41+
);
42+
if email != "" && email != "None" {
43+
println!(" {:<14}: {}", "Email", email);
44+
}
45+
println!(" {:<14}: {}", "Bio", bio);
46+
println!(" {:<14}: {}", "Account type", account_type);
47+
println!(" {:<14}: {}", "URL", url);
48+
if blog != "" && blog != "None" {
49+
println!(" {:<14}: {}", "Blog", blog);
50+
}
51+
if company != "null" && company != "None" {
52+
println!(" {:<14}: {}", "Company", company);
53+
}
54+
if location != "null" && location != "None" {
55+
println!(" {:<14}: {}", "Location", location);
56+
}
57+
println!();
58+
println!("USER STATS");
59+
println!(" {:<14}: {}", "Public repos", public_repos);
60+
println!(" {:<14}: {}", "Public gists", public_gists);
61+
println!(" {:<14}: {}", "Followers", followers);
62+
println!(" {:<14}: {}", "Following", following);
63+
println!();
64+
println!("IMPORTANT DATES");
65+
println!(" {:<14}: {}", "Created at", pretty_dates(created_at));
66+
println!(" {:<14}: {}", "Updated at", pretty_dates(updated_at));
67+
println!();
68+
} else {
69+
println!("{}", serde_json::to_string_pretty(&json).unwrap());
70+
}
71+
72+
match output {
73+
Some(path) => {
74+
let result = write_to_file(serde_json::to_string_pretty(&json).unwrap(), path);
75+
match result {
76+
Ok(_) => {}
77+
Err(err) => println!("{}", err),
78+
}
79+
}
80+
None => {}
81+
}
82+
}

src/main.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod utils;
55
use crate::cli::{Arg, Cli, Command};
66
use crate::commands::all::all_command;
77
use crate::commands::releases::releases_command;
8+
use crate::commands::user::user_command;
89
use crate::utils::validate_and_convert_path;
910

1011
fn main() {
@@ -128,17 +129,17 @@ fn main() {
128129
match command.name {
129130
"version" => cli.version(),
130131
"all" => {
131-
let owner = command.get_value_of("owner").throw_if_none();
132+
let user = command.get_value_of("user").throw_if_none();
132133
let repo = command.get_value_of("repository").throw_if_none();
133134
let output = command.get_value_of("output").to_option();
134135
let display = command.has("display");
135136

136137
let output = output_to_path(output);
137138

138-
all_command(owner, repo, output, display);
139+
all_command(user, repo, output, display);
139140
}
140141
"releases" => {
141-
let owner = command.get_value_of("owner").throw_if_none();
142+
let user = command.get_value_of("user").throw_if_none();
142143
let repo = command.get_value_of("repository").throw_if_none();
143144
let individual = command.has("individual");
144145
let link = command.has("link");
@@ -148,7 +149,16 @@ fn main() {
148149

149150
let output = output_to_path(output);
150151

151-
releases_command(owner, repo, individual, link, output, all, display);
152+
releases_command(user, repo, individual, link, output, all, display);
153+
}
154+
"user" => {
155+
let user = command.get_value_of("user").throw_if_none();
156+
let output = command.get_value_of("output").to_option();
157+
let display = command.has("display");
158+
159+
let output = output_to_path(output);
160+
161+
user_command(user, output, display);
152162
}
153163
"help" => cli.help(),
154164
_ => cli.help(),

0 commit comments

Comments
 (0)