Implement init command
This commit is contained in:
parent
1441b83a14
commit
ef65d75a08
5 changed files with 1914 additions and 14 deletions
1846
Cargo.lock
generated
1846
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -4,8 +4,10 @@ version = "0.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
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"
|
||||||
|
gix = "0.69.1"
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
rust.unsafe_code = { level = "forbid", priority = 1 }
|
rust.unsafe_code = { level = "forbid", priority = 1 }
|
||||||
|
|
|
||||||
18
src/commands.rs
Normal file
18
src/commands.rs
Normal 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
27
src/commands/init.rs
Normal 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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
35
src/main.rs
35
src/main.rs
|
|
@ -1,16 +1,11 @@
|
||||||
|
mod commands;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
|
|
||||||
/// Initialize the note repository.
|
use crate::commands::Command;
|
||||||
#[derive(Debug, Parser)]
|
|
||||||
struct CmdInit {}
|
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
|
||||||
enum Command {
|
|
||||||
Init(CmdInit),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// GedächtNAS - external storage for your brain.
|
/// GedächtNAS - external storage for your brain.
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
|
|
@ -23,6 +18,17 @@ struct Args {
|
||||||
cmd: Command,
|
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() {
|
fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let dirs = ProjectDirs::from("de", "plugh", env!("CARGO_PKG_NAME")).unwrap();
|
let dirs = ProjectDirs::from("de", "plugh", env!("CARGO_PKG_NAME")).unwrap();
|
||||||
|
|
@ -31,8 +37,19 @@ fn main() {
|
||||||
.config
|
.config
|
||||||
.unwrap_or_else(|| dirs.config_dir().join("config.toml"));
|
.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!("Config file: {}", config_file.display());
|
||||||
println!("Data dir: {}", data_dir.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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue