Skip to content

Commit abc8bf4

Browse files
committed
file output for all command
1 parent 0ec24d0 commit abc8bf4

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

src/commands/all.rs

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

@@ -20,15 +20,24 @@ pub fn all(
2020
if !display {
2121
println!("{:#?}", json);
2222
} else {
23-
simplify_and_display_json(json)
23+
simplify_and_display_json(&json)
2424
}
2525

26-
26+
match output {
27+
Some(path) => {
28+
let result = write_json_to_file(json, path);
29+
match result {
30+
Ok(_) => {}
31+
Err(err) => println!("{}", err),
32+
}
33+
}
34+
None => {}
35+
}
2736

2837
Ok(())
2938
}
3039

31-
fn simplify_and_display_json(json: Value) {
40+
fn simplify_and_display_json(json: &Value) {
3241
// General Info
3342
let name = &json["name"].as_str().unwrap_or("None");
3443
let description = &json["description"].as_str().unwrap_or("None");

src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ pub mod cli;
22
pub mod commands;
33
pub mod utils;
44

5-
use std::process::Output;
6-
75
use crate::cli::{Arg, Cli, Command};
86
use crate::commands::all::all;
97
use crate::utils::validate_and_convert_path;

src/utils.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use reqwest::header::{HeaderMap, ACCEPT, USER_AGENT};
22
use serde_json::Value;
3+
use std::fs::{create_dir_all, remove_file, File};
4+
use std::io::{BufWriter, Write};
35
use std::path::{Path, PathBuf};
46

57
pub fn request(url: String) -> Result<Value, Box<dyn std::error::Error>> {
@@ -28,15 +30,42 @@ pub fn pretty_dates(date: &str) -> String {
2830
pub fn validate_and_convert_path(path: String) -> Result<PathBuf, String> {
2931
let real_path = Path::new(&path);
3032

33+
if real_path.extension().is_some() && real_path.extension().unwrap() != "json" {
34+
return Err("The file must be a json file.".to_owned());
35+
}
36+
3137
if real_path.is_file() {
3238
println!("A file was found at the path: \"{}\"", path);
3339
println!("Would you like to clear the file and continue? [y/N]");
3440
let mut input = String::new();
3541
std::io::stdin().read_line(&mut input).unwrap();
3642
if input.trim().to_lowercase() != "y" {
3743
return Err("A file already exists at the path.".to_owned());
44+
} else {
45+
remove_file(&real_path).expect("Failed to delete existing directory");
3846
}
3947
}
4048

4149
Ok(real_path.to_owned())
4250
}
51+
52+
pub fn write_json_to_file(json: Value, mut path: PathBuf) -> Result<(), String> {
53+
if path.extension().is_none() {
54+
path.push("gstats-output.json");
55+
}
56+
57+
println!("Saving the json to: {:?}", path);
58+
create_dir_all(path.parent().unwrap()).unwrap();
59+
let file = match File::create(&path) {
60+
Ok(file) => file,
61+
Err(_) => {
62+
return Err("Failed to create the file.".to_owned());
63+
}
64+
};
65+
66+
let mut writer = BufWriter::new(file);
67+
let json_string = serde_json::to_string_pretty(&json).unwrap();
68+
writer.write_all(json_string.as_bytes()).unwrap();
69+
70+
Ok(())
71+
}

0 commit comments

Comments
 (0)