Skip to content

Commit 31e33b0

Browse files
author
Juliette Cordor
committed
removed ambiguous utils module
1 parent e6d8cea commit 31e33b0

File tree

5 files changed

+38
-36
lines changed

5 files changed

+38
-36
lines changed

src/cache.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@
33
use std::{
44
fs::{self, read_to_string, DirEntry},
55
io::{Read, Write},
6+
path::PathBuf,
67
};
78

89
use anyhow::Context;
910
use parking_lot::{const_mutex, Mutex};
1011

11-
use crate::utils::CACHE_DIR;
12+
use directories::BaseDirs;
13+
use lazy_static::lazy_static;
14+
15+
lazy_static! {
16+
/// The directory containing the cache
17+
pub static ref CACHE_DIR: PathBuf = BaseDirs::new()
18+
.expect("failed to find cache dir")
19+
.cache_dir()
20+
.join("gitignore");
21+
22+
/// If the cache is enabled or not
23+
pub static ref CACHE_ENABLED: bool = {
24+
let mut dir = CACHE_DIR.clone();
25+
dir.pop();
26+
dir.exists()
27+
};
28+
}
1229

1330
/// Purge the current cache
1431
pub fn purge() -> anyhow::Result<()> {
@@ -42,7 +59,7 @@ pub fn init_cache() -> anyhow::Result<()> {
4259

4360
if !cache_dir.exists() {
4461
fs::create_dir_all(&cache_dir)?;
45-
fs::File::create(&fetch_path)?.write_all(crate::utils::TIMESTAMP.to_string().as_bytes())?;
62+
fs::File::create(&fetch_path)?.write_all(crate::TIMESTAMP.to_string().as_bytes())?;
4663
return clone_templates();
4764
}
4865

@@ -53,7 +70,7 @@ pub fn init_cache() -> anyhow::Result<()> {
5370

5471
let timestamp_string = read_to_string(fetch_path)?;
5572
let timestamp = timestamp_string.parse::<u128>()?;
56-
let now = *crate::utils::TIMESTAMP;
73+
let now = *crate::TIMESTAMP;
5774

5875
let since = now - timestamp;
5976

src/commands/args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Commands {
4444
pub fn run(&self) -> anyhow::Result<()> {
4545
match self {
4646
Commands::List => {
47-
if *crate::utils::CACHE_ENABLED {
47+
if *crate::cache::CACHE_ENABLED {
4848
init_cache()?;
4949
}
5050

@@ -57,7 +57,7 @@ impl Commands {
5757
overwrite,
5858
no_overwrite,
5959
} => {
60-
if *crate::utils::CACHE_ENABLED {
60+
if *crate::cache::CACHE_ENABLED {
6161
init_cache()?;
6262
}
6363

src/commands/pull.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn run(
9191
}
9292
}
9393

94-
if *crate::utils::CACHE_ENABLED {
94+
if *crate::cache::CACHE_ENABLED {
9595
println!("Getting template {}", template_path);
9696
let template = get_template(template_path)?;
9797
let title = format!("# {}.gitignore\n", template_path);

src/main.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@
22
33
#![forbid(unsafe_code)]
44
#![warn(missing_docs)]
5+
6+
use std::time::SystemTime;
7+
58
use clap::Parser;
69

10+
use lazy_static::lazy_static;
11+
12+
lazy_static! {
13+
/// The current time in milliseconds
14+
pub static ref TIMESTAMP: u128 = SystemTime::now()
15+
.duration_since(SystemTime::UNIX_EPOCH)
16+
.expect("time went backwards")
17+
.as_millis();
18+
}
19+
720
pub mod cache;
821
pub mod commands;
922
pub mod macros;
1023
pub mod templates;
11-
pub mod utils;
1224

1325
// TODO: add custom errors with `thiserror`
1426

1527
use commands::args::Args;
1628

1729
fn main() -> anyhow::Result<()> {
18-
lazy_static::initialize(&utils::TIMESTAMP);
30+
lazy_static::initialize(&TIMESTAMP);
1931

20-
if !*utils::CACHE_ENABLED {
32+
if !*cache::CACHE_ENABLED {
2133
use mincolor::Colorize;
2234
println!(
2335
"{}",

src/utils.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)