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
|
|
@ -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:?}");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue