Skip to content

Commit 3b6599f

Browse files
committed
added followers command
1 parent 76a7715 commit 3b6599f

3 files changed

Lines changed: 132 additions & 20 deletions

File tree

src/commands/followers.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use crate::utils::{request, write_to_file};
2+
use std::path::PathBuf;
3+
4+
pub fn followers_command(user: String, total: bool, output: Option<PathBuf>, display: bool) {
5+
let url = format!("https://api.github.com/users/{}/followers", 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+
for follower in json.as_array().unwrap() {
16+
let username = follower["login"].as_str().unwrap_or("None");
17+
let html_url = follower["html_url"].as_str().unwrap_or("None");
18+
println!("{:<20} - {}", username, html_url);
19+
println!();
20+
}
21+
if total {
22+
println!("Followers: {}", json.as_array().unwrap().len())
23+
}
24+
} else {
25+
let mut total_json = serde_json::Map::new();
26+
total_json.insert("followers".to_string(), json.clone());
27+
if total {
28+
total_json.insert(
29+
"total".to_string(),
30+
serde_json::Value::Number(serde_json::Number::from(json.as_array().unwrap().len())),
31+
);
32+
}
33+
34+
println!("{}", serde_json::to_string_pretty(&total_json).unwrap())
35+
}
36+
37+
if total {
38+
let mut total_json = serde_json::Map::new();
39+
total_json.insert(
40+
"total".to_string(),
41+
serde_json::Value::Number(serde_json::Number::from(json.as_array().unwrap().len())),
42+
);
43+
total_json.insert("followers".to_string(), json);
44+
45+
match output {
46+
Some(path) => {
47+
let result =
48+
write_to_file(serde_json::to_string_pretty(&total_json).unwrap(), path);
49+
match result {
50+
Ok(_) => {}
51+
Err(err) => println!("{}", err),
52+
}
53+
}
54+
None => {}
55+
}
56+
} else {
57+
match output {
58+
Some(path) => {
59+
let result = write_to_file(serde_json::to_string_pretty(&json).unwrap(), path);
60+
match result {
61+
Ok(_) => {}
62+
Err(err) => println!("{}", err),
63+
}
64+
}
65+
None => {}
66+
}
67+
}
68+
}

src/commands/mod.rs

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

src/main.rs

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,124 +4,157 @@ pub mod utils;
44

55
use crate::cli::{Arg, Cli, Command};
66
use crate::commands::all::all_command;
7+
use crate::commands::followers::followers_command;
78
use crate::commands::releases::releases_command;
89
use crate::commands::user::user_command;
910
use crate::utils::{install, validate_and_convert_path, OS};
1011

1112
fn main() {
1213
let cli = Cli::new().with_default_command("help").with_commands(vec![
13-
Command::new("version", "Displays the current version of github-stats.").with_short('v'),
14-
Command::new("install", "Installs the files and directories."),
15-
Command::new("all", "Gives all stats found on a repository as json.")
14+
Command::new("version", "Displays the current version of github-stats").with_short('v'),
15+
Command::new("install", "Installs the files and directories"),
16+
Command::new("all", "Gives all stats found on a repository as json")
1617
.with_arg(
1718
Arg::new()
1819
.with_name("user")
1920
.with_short('u')
2021
.with_long("user")
2122
.with_value_name("USER")
22-
.with_help("The user who owns the repository."),
23+
.with_help("The user who owns the repository"),
2324
)
2425
.with_arg(
2526
Arg::new()
2627
.with_name("repository")
2728
.with_short('r')
2829
.with_long("repository")
2930
.with_value_name("REPOSITORY")
30-
.with_help("Name of the repository."),
31+
.with_help("Name of the repository"),
3132
)
3233
.with_arg(
3334
Arg::new()
3435
.with_name("output")
3536
.with_short('o')
3637
.with_long("output")
3738
.with_value_name("OUTPUT")
38-
.with_help("File path to save the json."),
39+
.with_help("File path to save the json"),
3940
)
4041
.with_arg(
4142
Arg::new()
4243
.with_name("display")
4344
.with_short('d')
4445
.with_long("display")
45-
.with_help("Converts the json to an easier format (will remove some data)."),
46+
.with_help("Converts the json to an easier format (will remove some data)"),
4647
),
47-
Command::new("releases", "Gives information on github releases.")
48+
Command::new("releases", "Gives information on github releases")
4849
.with_arg(
4950
Arg::new()
5051
.with_name("user")
5152
.with_short('u')
5253
.with_long("user")
5354
.with_value_name("USER")
54-
.with_help("The user who owns the repository."),
55+
.with_help("The user who owns the repository"),
5556
)
5657
.with_arg(
5758
Arg::new()
5859
.with_name("repository")
5960
.with_short('r')
6061
.with_long("repository")
6162
.with_value_name("REPOSITORY")
62-
.with_help("Name of the repository."),
63+
.with_help("Name of the repository"),
6364
)
6465
.with_arg(
6566
Arg::new()
6667
.with_name("individual")
6768
.with_short('i')
6869
.with_long("individual")
69-
.with_help("Downloads per release."),
70+
.with_help("Downloads per release"),
7071
)
7172
.with_arg(
7273
Arg::new()
7374
.with_name("link")
7475
.with_short('l')
7576
.with_long("link")
76-
.with_help("Download links for releases (if not individual then for latest)."),
77+
.with_help("Download links for releases (if not individual then for latest)"),
7778
)
7879
.with_arg(
7980
Arg::new()
8081
.with_name("output")
8182
.with_short('o')
8283
.with_long("output")
8384
.with_value_name("OUTPUT")
84-
.with_help("File path to save the json."),
85+
.with_help("File path to save the json"),
8586
)
8687
.with_arg(
8788
Arg::new()
8889
.with_name("all")
8990
.with_short('a')
9091
.with_long("all")
91-
.with_help("All json from request."),
92+
.with_help("All json from request"),
9293
)
9394
.with_arg(
9495
Arg::new()
9596
.with_name("display")
9697
.with_short('d')
9798
.with_long("display")
98-
.with_help("Converts the json to an easier format (will remove some data)."),
99+
.with_help("Converts the json to an easier format (will remove some data)"),
99100
),
100-
Command::new("user", "Gives information about a github user.")
101+
Command::new("user", "Gives information about a github user")
101102
.with_arg(
102103
Arg::new()
103104
.with_name("user")
104105
.with_short('u')
105106
.with_long("user")
106107
.with_value_name("USER")
107-
.with_help("The user you want information on."),
108+
.with_help("The user you want information on"),
108109
)
109110
.with_arg(
110111
Arg::new()
111112
.with_name("output")
112113
.with_short('o')
113114
.with_long("output")
114115
.with_value_name("OUTPUT")
115-
.with_help("File path to save the json."),
116+
.with_help("File path to save the json"),
116117
)
117118
.with_arg(
118119
Arg::new()
119120
.with_name("display")
120121
.with_short('d')
121122
.with_long("display")
122-
.with_help("Converts the json to an easier format (will remove some data)."),
123+
.with_help("Converts the json to an easier format (will remove some data)"),
123124
),
124-
Command::new("help", "Helps you with the commands.").with_short('h'),
125+
Command::new("followers", "Lists the followers of a github user")
126+
.with_arg(
127+
Arg::new()
128+
.with_name("user")
129+
.with_short('u')
130+
.with_long("user")
131+
.with_value_name("USER")
132+
.with_help("The user you want information on"),
133+
)
134+
.with_arg(
135+
Arg::new()
136+
.with_name("total")
137+
.with_short('t')
138+
.with_long("total")
139+
.with_value_name("TOTAL")
140+
.with_help("Only gives the follower count"),
141+
)
142+
.with_arg(
143+
Arg::new()
144+
.with_name("output")
145+
.with_short('o')
146+
.with_long("output")
147+
.with_value_name("OUTPUT")
148+
.with_help("File path to save the json"),
149+
)
150+
.with_arg(
151+
Arg::new()
152+
.with_name("display")
153+
.with_short('d')
154+
.with_long("display")
155+
.with_help("Converts the json to an easier format (will remove some data)"),
156+
),
157+
Command::new("help", "Helps you with the commands").with_short('h'),
125158
]);
126159

127160
let command = cli.match_commands();
@@ -169,6 +202,16 @@ fn main() {
169202

170203
user_command(user, output, display);
171204
}
205+
"followers" => {
206+
let user = command.get_value_of("user").throw_if_none();
207+
let total = command.has("total");
208+
let output = command.get_value_of("output").to_option();
209+
let display = command.has("display");
210+
211+
let output = output_to_path(output);
212+
213+
followers_command(user, total, output, display);
214+
}
172215
"help" => cli.help(),
173216
_ => cli.help(),
174217
}

0 commit comments

Comments
 (0)