Skip to content

Commit 77528ba

Browse files
committed
renamed write_json_to_file to write_to_file & now takes String & release command output to file
1 parent 4d559f8 commit 77528ba

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/commands/all.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{pretty_dates, request, write_json_to_file};
1+
use crate::utils::{pretty_dates, request, write_to_file};
22
use serde_json::Value;
33
use std::path::PathBuf;
44

@@ -20,7 +20,7 @@ pub fn all_command(owner: String, repo: String, output: Option<PathBuf>, display
2020

2121
match output {
2222
Some(path) => {
23-
let result = write_json_to_file(json, path);
23+
let result = write_to_file(serde_json::to_string_pretty(&json).unwrap(), path);
2424
match result {
2525
Ok(_) => {}
2626
Err(err) => println!("{}", err),

src/commands/releases.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{bytes_to_best_size, pretty_dates, request, write_json_to_file};
1+
use crate::utils::{bytes_to_best_size, pretty_dates, request, write_to_file};
22
use serde::{Deserialize, Serialize};
33
use serde_json::Value;
44
use std::path::PathBuf;
@@ -123,6 +123,7 @@ pub fn releases_command(
123123

124124
if all && !display {
125125
println!("{}", serde_json::to_string_pretty(&json).unwrap());
126+
write_json(serde_json::to_string_pretty(&json).unwrap(), output);
126127
} else if all && display {
127128
let mut download_count = 0;
128129
for release in &simple_data {
@@ -133,9 +134,11 @@ pub fn releases_command(
133134
if link {
134135
println!("Latest Release: {}", simple_data[0].html_url)
135136
}
137+
write_json(serde_json::to_string_pretty(&simple_data).unwrap(), output);
136138
} else if !all && !display {
137139
if individual {
138140
println!("{}", serde_json::to_string_pretty(&simple_data).unwrap());
141+
write_json(serde_json::to_string_pretty(&simple_data).unwrap(), output);
139142
} else {
140143
let mut overview = simple_data[0].clone();
141144
let mut download_count = 0;
@@ -146,6 +149,7 @@ pub fn releases_command(
146149
overview.downloads = download_count;
147150

148151
println!("{}", serde_json::to_string_pretty(&overview).unwrap());
152+
write_json(serde_json::to_string_pretty(&overview).unwrap(), output);
149153
}
150154
} else if !all && display {
151155
let mut download_count = 0;
@@ -163,11 +167,14 @@ pub fn releases_command(
163167
if link {
164168
println!("Latest Release: {}", simple_data[0].html_url)
165169
}
170+
write_json(serde_json::to_string_pretty(&simple_data).unwrap(), output);
166171
}
172+
}
167173

174+
fn write_json(json_string: String, output: Option<PathBuf>) {
168175
match output {
169176
Some(path) => {
170-
let result = write_json_to_file(json, path);
177+
let result = write_to_file(serde_json::to_string_pretty(&json_string).unwrap(), path);
171178
match result {
172179
Ok(_) => {}
173180
Err(err) => println!("{}", err),

src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn validate_and_convert_path(path: String) -> Result<PathBuf, String> {
4949
Ok(real_path.to_owned())
5050
}
5151

52-
pub fn write_json_to_file(json: Value, mut path: PathBuf) -> Result<(), String> {
52+
pub fn write_to_file(json_string: String, mut path: PathBuf) -> Result<(), String> {
5353
if path.extension().is_none() {
5454
path.push("gstats-output.json");
5555
}
@@ -64,7 +64,7 @@ pub fn write_json_to_file(json: Value, mut path: PathBuf) -> Result<(), String>
6464
};
6565

6666
let mut writer = BufWriter::new(file);
67-
let json_string = serde_json::to_string_pretty(&json).unwrap();
67+
let json_string = json_string;
6868
writer.write_all(json_string.as_bytes()).unwrap();
6969

7070
Ok(())

0 commit comments

Comments
 (0)