Skip to content

Commit

Permalink
Enable visit all the folders/file inside the zet repository
Browse files Browse the repository at this point in the history
  • Loading branch information
peterramaldes committed Nov 1, 2023
1 parent 4cd9bc4 commit 500c307
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
33 changes: 6 additions & 27 deletions src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ use std::{
};

pub fn run() -> io::Result<()> {
let path = get_path_from_files();
let path = zet::dir();

// Create the path if doesn't exists, thrown an error if cannot
fs::create_dir_all(&path)?;

// Create dir if doesn´t exists
let today = Utc::now();
let folder_name = today.date_naive().format("%Y%m%d");
let folder_path = format!("{path}/{folder_name}");
let folder_path = format!(
"{}/{}",
path.into_os_string().into_string().unwrap(),
folder_name
);
let _ = fs::create_dir(&folder_path);

// Create the file
Expand All @@ -29,28 +33,3 @@ pub fn run() -> io::Result<()> {

Ok(())
}

/// Get path with this precendece:
///
/// 1. $ZET_PATH
/// 2. $HOME/.config/zet
///
/// This function is not portable, the .config folder will not work on windows
fn get_path_from_files() -> String {
let mut path = match env::var("ZET_PATH") {
Ok(val) => String::from(val),
Err(_e) => "".to_string(),
};

if path.is_empty() {
let home = match env::var("HOME") {
Ok(val) => String::from(val),
Err(_e) => panic!("doesn't have $HOME env set"), // TODO: this should not be a problem
// for windows :(
};

path = format!("{home}/.config/zet");
}

path
}
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::{
env,
path::{Path, PathBuf},
};

/// Retrieve the current dir for Zettelkasten notes
///
/// 1. $ZET_PATH
/// 2. $HOME/.config/zet
///
/// This function is not portable, the .config folder will not work on windows
pub fn dir() -> PathBuf {
let mut path = match env::var("ZET_PATH") {
Ok(val) => String::from(val),
Err(_e) => "".to_string(),
};

if path.is_empty() {
let home = match env::var("HOME") {
Ok(val) => String::from(val),
Err(_e) => panic!("doesn't have $HOME env set"), // TODO: this should not be a problem
// for windows :(
};

path = format!("{home}/.config/zet");
}

return Path::new(&path).to_path_buf();
}
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod create;
pub mod search;

use clap::Parser;

Expand All @@ -16,12 +17,17 @@ enum Subcommand {
/// the env isn´t set.
#[clap(visible_alias = "c")]
Create,

/// Search specific words on the Zettelkasten repository
#[clap(visible_alias = "s")]
Search,
}

fn main() -> std::io::Result<()> {
let cli = Cli::parse();

return match cli.subcommand {
Subcommand::Create => create::run(),
Subcommand::Search => search::run(),
};
}
25 changes: 25 additions & 0 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::{
fs::{self},
io,
path::PathBuf,
};

pub fn run() -> io::Result<()> {
let _ = visit_dirs(&zet::dir());

Ok(())
}

fn visit_dirs(dir: &PathBuf) -> io::Result<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
println!("{:?}", path);
if path.is_dir() {
visit_dirs(&path)?;
}
}
}
Ok(())
}

0 comments on commit 500c307

Please sign in to comment.