mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 23:36:11 +00:00
feat: provider wizards (init, login, new, import, config repo, bootstrap)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e599d48db8
commit
b86342b1d0
10 files changed
+795
-11
No files matched your search
@@ -1,11 +1,11 @@
|
||||
//! `config.toml` model, loading and validation (section 6 of the plan).
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
||||
pub struct Config {
|
||||
pub machine: Machine,
|
||||
pub root: Root,
|
||||
@@ -15,20 +15,21 @@ pub struct Config {
|
||||
pub groups: Vec<Group>,
|
||||
#[serde(default)]
|
||||
pub sync: Sync,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub config_repo: Option<ConfigRepo>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
||||
pub struct Machine {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
||||
pub struct Root {
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ProviderKind {
|
||||
Gitea,
|
||||
@@ -36,13 +37,14 @@ pub enum ProviderKind {
|
||||
Github,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
||||
pub struct Provider {
|
||||
pub kind: ProviderKind,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub url: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum Layout {
|
||||
#[default]
|
||||
@@ -50,7 +52,7 @@ pub enum Layout {
|
||||
Plain,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
||||
pub struct Group {
|
||||
pub name: String,
|
||||
pub primary: String,
|
||||
@@ -62,7 +64,7 @@ pub struct Group {
|
||||
pub auto_materialize: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
||||
pub struct Sync {
|
||||
#[serde(default = "default_debounce")]
|
||||
pub snapshot_debounce_ms: u64,
|
||||
@@ -82,7 +84,7 @@ impl Default for Sync {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
||||
pub struct ConfigRepo {
|
||||
pub provider: String,
|
||||
pub name: String,
|
||||
@@ -124,6 +126,18 @@ impl Config {
|
||||
Ok(cfg)
|
||||
}
|
||||
|
||||
/// Writes the config atomically (temp file + rename).
|
||||
pub fn save(&self, path: &Path) -> Result<(), ConfigError> {
|
||||
let text = toml::to_string_pretty(self).map_err(|e| ConfigError::Parse(e.to_string()))?;
|
||||
let io = |e| ConfigError::Io(path.to_path_buf(), e);
|
||||
if let Some(dir) = path.parent() {
|
||||
std::fs::create_dir_all(dir).map_err(io)?;
|
||||
}
|
||||
let tmp = path.with_extension("toml.tmp");
|
||||
std::fs::write(&tmp, text).map_err(io)?;
|
||||
std::fs::rename(&tmp, path).map_err(io)
|
||||
}
|
||||
|
||||
pub fn load(path: &Path) -> Result<Config, ConfigError> {
|
||||
let text =
|
||||
std::fs::read_to_string(path).map_err(|e| ConfigError::Io(path.to_path_buf(), e))?;
|
||||
|
||||
@@ -6,6 +6,7 @@ pub mod inspect;
|
||||
pub mod layout;
|
||||
pub mod migrate;
|
||||
pub mod receive;
|
||||
pub mod registry;
|
||||
pub mod snapshot;
|
||||
pub mod state;
|
||||
pub mod sync;
|
||||
@@ -0,0 +1,114 @@
|
||||
//! The config repo: a private git repo holding `repos.toml` (what to materialize) and one
|
||||
//! heartbeat file per machine. A fresh machine needs nothing else to rebuild its workspace.
|
||||
|
||||
use crate::git::{Git, GitError};
|
||||
use crate::layout::dir_name;
|
||||
use crate::receive::{ensure_fetch_refspec, materialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize, Serialize)]
|
||||
pub struct Registry {
|
||||
#[serde(default)]
|
||||
pub repos: Vec<Entry>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct Entry {
|
||||
pub group: String,
|
||||
pub name: String,
|
||||
/// Clone URL on the group's primary remote.
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
impl Registry {
|
||||
pub fn parse(text: &str) -> Result<Registry, String> {
|
||||
toml::from_str(text).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
pub fn to_text(&self) -> String {
|
||||
toml::to_string_pretty(self).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Adds or updates an entry (same group + name).
|
||||
pub fn upsert(&mut self, e: Entry) {
|
||||
match self
|
||||
.repos
|
||||
.iter_mut()
|
||||
.find(|x| x.group == e.group && x.name == e.name)
|
||||
{
|
||||
Some(x) => *x = e,
|
||||
None => self.repos.push(e),
|
||||
}
|
||||
self.repos
|
||||
.sort_by(|a, b| (&a.group, &a.name).cmp(&(&b.group, &b.name)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates `<dir>/.bare` as a clone of `url` and a worktree per remote branch.
|
||||
/// `envs` are extra git environment variables (credentials).
|
||||
pub fn clone_bare_layout(
|
||||
url: &str,
|
||||
dir: &Path,
|
||||
envs: &[(String, String)],
|
||||
) -> Result<Vec<PathBuf>, GitError> {
|
||||
fs::create_dir_all(dir)?;
|
||||
let bare = dir.join(".bare");
|
||||
let mut g = Git::new(dir);
|
||||
for (k, v) in envs {
|
||||
g = g.with_env(k, v);
|
||||
}
|
||||
g.run(&["clone", "-q", "--bare", url, &bare.to_string_lossy()])?;
|
||||
Git::new(&bare).run(&["config", "core.bare", "true"])?;
|
||||
ensure_fetch_refspec(&bare, "origin")?;
|
||||
let mut f = Git::new(&bare);
|
||||
for (k, v) in envs {
|
||||
f = f.with_env(k, v);
|
||||
}
|
||||
f.run(&["fetch", "-q", "--prune", "origin"])?;
|
||||
materialize(dir, "origin")
|
||||
}
|
||||
|
||||
/// Creates a brand new local repo in the bare layout with an initial empty commit on `main`,
|
||||
/// pointing at `url`. Nothing is pushed here.
|
||||
pub fn init_bare_layout(dir: &Path, url: &str) -> Result<PathBuf, GitError> {
|
||||
fs::create_dir_all(dir)?;
|
||||
let bare = dir.join(".bare");
|
||||
let g = Git::new(dir);
|
||||
g.run(&[
|
||||
"init",
|
||||
"-q",
|
||||
"--bare",
|
||||
"-b",
|
||||
"main",
|
||||
&bare.to_string_lossy(),
|
||||
])?;
|
||||
let b = Git::new(&bare);
|
||||
b.run(&["remote", "add", "origin", url])?;
|
||||
ensure_fetch_refspec(&bare, "origin")?;
|
||||
// `hash-object -t tree` on an empty file writes the empty tree object.
|
||||
let tree = b.run(&["hash-object", "-t", "tree", "-w", "/dev/null"])?;
|
||||
let commit = b
|
||||
.with_env("GIT_AUTHOR_NAME", "grove")
|
||||
.with_env("GIT_AUTHOR_EMAIL", "grove@localhost")
|
||||
.with_env("GIT_COMMITTER_NAME", "grove")
|
||||
.with_env("GIT_COMMITTER_EMAIL", "grove@localhost")
|
||||
.run(&["commit-tree", &tree, "-m", "chore: initial commit"])?;
|
||||
let b = Git::new(&bare);
|
||||
b.run(&["update-ref", "refs/heads/main", &commit])?;
|
||||
let name = dir_name("main", &[]);
|
||||
let wt = dir.join(&name);
|
||||
b.run(&["worktree", "add", "-q", &wt.to_string_lossy(), "main"])?;
|
||||
Ok(wt)
|
||||
}
|
||||
|
||||
/// Pushes `main` of a freshly initialised layout and sets the upstream.
|
||||
pub fn push_initial(dir: &Path, envs: &[(String, String)]) -> Result<(), GitError> {
|
||||
let mut g = Git::new(dir.join(".bare"));
|
||||
for (k, v) in envs {
|
||||
g = g.with_env(k, v);
|
||||
}
|
||||
g.run(&["push", "-q", "-u", "origin", "main"])?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user