Skip to content

Commit 0ec24d0

Browse files
committed
path validation
1 parent b582500 commit 0ec24d0

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

src/commands/all.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
use crate::utils::{pretty_dates, request};
22
use serde_json::Value;
3+
use std::path::PathBuf;
34

45
pub fn all(
56
owner: String,
67
repo: String,
7-
output: Option<String>,
8+
output: Option<PathBuf>,
89
display: bool,
910
) -> Result<(), Box<dyn std::error::Error>> {
10-
//TODO: handle no repo/private repo
1111
let url = format!("https://api.github.com/repos/{}/{}", owner, repo);
1212

1313
let json = request(url)?;
1414

15+
if &json["message"] == "Not Found" {
16+
println!("Repository not found.");
17+
return Ok(());
18+
}
19+
1520
if !display {
1621
println!("{:#?}", json);
1722
} else {
1823
simplify_and_display_json(json)
1924
}
2025

26+
27+
2128
Ok(())
2229
}
2330

src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ pub mod cli;
22
pub mod commands;
33
pub mod utils;
44

5+
use std::process::Output;
6+
7+
use crate::cli::{Arg, Cli, Command};
58
use crate::commands::all::all;
6-
use cli::{Arg, Cli, Command};
9+
use crate::utils::validate_and_convert_path;
710

811
fn main() {
912
let cli = Cli::new().with_default_command("help").with_commands(vec![
@@ -135,6 +138,17 @@ fn main() {
135138
let output = command.get_value_of("output").to_option();
136139
let display = command.has("display");
137140

141+
let output = match output {
142+
Some(path) => match validate_and_convert_path(path) {
143+
Ok(real_path) => Some(real_path),
144+
Err(err) => {
145+
println!("{}", err);
146+
std::process::exit(0)
147+
}
148+
},
149+
None => None,
150+
};
151+
138152
let _ = all(owner, repo, output, display);
139153
}
140154
"downloads" => {

src/utils.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn request(url: String) -> Result<Value, Box<dyn std::error::Error>> {
1414

1515
fn construct_header() -> HeaderMap {
1616
let mut headers = HeaderMap::new();
17-
headers.insert(USER_AGENT, "User".parse().unwrap());
17+
headers.insert(USER_AGENT, "GSTATS".parse().unwrap());
1818
headers.insert(ACCEPT, "application/vnd.github.v3+json".parse().unwrap());
1919
return headers;
2020
}
@@ -25,14 +25,18 @@ pub fn pretty_dates(date: &str) -> String {
2525
format!("{}-{}-{}", date[2], date[1], date[0])
2626
}
2727

28-
pub fn validate_path(path: String) -> Result<PathBuf, String> {
28+
pub fn validate_and_convert_path(path: String) -> Result<PathBuf, String> {
2929
let real_path = Path::new(&path);
3030

31-
if !real_path.exists() {
32-
return Err(format!("Failed to find path \"{}\"", path));
31+
if real_path.is_file() {
32+
println!("A file was found at the path: \"{}\"", path);
33+
println!("Would you like to clear the file and continue? [y/N]");
34+
let mut input = String::new();
35+
std::io::stdin().read_line(&mut input).unwrap();
36+
if input.trim().to_lowercase() != "y" {
37+
return Err("A file already exists at the path.".to_owned());
38+
}
3339
}
3440

35-
36-
3741
Ok(real_path.to_owned())
3842
}

0 commit comments

Comments
 (0)