|
| 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 | +} |
0 commit comments