Add migrate cli command
This commit is contained in:
parent
909399b276
commit
9d2d1fa3c1
9 changed files with 31 additions and 1247 deletions
1165
Cargo.lock
generated
1165
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -11,7 +11,6 @@ anyhow = "1.0.95"
|
||||||
clap = { version = "4.5.28", features = ["derive", "deprecated"] }
|
clap = { version = "4.5.28", features = ["derive", "deprecated"] }
|
||||||
directories = "6.0.0"
|
directories = "6.0.0"
|
||||||
gdn = { path = "gdn" }
|
gdn = { path = "gdn" }
|
||||||
gix = "0.70.0"
|
|
||||||
rand = "0.9.0"
|
rand = "0.9.0"
|
||||||
serde = { version = "1.0.217", features = ["derive"] }
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
serde_json = "1.0.138"
|
serde_json = "1.0.138"
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ edition = { workspace = true }
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
gdn = { workspace = true }
|
gdn = { workspace = true }
|
||||||
gix = { workspace = true }
|
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
mod init;
|
mod migrate;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
|
|
@ -6,13 +6,13 @@ use crate::Environment;
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
Init(init::Command),
|
Migrate(migrate::Command),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Command {
|
impl Command {
|
||||||
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
|
pub fn run(self, env: &Environment) -> anyhow::Result<()> {
|
||||||
match self {
|
match self {
|
||||||
Self::Init(command) => command.run(env),
|
Self::Migrate(command) => command.run(env),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
14
gdn-cli/src/commands/migrate.rs
Normal file
14
gdn-cli/src/commands/migrate.rs
Normal 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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@ mod commands;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use gdn::Paths;
|
|
||||||
|
|
||||||
use crate::commands::Command;
|
use crate::commands::Command;
|
||||||
|
|
||||||
|
|
@ -19,26 +18,23 @@ struct Args {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Environment {
|
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() {
|
fn main() {
|
||||||
let args = Args::parse();
|
if let Err(err) = run() {
|
||||||
|
|
||||||
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!();
|
println!();
|
||||||
eprintln!("{err:?}");
|
eprintln!("{err:?}");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
pub mod data;
|
pub mod data;
|
||||||
pub mod ids;
|
pub mod ids;
|
||||||
mod paths;
|
|
||||||
|
|
||||||
pub use crate::paths::Paths;
|
|
||||||
|
|
||||||
pub const PROPER_NAME: &str = "GedächtNAS";
|
pub const PROPER_NAME: &str = "GedächtNAS";
|
||||||
pub const TECHNICAL_NAME: &str = "gedaechtnas";
|
pub const TECHNICAL_NAME: &str = "gedaechtnas";
|
||||||
|
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use directories::ProjectDirs;
|
|
||||||
|
|
||||||
pub struct Paths(ProjectDirs);
|
|
||||||
|
|
||||||
impl Paths {
|
|
||||||
pub fn on_linux() -> Option<Self> {
|
|
||||||
ProjectDirs::from("de", "plugh", crate::TECHNICAL_NAME).map(Self)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn on_windows() -> Option<Self> {
|
|
||||||
ProjectDirs::from("de", "plugh", crate::PROPER_NAME).map(Self)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn state_file(&self) -> PathBuf {
|
|
||||||
self.0.data_local_dir().join("state.json")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn repos_dir(&self) -> PathBuf {
|
|
||||||
self.0.data_local_dir().join("repos")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn repo_dir(&self, name: &str) -> PathBuf {
|
|
||||||
self.repos_dir().join(format!("{name}.git"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue