Set up cargo workspace

This commit is contained in:
Joscha 2025-01-24 01:03:18 +01:00
parent ef65d75a08
commit 89d6006d8d
6 changed files with 21 additions and 5 deletions

13
gdn-cli/Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "gdn-cli"
version = { workspace = true }
edition = { workspace = true }
[dependencies]
anyhow = { workspace = true }
clap = { workspace = true }
directories = { workspace = true }
gix = { workspace = true }
[lints]
workspace = true

18
gdn-cli/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),
}
}
}

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(())
}
}

55
gdn-cli/src/main.rs Normal file
View file

@ -0,0 +1,55 @@
mod commands;
use std::path::PathBuf;
use clap::Parser;
use directories::ProjectDirs;
use crate::commands::Command;
/// GedächtNAS - external storage for your brain.
#[derive(Debug, Parser)]
#[command(version)]
struct Args {
/// Path to the config file.
#[arg(long, short)]
config: Option<PathBuf>,
#[command(subcommand)]
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();
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,
};
if let Err(err) = args.cmd.run(&env) {
println!();
eprintln!("{err:?}");
std::process::exit(1);
}
}