Skip to content

Commit 4d559f8

Browse files
committed
renamed download command to release & finished all display modes for release
1 parent 6161526 commit 4d559f8

4 files changed

Lines changed: 36 additions & 69 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exclude = [".gitignore", ".github/*"]
1717
[dependencies]
1818
reqwest = { version = "0.12", features = ["json", "blocking"] }
1919
serde_json = "1.0"
20-
serde = "1.0"
20+
serde = { version = "1.0", features = ["derive"] }
2121

2222
[[bin]]
2323
name = "gstats"

src/commands/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pub mod all;
2-
pub mod downloads;
2+
pub mod releases;
Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::utils::{bytes_to_best_size, pretty_dates, request, write_json_to_file};
2-
use serde::{Deserialize, Deserializer, Serialize};
2+
use serde::{Deserialize, Serialize};
33
use serde_json::Value;
4-
use std::{path::PathBuf, vec};
4+
use std::path::PathBuf;
55

6-
pub struct DownloadData {
6+
#[derive(Serialize, Deserialize, Debug, Clone)]
7+
pub struct ReleaseData {
78
pub name: String,
89
pub tag: String,
910
pub published_at: String,
@@ -14,16 +15,16 @@ pub struct DownloadData {
1415
pub downloads: i64,
1516
}
1617

17-
impl DownloadData {
18+
impl ReleaseData {
1819
pub fn new(
1920
name: String,
2021
tag: String,
2122
published_at: String,
2223
created_at: String,
2324
html_url: String,
2425
body: String,
25-
) -> DownloadData {
26-
DownloadData {
26+
) -> ReleaseData {
27+
ReleaseData {
2728
name,
2829
tag,
2930
published_at,
@@ -52,6 +53,7 @@ impl DownloadData {
5253
}
5354
}
5455

56+
#[derive(Serialize, Deserialize, Debug, Clone)]
5557
pub struct Asset {
5658
pub download_url: String,
5759
pub created_at: String,
@@ -94,7 +96,7 @@ impl Asset {
9496
}
9597
}
9698

97-
pub fn downloads_command(
99+
pub fn releases_command(
98100
owner: String,
99101
repo: String,
100102
individual: bool,
@@ -120,17 +122,31 @@ pub fn downloads_command(
120122
let simple_data = simplify_json_release_data(&json);
121123

122124
if all && !display {
123-
println!("{:#?}", json);
125+
println!("{}", serde_json::to_string_pretty(&json).unwrap());
124126
} else if all && display {
125127
let mut download_count = 0;
126-
for release in simple_data {
128+
for release in &simple_data {
127129
release.display();
128130
download_count += release.downloads;
129131
}
130132
println!("Total Downloads: {}", download_count);
133+
if link {
134+
println!("Latest Release: {}", simple_data[0].html_url)
135+
}
131136
} else if !all && !display {
132-
// if individual, get latest download url, and sum all item count
133-
// {download_url: Vec<(String name, String link for each asset of latest)>, html_url: String, download_count: i32}
137+
if individual {
138+
println!("{}", serde_json::to_string_pretty(&simple_data).unwrap());
139+
} else {
140+
let mut overview = simple_data[0].clone();
141+
let mut download_count = 0;
142+
for release in &simple_data {
143+
download_count += release.downloads;
144+
}
145+
146+
overview.downloads = download_count;
147+
148+
println!("{}", serde_json::to_string_pretty(&overview).unwrap());
149+
}
134150
} else if !all && display {
135151
let mut download_count = 0;
136152
for release in &simple_data {
@@ -161,8 +177,8 @@ pub fn downloads_command(
161177
}
162178
}
163179

164-
fn simplify_json_release_data(json: &Value) -> Vec<DownloadData> {
165-
let mut download_data: Vec<DownloadData> = Vec::new();
180+
fn simplify_json_release_data(json: &Value) -> Vec<ReleaseData> {
181+
let mut download_data: Vec<ReleaseData> = Vec::new();
166182

167183
for release in json.as_array().unwrap() {
168184
let name = &release["name"].as_str().unwrap_or("None").to_string();
@@ -175,7 +191,7 @@ fn simplify_json_release_data(json: &Value) -> Vec<DownloadData> {
175191
let html_url = &release["html_url"].as_str().unwrap_or("None").to_string();
176192
let body = &release["body"].as_str().unwrap_or("None").to_string();
177193

178-
let mut download = DownloadData::new(
194+
let mut download = ReleaseData::new(
179195
name.to_string(),
180196
tag.to_string(),
181197
pretty_dates(&published_at),

src/main.rs

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod utils;
44

55
use crate::cli::{Arg, Cli, Command};
66
use crate::commands::all::all_command;
7-
use crate::commands::downloads::downloads_command;
7+
use crate::commands::releases::releases_command;
88
use crate::utils::validate_and_convert_path;
99

1010
fn main() {
@@ -44,7 +44,7 @@ fn main() {
4444
.with_long("display")
4545
.with_help("Converts the json to an easier format (will remove some data)."),
4646
),
47-
Command::new("downloads", "Gives download count of releases as json")
47+
Command::new("releases", "Gives information on github releases")
4848
.with_arg(
4949
Arg::new()
5050
.with_name("owner")
@@ -97,48 +97,6 @@ fn main() {
9797
.with_long("display")
9898
.with_help("Converts the json to an easier format (will remove some data)."),
9999
),
100-
Command::new(
101-
"releases",
102-
"Gives names and download links for all releases as json",
103-
)
104-
.with_arg(
105-
Arg::new()
106-
.with_name("owner")
107-
.with_short('o')
108-
.with_long("owner")
109-
.with_value_name("OWNER")
110-
.with_help("Owner of the repository"),
111-
)
112-
.with_arg(
113-
Arg::new()
114-
.with_name("repository")
115-
.with_short('r')
116-
.with_long("repository")
117-
.with_value_name("REPOSITORY")
118-
.with_help("Name of the repository"),
119-
)
120-
.with_arg(
121-
Arg::new()
122-
.with_name("output")
123-
.with_short('f')
124-
.with_long("output")
125-
.with_value_name("OUTPUT")
126-
.with_help("File path to save the json"),
127-
)
128-
.with_arg(
129-
Arg::new()
130-
.with_name("all")
131-
.with_short('a')
132-
.with_long("all")
133-
.with_help("All json from request"),
134-
)
135-
.with_arg(
136-
Arg::new()
137-
.with_name("display")
138-
.with_short('d')
139-
.with_long("display")
140-
.with_help("Converts the json to an easier format (will remove some data)."),
141-
),
142100
Command::new("help", "Helps you with the commands").with_short('h'),
143101
]);
144102

@@ -156,7 +114,7 @@ fn main() {
156114

157115
all_command(owner, repo, output, display);
158116
}
159-
"downloads" => {
117+
"releases" => {
160118
let owner = command.get_value_of("owner").throw_if_none();
161119
let repo = command.get_value_of("repository").throw_if_none();
162120
let individual = command.has("individual");
@@ -167,14 +125,7 @@ fn main() {
167125

168126
let output = output_to_path(output);
169127

170-
downloads_command(owner, repo, individual, link, output, all, display);
171-
}
172-
"releases" => {
173-
let owner = command.get_value_of("owner").throw_if_none();
174-
let repo = command.get_value_of("repository").throw_if_none();
175-
let output = command.get_value_of("output").to_option();
176-
let all = command.has("all");
177-
let display = command.has("display");
128+
releases_command(owner, repo, individual, link, output, all, display);
178129
}
179130
"help" => cli.help(),
180131
_ => cli.help(),

0 commit comments

Comments
 (0)