Skip to content

Commit

Permalink
removed ambiguous utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliette Cordor committed Dec 5, 2022
1 parent e6d8cea commit 31e33b0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
23 changes: 20 additions & 3 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@
use std::{
fs::{self, read_to_string, DirEntry},
io::{Read, Write},
path::PathBuf,
};

use anyhow::Context;
use parking_lot::{const_mutex, Mutex};

use crate::utils::CACHE_DIR;
use directories::BaseDirs;
use lazy_static::lazy_static;

lazy_static! {
/// The directory containing the cache
pub static ref CACHE_DIR: PathBuf = BaseDirs::new()
.expect("failed to find cache dir")
.cache_dir()
.join("gitignore");

/// If the cache is enabled or not
pub static ref CACHE_ENABLED: bool = {
let mut dir = CACHE_DIR.clone();
dir.pop();
dir.exists()
};
}

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

if !cache_dir.exists() {
fs::create_dir_all(&cache_dir)?;
fs::File::create(&fetch_path)?.write_all(crate::utils::TIMESTAMP.to_string().as_bytes())?;
fs::File::create(&fetch_path)?.write_all(crate::TIMESTAMP.to_string().as_bytes())?;
return clone_templates();
}

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

let timestamp_string = read_to_string(fetch_path)?;
let timestamp = timestamp_string.parse::<u128>()?;
let now = *crate::utils::TIMESTAMP;
let now = *crate::TIMESTAMP;

let since = now - timestamp;

Expand Down
4 changes: 2 additions & 2 deletions src/commands/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Commands {
pub fn run(&self) -> anyhow::Result<()> {
match self {
Commands::List => {
if *crate::utils::CACHE_ENABLED {
if *crate::cache::CACHE_ENABLED {
init_cache()?;
}

Expand All @@ -57,7 +57,7 @@ impl Commands {
overwrite,
no_overwrite,
} => {
if *crate::utils::CACHE_ENABLED {
if *crate::cache::CACHE_ENABLED {
init_cache()?;
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn run(
}
}

if *crate::utils::CACHE_ENABLED {
if *crate::cache::CACHE_ENABLED {
println!("Getting template {}", template_path);
let template = get_template(template_path)?;
let title = format!("# {}.gitignore\n", template_path);
Expand Down
18 changes: 15 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,34 @@
#![forbid(unsafe_code)]
#![warn(missing_docs)]

use std::time::SystemTime;

use clap::Parser;

use lazy_static::lazy_static;

lazy_static! {
/// The current time in milliseconds
pub static ref TIMESTAMP: u128 = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("time went backwards")
.as_millis();
}

pub mod cache;
pub mod commands;
pub mod macros;
pub mod templates;
pub mod utils;

// TODO: add custom errors with `thiserror`

use commands::args::Args;

fn main() -> anyhow::Result<()> {
lazy_static::initialize(&utils::TIMESTAMP);
lazy_static::initialize(&TIMESTAMP);

if !*utils::CACHE_ENABLED {
if !*cache::CACHE_ENABLED {
use mincolor::Colorize;
println!(
"{}",
Expand Down
27 changes: 0 additions & 27 deletions src/utils.rs

This file was deleted.

0 comments on commit 31e33b0

Please sign in to comment.