Implement init command

This commit is contained in:
Joscha 2025-01-14 01:40:27 +01:00
parent 1441b83a14
commit ef65d75a08
5 changed files with 1914 additions and 14 deletions

1846
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,8 +4,10 @@ version = "0.0.0"
edition = "2021"
[dependencies]
anyhow = "1.0.95"
clap = { version = "4.5.26", features = ["derive", "deprecated"] }
directories = "6.0.0"
gix = "0.69.1"
[lints]
rust.unsafe_code = { level = "forbid", priority = 1 }

18
src/commands.rs Normal file
View file

@ -0,0 +1,18 @@
mod init;
use clap::Parser;
use crate::Environment;
#[derive(Debug, Parser)]
pub enum Command {
Init(init::Command),
}
impl Command {
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
match self {
Self::Init(command) => command.run(env),
}
}
}

27
src/commands/init.rs Normal file
View file

@ -0,0 +1,27 @@
use std::fs;
use anyhow::Context;
use clap::Parser;
use crate::Environment;
/// Initialize the note repository.
#[derive(Debug, Parser)]
pub struct Command {}
impl Command {
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
let directory = env.repo_dir();
fs::create_dir_all(&directory)
.with_context(|| format!("Failed to create directory {}", directory.display()))
.context("Failed to initialize notes repo")?;
let repo = gix::init_bare(&directory)
.with_context(|| format!("Failed to initialize git repo at {}", directory.display()))
.context("Failed to initialize notes repo")?;
dbg!(repo);
Ok(())
}
}

View file

@ -1,16 +1,11 @@
mod commands;
use std::path::PathBuf;
use clap::Parser;
use directories::ProjectDirs;
/// Initialize the note repository.
#[derive(Debug, Parser)]
struct CmdInit {}
#[derive(Debug, Parser)]
enum Command {
Init(CmdInit),
}
use crate::commands::Command;
/// GedächtNAS - external storage for your brain.
#[derive(Debug, Parser)]
@ -23,6 +18,17 @@ struct Args {
cmd: Command,
}
struct Environment {
config_file: PathBuf,
data_dir: PathBuf,
}
impl Environment {
fn repo_dir(&self) -> PathBuf {
self.data_dir.join("notes.git")
}
}
fn main() {
let args = Args::parse();
let dirs = ProjectDirs::from("de", "plugh", env!("CARGO_PKG_NAME")).unwrap();
@ -31,8 +37,19 @@ fn main() {
.config
.unwrap_or_else(|| dirs.config_dir().join("config.toml"));
let data_dir = dirs.data_dir();
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,
};
if let Err(err) = args.cmd.run(&env) {
println!();
eprintln!("{err:?}");
std::process::exit(1);
}
}