Merge pull request #495 from runkids/runkids/extras-validate-names

refactor(extras): validate every mode change and share special extra names
This commit is contained in:
Willie
2026-10-08 16:16:52 +08:00
committed by GitHub
9 changed files with 67 additions and 26 deletions
+6 -2
View File
@@ -1,6 +1,10 @@
package main
import "fmt"
import (
"fmt"
"skillshare/internal/config"
)
func cmdExtras(args []string) error {
if len(args) == 0 {
@@ -35,7 +39,7 @@ func cmdExtras(args []string) error {
return cmdExtrasCollect(rest)
case "source":
return cmdExtrasSource(rest)
case "memory":
case config.MemoryExtraName:
return cmdExtrasMemory(rest)
case "--help", "-h":
printExtrasHelp()
+3 -6
View File
@@ -181,12 +181,9 @@ func cmdExtrasMode(args []string) error {
return filterErr
}
}
// Filters rule out single files and symlink mode, so check them whenever
// either side changes.
if _, updated := findExtraByName(extras, name); updated.File != "" || config.ManagedExtraMode(syncMode) || syncMode == "symlink" || filters.hasUpdates() {
if err := config.ValidateExtraConfig(updated); err != nil {
return err
}
_, updated := findExtraByName(extras, name)
if err := config.ValidateExtraConfig(updated); err != nil {
return err
}
if err := validateFn(); err != nil {
+2 -5
View File
@@ -14,9 +14,6 @@ import (
"skillshare/internal/ui"
)
// extrasAgentsName is the extras entry name that may overlap with the agents sync system.
const extrasAgentsName = "agents"
type syncExtrasJSONOutput struct {
Extras []syncExtrasJSONEntry `json:"extras"`
Duration string `json:"duration"`
@@ -110,7 +107,7 @@ func cmdSyncExtrasGlobal(dryRun, force, jsonOutput, partOfAll bool, start time.T
// Detect overlap between extras "agents" and the agents sync system
var agentTargetPaths map[string]bool
for _, extra := range cfg.Extras {
if extra.Name == extrasAgentsName {
if extra.Name == config.AgentsExtraName {
agentTargetPaths = collectAgentTargetPathsGlobal(cfg)
break
}
@@ -220,7 +217,7 @@ func cmdSyncExtrasProject(cwd string, dryRun, force, jsonOutput, partOfAll bool,
// Detect overlap between extras "agents" and the agents sync system
var agentTargetPaths map[string]bool
for _, extra := range projCfg.Extras {
if extra.Name == extrasAgentsName {
if extra.Name == config.AgentsExtraName {
agentTargetPaths = collectAgentTargetPathsProject(cwd)
break
}
+19
View File
@@ -79,6 +79,25 @@ func ValidateExtraNameUnique(name string, existing []ExtraConfig) error {
return nil
}
// Extras with these names get special handling: sync skips agent target
// folders for the extra named agents, and the memory notes live in the extra
// named memory.
const (
AgentsExtraName = "agents"
MemoryExtraName = "memory"
)
// ValidateExtraRename rejects renaming an extra to or from a name with
// special handling, ignoring case.
func ValidateExtraRename(oldName, newName string) error {
for _, special := range []string{AgentsExtraName, MemoryExtraName} {
if strings.EqualFold(oldName, special) || strings.EqualFold(newName, special) {
return fmt.Errorf("an extra cannot be renamed to or from %s", special)
}
}
return nil
}
// ValidateExtraConfig checks an extra's single-file settings and target modes.
// file and as must be plain filenames; as, import mode, and the absence of
// flatten are tied to file; an extension cannot transform a single file.
+2 -2
View File
@@ -36,14 +36,14 @@ type Note struct {
// Extra reuses the existing named source, including its per-extra override.
func Extra(extras []config.ExtraConfig) (config.ExtraConfig, bool, error) {
for _, extra := range extras {
if strings.EqualFold(extra.Name, "memory") {
if strings.EqualFold(extra.Name, config.MemoryExtraName) {
if extra.File != "" {
return extra, true, fmt.Errorf("extra %q must be a folder to manage memory notes", extra.Name)
}
return extra, true, nil
}
}
return config.ExtraConfig{Name: "memory"}, false, nil
return config.ExtraConfig{Name: config.MemoryExtraName}, false, nil
}
func GlobalRoot(cfg *config.Config) (string, error) {
+1 -1
View File
@@ -697,7 +697,7 @@ func (s *Server) syncExtras(name string, dryRun, force bool) []extraSyncResult {
ResolveExtension: s.resolveExtensionSpec,
}
for _, extra := range s.extrasConfig() {
if extra.Name == "agents" {
if extra.Name == config.AgentsExtraName {
opts.AgentTargetPaths = s.extrasAgentTargetPaths()
break
}
+3 -8
View File
@@ -7,7 +7,6 @@ import (
"os"
"path/filepath"
"slices"
"strings"
"time"
"skillshare/internal/config"
@@ -199,13 +198,9 @@ func (s *Server) handleExtrasEdit(w http.ResponseWriter, r *http.Request) {
next := extra
if body.Name != "" && body.Name != name {
// Sync skips agent target folders for the extra named agents, and the
// memory notes live in the extra named memory.
for _, special := range []string{"agents", "memory"} {
if strings.EqualFold(name, special) || strings.EqualFold(body.Name, special) {
writeError(w, http.StatusBadRequest, "an extra cannot be renamed to or from "+special)
return
}
if err := config.ValidateExtraRename(name, body.Name); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
if err := config.ValidateExtraName(body.Name); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
+2 -2
View File
@@ -65,8 +65,8 @@ func RunExtraTargets(extra config.ExtraConfig, sourceDir string, opts ExtraRunOp
func runExtraTarget(extra config.ExtraConfig, t config.ExtraTargetConfig, sourceDir string, opts ExtraRunOptions) ExtraTargetRun {
tr := ExtraTargetRun{Target: t, Path: opts.ResolvePath(t.Path), Mode: EffectiveMode(t.Mode)}
if extra.Name == "agents" && opts.AgentTargetPaths[filepath.Clean(tr.Path)] {
tr.SkippedBy = "agents"
if extra.Name == config.AgentsExtraName && opts.AgentTargetPaths[filepath.Clean(tr.Path)] {
tr.SkippedBy = config.AgentsExtraName
return tr
}
+29
View File
@@ -901,6 +901,35 @@ extras:
}
}
// TestExtras_Mode_FlattenFolderExtra verifies that a flatten-only change on a
// plain folder extra passes the full extra validation.
func TestExtras_Mode_FlattenFolderExtra(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
rulesTarget := filepath.Join(sb.Home, ".claude", "rules")
os.MkdirAll(rulesTarget, 0755)
claudeTarget := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + claudeTarget + `
extras:
- name: rules
targets:
- path: ` + rulesTarget + `
mode: copy
`)
result := sb.RunCLI("extras", "rules", "--flatten", "-g")
result.AssertSuccess(t)
if configContent := sb.ReadFile(sb.ConfigPath); !strings.Contains(configContent, "flatten: true") {
t.Errorf("expected config to contain 'flatten: true', got:\n%s", configContent)
}
}
// TestExtras_Mode_MultipleTargets_NoTarget verifies that "extras <name> --mode" errors
// when the extra has multiple targets and --target is not specified.
func TestExtras_Mode_MultipleTargets_NoTarget(t *testing.T) {