Locate data dir on Linux and Windows
This commit is contained in:
parent
a132a9bcdf
commit
c80325ea40
8 changed files with 77 additions and 32 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
|
@ -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",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ edition = { workspace = true }
|
|||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
clap = { workspace = true }
|
||||
directories = { workspace = true }
|
||||
gdn = { workspace = true }
|
||||
gix = { workspace = true }
|
||||
|
||||
[lints]
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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:?}");
|
||||
|
|
|
|||
10
gdn/Cargo.toml
Normal file
10
gdn/Cargo.toml
Normal 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
7
gdn/src/lib.rs
Normal 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
27
gdn/src/paths.rs
Normal 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"))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue