Add migrate cli command

This commit is contained in:
Joscha 2025-02-19 13:49:58 +01:00
parent 909399b276
commit 9d2d1fa3c1
9 changed files with 31 additions and 1247 deletions

View file

@ -7,7 +7,6 @@ edition = { workspace = true }
anyhow = { workspace = true }
clap = { workspace = true }
gdn = { workspace = true }
gix = { workspace = true }
[lints]
workspace = true

View file

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

View file

@ -1,29 +0,0 @@
use std::fs;
use anyhow::Context;
use clap::Parser;
use crate::Environment;
/// Initialize a note repository.
#[derive(Debug, Parser)]
pub struct Command {
repo_name: String,
}
impl Command {
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
let dir = env.paths.repo_dir(&self.repo_name);
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(&dir)
.with_context(|| format!("Failed to initialize bare git repo at {}", dir.display()))
.context("Failed to initialize notes repo")?;
dbg!(repo);
Ok(())
}
}

View file

@ -0,0 +1,14 @@
use clap::Parser;
use crate::Environment;
/// Create or migrate the data dir, if necessary.
#[derive(Debug, Parser)]
pub struct Command {}
impl Command {
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
gdn::data::open_and_migrate(env.data_dir.clone())?;
Ok(())
}
}

View file

@ -3,7 +3,6 @@ mod commands;
use std::path::PathBuf;
use clap::Parser;
use gdn::Paths;
use crate::commands::Command;
@ -19,26 +18,23 @@ struct Args {
}
struct Environment {
paths: Paths,
data_dir: PathBuf,
}
fn run() -> anyhow::Result<()> {
let args = Args::parse();
let env = Environment {
data_dir: gdn::data::path()?,
};
println!("Data dir: {}", env.data_dir.display());
args.cmd.run(&env)?;
Ok(())
}
fn main() {
let args = Args::parse();
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) {
if let Err(err) = run() {
println!();
eprintln!("{err:?}");
std::process::exit(1);