mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 23:36:11 +00:00
feat: manage namespaces/providers and open worktrees in editors
- grove group add|remove|list and grove provider add|remove|list edit config.toml (validated first, previous file kept as config.toml.bak) - grove open [group/]repo[@branch] launches Zed, VS Code, Claude Code (in a terminal) or Claude Desktop; [open] config for defaults, terminal and own launchers; grove new --open - GUI: provider/namespace forms and removal in Settings, Open buttons on repos and worktrees, open-after-create on New repository - docs/phase8.md, DECISIONS 10-11 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011GZUAGhGYoiLvmfXweQnDY
This commit is contained in:
13 files changed
+2075
-25
No files matched your search
@@ -18,6 +18,9 @@ const ALLOWED: &[&str] = &[
|
||||
"secrets",
|
||||
"sync",
|
||||
"status",
|
||||
"group",
|
||||
"provider",
|
||||
"open",
|
||||
];
|
||||
|
||||
pub struct Bridge {
|
||||
@@ -53,6 +56,17 @@ impl Bridge {
|
||||
Err(e) => return json!({"ok": false, "output": format!("rejected: {e}")}),
|
||||
}
|
||||
}
|
||||
// `open` only for a directory under the workspace root, in canonical form.
|
||||
if sub == "open" {
|
||||
let checked_path = self
|
||||
.loaded_config()
|
||||
.ok_or_else(|| "no readable configuration".to_string())
|
||||
.and_then(|cfg| check_open_path(&cfg, Path::new(&args[1])));
|
||||
match checked_path {
|
||||
Ok(canon) => checked[1] = canon.to_string_lossy().into_owned(),
|
||||
Err(e) => return json!({"ok": false, "output": format!("rejected: {e}")}),
|
||||
}
|
||||
}
|
||||
let args = &checked[..];
|
||||
let mut cmd = Command::new(&self.grove_bin);
|
||||
if let Some(c) = &self.config {
|
||||
@@ -106,6 +120,26 @@ impl Bridge {
|
||||
check_migrate_path(&cfg, Path::new(path))
|
||||
}
|
||||
|
||||
/// Installed launchers (name, label) for "Open in" buttons, the default one first.
|
||||
pub fn launchers(&self) -> Vec<(String, String)> {
|
||||
use grove_core::launch::{System, launchers, pick};
|
||||
let Some(cfg) = self.loaded_config() else {
|
||||
return Vec::new();
|
||||
};
|
||||
let all = launchers(&cfg.open, &System);
|
||||
let default = pick(&all, &cfg.open, None).ok().map(|l| l.name.clone());
|
||||
let mut out: Vec<(String, String)> = all
|
||||
.into_iter()
|
||||
.filter(|l| l.available)
|
||||
.map(|l| (l.name, l.label))
|
||||
.collect();
|
||||
if let Some(i) = out.iter().position(|(n, _)| Some(n) == default.as_ref()) {
|
||||
let d = out.remove(i);
|
||||
out.insert(0, d);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Where `grove add` writes its safety backup.
|
||||
pub fn backup_dir() -> Option<PathBuf> {
|
||||
grove_core::config::Config::state_dir().map(|d| d.join("backups"))
|
||||
@@ -135,7 +169,20 @@ fn flag_spec(sub: &str) -> (&'static [&'static str], &'static [&'static str]) {
|
||||
"--url",
|
||||
],
|
||||
),
|
||||
"new" => (&["--public"], &[]),
|
||||
"new" => (&["--public"], &["--open"]),
|
||||
"open" => (&[], &["--with"]),
|
||||
"group" => (
|
||||
&["--replace", "--no-materialize"],
|
||||
&[
|
||||
"--primary",
|
||||
"--mirror-to",
|
||||
"--owner",
|
||||
"--public-primary",
|
||||
"--public-mirror-to",
|
||||
"--snapshots",
|
||||
],
|
||||
),
|
||||
"provider" => (&["--replace"], &["--kind", "--url", "--protocol"]),
|
||||
"resolve" => (&["--take-remote", "--keep-local"], &["--save-as"]),
|
||||
_ => (&[], &[]),
|
||||
}
|
||||
@@ -159,11 +206,22 @@ pub fn validate(args: &[String]) -> Result<(), String> {
|
||||
}
|
||||
} else if sub == "secrets" && i == 0 && !["init", "pull", "rekey"].contains(&a.as_str()) {
|
||||
return Err(format!("secrets {a:?} is not allowed from the GUI"));
|
||||
} else if (sub == "group" || sub == "provider")
|
||||
&& i == 0
|
||||
&& !["add", "remove"].contains(&a.as_str())
|
||||
{
|
||||
return Err(format!("{sub} {a:?} is not allowed from the GUI"));
|
||||
}
|
||||
}
|
||||
if sub == "secrets" && args.len() != 2 {
|
||||
return Err("secrets takes exactly one action".into());
|
||||
}
|
||||
if (sub == "group" || sub == "provider") && args.len() < 3 {
|
||||
return Err(format!("{sub} needs an action and a name"));
|
||||
}
|
||||
if sub == "open" && args.get(1).is_none_or(|p| p.starts_with('-')) {
|
||||
return Err("open needs a path first".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -192,6 +250,27 @@ pub fn check_migrate_path(
|
||||
}
|
||||
}
|
||||
|
||||
/// A directory the GUI may open in an editor: an existing directory inside the workspace root.
|
||||
pub fn check_open_path(cfg: &grove_core::config::Config, path: &Path) -> Result<PathBuf, String> {
|
||||
let canon = path
|
||||
.canonicalize()
|
||||
.map_err(|e| format!("{}: {e}", path.display()))?;
|
||||
if !canon.is_dir() {
|
||||
return Err(format!("{} is not a directory", path.display()));
|
||||
}
|
||||
let root = cfg.root_path();
|
||||
let root = root.canonicalize().unwrap_or(root);
|
||||
if canon.starts_with(&root) && canon != root {
|
||||
Ok(canon)
|
||||
} else {
|
||||
Err(format!(
|
||||
"{} is not inside {}",
|
||||
path.display(),
|
||||
root.display()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn grove_core_default_config() -> Option<PathBuf> {
|
||||
let base = std::env::var_os("XDG_CONFIG_HOME")
|
||||
.map(PathBuf::from)
|
||||
@@ -215,6 +294,48 @@ mod tests {
|
||||
assert!(v(&["resolve", "/p", "--save-as", "b", "--take-remote"]).is_ok());
|
||||
assert!(v(&["new", "g", "n", "--public"]).is_ok());
|
||||
assert!(v(&["secrets", "pull"]).is_ok());
|
||||
assert!(v(&["group", "list"]).is_err());
|
||||
assert!(v(&["group", "add"]).is_err());
|
||||
assert!(v(&["group", "add", "g", "--primary", "p", "--force"]).is_err());
|
||||
assert!(
|
||||
v(&[
|
||||
"group",
|
||||
"add",
|
||||
"g",
|
||||
"--primary",
|
||||
"p",
|
||||
"--mirror-to",
|
||||
"q",
|
||||
"--replace"
|
||||
])
|
||||
.is_ok()
|
||||
);
|
||||
assert!(v(&["provider", "remove", "p"]).is_ok());
|
||||
assert!(v(&["provider", "add", "p", "--url", "--kind"]).is_err());
|
||||
assert!(v(&["open", "/p", "--with", "zed"]).is_ok());
|
||||
assert!(v(&["open", "/p", "--print"]).is_err());
|
||||
assert!(v(&["open", "--with", "zed"]).is_err());
|
||||
assert!(v(&["new", "g", "n", "--open", "zed"]).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_path_must_be_inside_the_root() {
|
||||
let tmp = crate::test_tmp::TempDir::new("grove-open");
|
||||
let base = tmp.path().canonicalize().unwrap();
|
||||
std::fs::create_dir_all(base.join("ws/work/app/main")).unwrap();
|
||||
std::fs::create_dir_all(base.join("elsewhere")).unwrap();
|
||||
let cfg = grove_core::config::Config::parse(&format!(
|
||||
"[machine]\nname=\"m\"\n[root]\npath=\"{}/ws\"\n",
|
||||
base.display()
|
||||
))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
check_open_path(&cfg, &base.join("ws/work/app/../app/main")).unwrap(),
|
||||
base.join("ws/work/app/main")
|
||||
);
|
||||
for bad in ["ws", "elsewhere", "ws/work/../../elsewhere", "ws/none"] {
|
||||
assert!(check_open_path(&cfg, &base.join(bad)).is_err(), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -47,6 +47,8 @@ struct Snapshot {
|
||||
status: Option<Value>,
|
||||
log: Vec<Value>,
|
||||
config: Option<Value>,
|
||||
/// Installed launchers (name, label), the default one first.
|
||||
launchers: Vec<(String, String)>,
|
||||
alerts: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
@@ -141,6 +143,7 @@ struct UiState {
|
||||
toast_gen: u32,
|
||||
last_groups: Vec<String>,
|
||||
last_providers: Vec<String>,
|
||||
last_launchers: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
@@ -161,6 +164,7 @@ thread_local! {
|
||||
import_current: String::new(),
|
||||
toast_gen: 0,
|
||||
last_groups: Vec::new(),
|
||||
last_launchers: Vec::new(),
|
||||
last_providers: Vec::new(),
|
||||
});
|
||||
}
|
||||
@@ -428,6 +432,9 @@ fn render(ui: &MainWindow) {
|
||||
}
|
||||
if providers != st.last_providers {
|
||||
ui.set_providers(str_model(&providers));
|
||||
let mut mirrors = vec!["No mirror".to_string()];
|
||||
mirrors.extend(providers.iter().cloned());
|
||||
ui.set_mirror_choices(str_model(&mirrors));
|
||||
st.last_providers = providers;
|
||||
}
|
||||
let secs: Vec<Section> = model::settings_sections(cfg)
|
||||
@@ -446,6 +453,21 @@ fn render(ui: &MainWindow) {
|
||||
.collect();
|
||||
ui.set_sections(ModelRc::from(Rc::new(VecModel::from(secs))));
|
||||
}
|
||||
if snap.launchers != st.last_launchers {
|
||||
let rows: Vec<Launch> = snap
|
||||
.launchers
|
||||
.iter()
|
||||
.map(|(n, l)| Launch {
|
||||
name: n.into(),
|
||||
label: l.into(),
|
||||
})
|
||||
.collect();
|
||||
ui.set_launchers(ModelRc::from(Rc::new(VecModel::from(rows))));
|
||||
let mut choices = vec!["Don't open".to_string()];
|
||||
choices.extend(snap.launchers.iter().map(|(_, l)| format!("Open in {l}")));
|
||||
ui.set_open_choices(str_model(&choices));
|
||||
st.last_launchers = snap.launchers.clone();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -539,6 +561,15 @@ fn run_cli(
|
||||
});
|
||||
}
|
||||
|
||||
/// A config-editing command from Settings: output below the forms, a toast either way.
|
||||
fn run_settings_cli(ctx: &Ctx, ui: &MainWindow, args: Vec<String>, done_msg: &'static str) {
|
||||
run_cli(ctx, ui, None, args, String::new(), move |ui, ok, text| {
|
||||
let first = text.lines().next().unwrap_or("").to_string();
|
||||
toast(ui, if ok { done_msg } else { &first }, !ok);
|
||||
ui.set_settings_out(out(ok, text));
|
||||
});
|
||||
}
|
||||
|
||||
fn out(ok: bool, text: String) -> Out {
|
||||
Out {
|
||||
ok,
|
||||
@@ -743,13 +774,23 @@ fn wire(ui: &MainWindow, ctx: &Ctx) {
|
||||
|
||||
ui.on_create_repo({
|
||||
let c = ctx.clone();
|
||||
move |group, name, public| {
|
||||
move |group, name, public, open| {
|
||||
let Some(ui) = c.weak.upgrade() else { return };
|
||||
let group = model::group_name(&first_or(group.as_str(), &ui.get_groups())).to_string();
|
||||
let mut args = vec!["new".to_string(), group, name.trim().to_string()];
|
||||
if public {
|
||||
args.push("--public".into());
|
||||
}
|
||||
let launcher = ST.with(|st| {
|
||||
st.borrow()
|
||||
.last_launchers
|
||||
.iter()
|
||||
.find(|(_, l)| format!("Open in {l}") == open.as_str())
|
||||
.map(|(n, _)| n.clone())
|
||||
});
|
||||
if let Some(n) = launcher {
|
||||
args.extend(["--open".into(), n]);
|
||||
}
|
||||
ui.set_new_out(out(true, String::new()));
|
||||
run_cli(&c, &ui, None, args, String::new(), |ui, ok, text| {
|
||||
ui.set_new_out(out(ok, text));
|
||||
@@ -760,6 +801,23 @@ fn wire(ui: &MainWindow, ctx: &Ctx) {
|
||||
}
|
||||
});
|
||||
|
||||
ui.on_open_in({
|
||||
let c = ctx.clone();
|
||||
move |path, launcher| {
|
||||
let Some(ui) = c.weak.upgrade() else { return };
|
||||
let args = vec![
|
||||
"open".to_string(),
|
||||
path.to_string(),
|
||||
"--with".into(),
|
||||
launcher.to_string(),
|
||||
];
|
||||
run_cli(&c, &ui, None, args, String::new(), |ui, ok, text| {
|
||||
let first = text.lines().last().unwrap_or("").to_string();
|
||||
toast(ui, &first, !ok);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
ui.on_scan_import({
|
||||
let c = ctx.clone();
|
||||
move |group, force| {
|
||||
@@ -973,6 +1031,63 @@ fn wire(ui: &MainWindow, ctx: &Ctx) {
|
||||
});
|
||||
}
|
||||
});
|
||||
ui.on_add_provider({
|
||||
let c = ctx.clone();
|
||||
move |id, kind, url, protocol, replace| {
|
||||
let Some(ui) = c.weak.upgrade() else { return };
|
||||
let mut args: Vec<String> = vec![
|
||||
"provider".into(),
|
||||
"add".into(),
|
||||
id.trim().to_string(),
|
||||
"--kind".into(),
|
||||
kind.to_string(),
|
||||
"--protocol".into(),
|
||||
protocol.to_string(),
|
||||
];
|
||||
if !url.trim().is_empty() {
|
||||
args.extend(["--url".into(), url.trim().to_string()]);
|
||||
}
|
||||
if replace {
|
||||
args.push("--replace".into());
|
||||
}
|
||||
run_settings_cli(&c, &ui, args, "Provider saved");
|
||||
}
|
||||
});
|
||||
ui.on_remove_provider({
|
||||
let c = ctx.clone();
|
||||
move |id| {
|
||||
let Some(ui) = c.weak.upgrade() else { return };
|
||||
let args = vec!["provider".into(), "remove".into(), id.to_string()];
|
||||
run_settings_cli(&c, &ui, args, "Provider removed");
|
||||
}
|
||||
});
|
||||
ui.on_add_group({
|
||||
let c = ctx.clone();
|
||||
move |name, primary, mirror, owner, mirror_owner, snapshots, replace| {
|
||||
let Some(ui) = c.weak.upgrade() else { return };
|
||||
let primary = first_or(primary.as_str(), &ui.get_providers());
|
||||
let args = model::group_add_args(&model::GroupForm {
|
||||
name: name.to_string(),
|
||||
primary,
|
||||
mirror: mirror.to_string(),
|
||||
owner: owner.to_string(),
|
||||
mirror_owner: mirror_owner.to_string(),
|
||||
snapshots: snapshots.to_string(),
|
||||
replace,
|
||||
});
|
||||
run_settings_cli(&c, &ui, args, "Namespace saved");
|
||||
}
|
||||
});
|
||||
ui.on_remove_group({
|
||||
let c = ctx.clone();
|
||||
move |label| {
|
||||
let Some(ui) = c.weak.upgrade() else { return };
|
||||
let name = model::group_name(&first_or(label.as_str(), &ui.get_groups())).to_string();
|
||||
let args = vec!["group".into(), "remove".into(), name];
|
||||
run_settings_cli(&c, &ui, args, "Namespace removed");
|
||||
}
|
||||
});
|
||||
|
||||
ui.on_login({
|
||||
let c = ctx.clone();
|
||||
move |provider, token| {
|
||||
@@ -1200,6 +1315,7 @@ fn worker(
|
||||
status,
|
||||
log,
|
||||
config: core.bridge.config_json(),
|
||||
launchers: core.bridge.launchers(),
|
||||
alerts,
|
||||
};
|
||||
let w = weak.clone();
|
||||
|
||||
@@ -336,6 +336,53 @@ pub fn group_name(entry: &str) -> &str {
|
||||
entry.split(" → ").next().unwrap_or(entry)
|
||||
}
|
||||
|
||||
/// What the "Add or change a namespace" form holds.
|
||||
pub struct GroupForm {
|
||||
pub name: String,
|
||||
pub primary: String,
|
||||
/// Empty: no mirror.
|
||||
pub mirror: String,
|
||||
pub owner: String,
|
||||
pub mirror_owner: String,
|
||||
/// "default" leaves the global policy in force.
|
||||
pub snapshots: String,
|
||||
pub replace: bool,
|
||||
}
|
||||
|
||||
/// `grove group add ...` for the namespace form.
|
||||
pub fn group_add_args(f: &GroupForm) -> Vec<String> {
|
||||
let mut a: Vec<String> = vec![
|
||||
"group".into(),
|
||||
"add".into(),
|
||||
f.name.trim().to_string(),
|
||||
"--primary".into(),
|
||||
f.primary.clone(),
|
||||
];
|
||||
let mirror = f.mirror.trim();
|
||||
if !mirror.is_empty() {
|
||||
a.extend(["--mirror-to".into(), mirror.to_string()]);
|
||||
}
|
||||
if !f.owner.trim().is_empty() {
|
||||
a.extend([
|
||||
"--owner".into(),
|
||||
format!("{}={}", f.primary, f.owner.trim()),
|
||||
]);
|
||||
}
|
||||
if !mirror.is_empty() && !f.mirror_owner.trim().is_empty() {
|
||||
a.extend([
|
||||
"--owner".into(),
|
||||
format!("{mirror}={}", f.mirror_owner.trim()),
|
||||
]);
|
||||
}
|
||||
if !f.snapshots.is_empty() && f.snapshots != "default" {
|
||||
a.extend(["--snapshots".into(), f.snapshots.clone()]);
|
||||
}
|
||||
if f.replace {
|
||||
a.push("--replace".into());
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -501,4 +548,46 @@ mod tests {
|
||||
assert_eq!(s[3].1[0].1, "grove-config on gitea");
|
||||
assert_eq!(group_name("work → gitea"), "work");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn group_form_becomes_group_add_args() {
|
||||
let mut f = GroupForm {
|
||||
name: " oss ".into(),
|
||||
primary: "gh".into(),
|
||||
mirror: String::new(),
|
||||
owner: String::new(),
|
||||
mirror_owner: "ignored without a mirror".into(),
|
||||
snapshots: "default".into(),
|
||||
replace: false,
|
||||
};
|
||||
assert_eq!(
|
||||
group_add_args(&f),
|
||||
["group", "add", "oss", "--primary", "gh"]
|
||||
);
|
||||
f.mirror = "gitea".into();
|
||||
f.owner = "Zenvairo".into();
|
||||
f.mirror_owner = "Org".into();
|
||||
f.snapshots = "none".into();
|
||||
f.replace = true;
|
||||
assert_eq!(
|
||||
group_add_args(&f),
|
||||
[
|
||||
"group",
|
||||
"add",
|
||||
"oss",
|
||||
"--primary",
|
||||
"gh",
|
||||
"--mirror-to",
|
||||
"gitea",
|
||||
"--owner",
|
||||
"gh=Zenvairo",
|
||||
"--owner",
|
||||
"gitea=Org",
|
||||
"--snapshots",
|
||||
"none",
|
||||
"--replace"
|
||||
]
|
||||
);
|
||||
assert!(crate::bridge::validate(&group_add_args(&f)).is_ok());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user