Locate data dir on Linux and Windows

This commit is contained in:
Joscha 2025-01-25 12:01:21 +01:00
parent a132a9bcdf
commit c80325ea40
8 changed files with 77 additions and 32 deletions

9
Cargo.lock generated
View file

@ -344,13 +344,20 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "gdn"
version = "0.0.0"
dependencies = [
"directories",
]
[[package]] [[package]]
name = "gdn-cli" name = "gdn-cli"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"directories", "gdn",
"gix", "gix",
] ]

View file

@ -1,6 +1,6 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = ["gdn-cli"] members = ["gdn", "gdn-cli"]
[workspace.package] [workspace.package]
version = "0.0.0" version = "0.0.0"
@ -10,6 +10,7 @@ edition = "2021"
anyhow = "1.0.95" anyhow = "1.0.95"
clap = { version = "4.5.26", features = ["derive", "deprecated"] } clap = { version = "4.5.26", features = ["derive", "deprecated"] }
directories = "6.0.0" directories = "6.0.0"
gdn = { path = "gdn" }
gix = "0.69.1" gix = "0.69.1"
[workspace.lints] [workspace.lints]

View file

@ -6,7 +6,7 @@ edition = { workspace = true }
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
clap = { workspace = true } clap = { workspace = true }
directories = { workspace = true } gdn = { workspace = true }
gix = { workspace = true } gix = { workspace = true }
[lints] [lints]

View file

@ -5,20 +5,22 @@ use clap::Parser;
use crate::Environment; use crate::Environment;
/// Initialize the note repository. /// Initialize a note repository.
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct Command {} pub struct Command {
repo_name: String,
}
impl Command { impl Command {
pub fn run(self, env: &Environment) -> anyhow::Result<()> { 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) fs::create_dir_all(&dir)
.with_context(|| format!("Failed to create directory {}", directory.display())) .with_context(|| format!("Failed to create directory {}", dir.display()))
.context("Failed to initialize notes repo")?; .context("Failed to initialize notes repo")?;
let repo = gix::init_bare(&directory) let repo = gix::init_bare(&dir)
.with_context(|| format!("Failed to initialize git repo at {}", directory.display())) .with_context(|| format!("Failed to initialize bare git repo at {}", dir.display()))
.context("Failed to initialize notes repo")?; .context("Failed to initialize notes repo")?;
dbg!(repo); dbg!(repo);

View file

@ -3,7 +3,7 @@ mod commands;
use std::path::PathBuf; use std::path::PathBuf;
use clap::Parser; use clap::Parser;
use directories::ProjectDirs; use gdn::Paths;
use crate::commands::Command; use crate::commands::Command;
@ -19,34 +19,25 @@ struct Args {
} }
struct Environment { struct Environment {
config_file: PathBuf, paths: Paths,
data_dir: PathBuf,
}
impl Environment {
fn repo_dir(&self) -> PathBuf {
self.data_dir.join("notes.git")
}
} }
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let dirs = ProjectDirs::from("de", "plugh", env!("CARGO_PKG_NAME")).unwrap();
let config_file = args let paths = if cfg!(unix) {
.config Paths::on_linux().unwrap()
.unwrap_or_else(|| dirs.config_dir().join("config.toml")); } else if cfg!(windows) {
Paths::on_windows().unwrap()
let data_dir = dirs.data_dir().to_path_buf(); } else {
panic!("running on unsupported platform, only Linux and Windows are supported")
println!("Config file: {}", config_file.display());
println!("Data dir: {}", data_dir.display());
let env = Environment {
config_file,
data_dir,
}; };
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) { if let Err(err) = args.cmd.run(&env) {
println!(); println!();
eprintln!("{err:?}"); eprintln!("{err:?}");

10
gdn/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "gdn"
version = { workspace = true }
edition = { workspace = true }
[dependencies]
directories.workspace = true
[lints]
workspace = true

7
gdn/src/lib.rs Normal file
View file

@ -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";

27
gdn/src/paths.rs Normal file
View file

@ -0,0 +1,27 @@
use std::path::PathBuf;
use directories::ProjectDirs;
pub struct Paths(ProjectDirs);
impl Paths {
pub fn on_linux() -> Option<Self> {
ProjectDirs::from("de", "plugh", crate::TECHNICAL_NAME).map(Self)
}
pub fn on_windows() -> Option<Self> {
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"))
}
}