From c80325ea40e594356814605a558e74bcbebf5e60 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 25 Jan 2025 12:01:21 +0100 Subject: [PATCH] Locate data dir on Linux and Windows --- Cargo.lock | 9 ++++++++- Cargo.toml | 3 ++- gdn-cli/Cargo.toml | 2 +- gdn-cli/src/commands/init.rs | 16 +++++++++------- gdn-cli/src/main.rs | 35 +++++++++++++---------------------- gdn/Cargo.toml | 10 ++++++++++ gdn/src/lib.rs | 7 +++++++ gdn/src/paths.rs | 27 +++++++++++++++++++++++++++ 8 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 gdn/Cargo.toml create mode 100644 gdn/src/lib.rs create mode 100644 gdn/src/paths.rs diff --git a/Cargo.lock b/Cargo.lock index b1eb2cb..ad9bee7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -344,13 +344,20 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "gdn" +version = "0.0.0" +dependencies = [ + "directories", +] + [[package]] name = "gdn-cli" version = "0.0.0" dependencies = [ "anyhow", "clap", - "directories", + "gdn", "gix", ] diff --git a/Cargo.toml b/Cargo.toml index 0f343a0..80bba72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = ["gdn-cli"] +members = ["gdn", "gdn-cli"] [workspace.package] version = "0.0.0" @@ -10,6 +10,7 @@ edition = "2021" anyhow = "1.0.95" clap = { version = "4.5.26", features = ["derive", "deprecated"] } directories = "6.0.0" +gdn = { path = "gdn" } gix = "0.69.1" [workspace.lints] diff --git a/gdn-cli/Cargo.toml b/gdn-cli/Cargo.toml index 2d39b42..ef01560 100644 --- a/gdn-cli/Cargo.toml +++ b/gdn-cli/Cargo.toml @@ -6,7 +6,7 @@ edition = { workspace = true } [dependencies] anyhow = { workspace = true } clap = { workspace = true } -directories = { workspace = true } +gdn = { workspace = true } gix = { workspace = true } [lints] diff --git a/gdn-cli/src/commands/init.rs b/gdn-cli/src/commands/init.rs index ec9b7ea..f07c060 100644 --- a/gdn-cli/src/commands/init.rs +++ b/gdn-cli/src/commands/init.rs @@ -5,20 +5,22 @@ use clap::Parser; use crate::Environment; -/// Initialize the note repository. +/// Initialize a note repository. #[derive(Debug, Parser)] -pub struct Command {} +pub struct Command { + repo_name: String, +} impl Command { pub fn run(self, env: &Environment) -> anyhow::Result<()> { - let directory = env.repo_dir(); + let dir = env.paths.repo_dir(&self.repo_name); - fs::create_dir_all(&directory) - .with_context(|| format!("Failed to create directory {}", directory.display())) + fs::create_dir_all(&dir) + .with_context(|| format!("Failed to create directory {}", dir.display())) .context("Failed to initialize notes repo")?; - let repo = gix::init_bare(&directory) - .with_context(|| format!("Failed to initialize git repo at {}", directory.display())) + let repo = gix::init_bare(&dir) + .with_context(|| format!("Failed to initialize bare git repo at {}", dir.display())) .context("Failed to initialize notes repo")?; dbg!(repo); diff --git a/gdn-cli/src/main.rs b/gdn-cli/src/main.rs index 621f841..ced7541 100644 --- a/gdn-cli/src/main.rs +++ b/gdn-cli/src/main.rs @@ -3,7 +3,7 @@ mod commands; use std::path::PathBuf; use clap::Parser; -use directories::ProjectDirs; +use gdn::Paths; use crate::commands::Command; @@ -19,34 +19,25 @@ struct Args { } struct Environment { - config_file: PathBuf, - data_dir: PathBuf, -} - -impl Environment { - fn repo_dir(&self) -> PathBuf { - self.data_dir.join("notes.git") - } + paths: Paths, } fn main() { let args = Args::parse(); - let dirs = ProjectDirs::from("de", "plugh", env!("CARGO_PKG_NAME")).unwrap(); - let config_file = args - .config - .unwrap_or_else(|| dirs.config_dir().join("config.toml")); - - let data_dir = dirs.data_dir().to_path_buf(); - - println!("Config file: {}", config_file.display()); - println!("Data dir: {}", data_dir.display()); - - let env = Environment { - config_file, - data_dir, + let paths = if cfg!(unix) { + Paths::on_linux().unwrap() + } else if cfg!(windows) { + Paths::on_windows().unwrap() + } else { + panic!("running on unsupported platform, only Linux and Windows are supported") }; + println!("State file: {}", paths.state_file().display()); + println!("Repos dir: {}", paths.repos_dir().display()); + + let env = Environment { paths }; + if let Err(err) = args.cmd.run(&env) { println!(); eprintln!("{err:?}"); diff --git a/gdn/Cargo.toml b/gdn/Cargo.toml new file mode 100644 index 0000000..f7e6bae --- /dev/null +++ b/gdn/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "gdn" +version = { workspace = true } +edition = { workspace = true } + +[dependencies] +directories.workspace = true + +[lints] +workspace = true diff --git a/gdn/src/lib.rs b/gdn/src/lib.rs new file mode 100644 index 0000000..7928427 --- /dev/null +++ b/gdn/src/lib.rs @@ -0,0 +1,7 @@ +mod paths; + +pub use crate::paths::Paths; + +pub const PROPER_NAME: &str = "GedächtNAS"; +pub const TECHNICAL_NAME: &str = "gedaechtnas"; +pub const ABBREVIATED_NAME: &str = "gdn"; diff --git a/gdn/src/paths.rs b/gdn/src/paths.rs new file mode 100644 index 0000000..152800c --- /dev/null +++ b/gdn/src/paths.rs @@ -0,0 +1,27 @@ +use std::path::PathBuf; + +use directories::ProjectDirs; + +pub struct Paths(ProjectDirs); + +impl Paths { + pub fn on_linux() -> Option { + ProjectDirs::from("de", "plugh", crate::TECHNICAL_NAME).map(Self) + } + + pub fn on_windows() -> Option { + ProjectDirs::from("de", "plugh", crate::PROPER_NAME).map(Self) + } + + pub fn state_file(&self) -> PathBuf { + self.0.data_local_dir().join("state.json") + } + + pub fn repos_dir(&self) -> PathBuf { + self.0.data_local_dir().join("repos") + } + + pub fn repo_dir(&self, name: &str) -> PathBuf { + self.repos_dir().join(format!("{name}.git")) + } +}