fix(hub): apply the source follow policy to local discovery

Hub index, built-in search, and draft candidates discovered only the source path, dropping the follow policy used by the inventory. Add optional walk options to the shared Hub functions and pass the operation policy from dashboard handlers and CLI indexing.

Explicit CLI sources use the active config policy while retaining configuration-free indexing. Cover global/project modes, enabled/disabled following, all three handlers, and CLI source overrides; document the behavior in all website locales and built-in guidance.
This commit is contained in:
Willie
2026-10-05 22:03:47 +08:00
parent 1254fe522d
commit 113d410da7
14 changed files with 218 additions and 25 deletions
+24 -15
View File
@@ -8,6 +8,7 @@ import (
"skillshare/internal/config"
"skillshare/internal/hub"
"skillshare/internal/sourcewalk"
"skillshare/internal/ui"
)
@@ -149,13 +150,10 @@ func cmdHubIndex(args []string) error {
i++
}
// Resolve source path
if sourcePath == "" {
resolved, err := resolveSourcePath(mode, cwd)
if err != nil {
return err
}
sourcePath = resolved
// Resolve the source and its operation policy, including an explicit source override.
sourcePath, walk, err := resolveHubSource(mode, cwd, sourcePath)
if err != nil {
return err
}
// Resolve output path
@@ -165,7 +163,7 @@ func cmdHubIndex(args []string) error {
start := time.Now()
sp := ui.StartSpinner("Scanning source directory...")
idx, err := hub.BuildIndex(sourcePath, full, auditSkills)
idx, err := hub.BuildIndex(sourcePath, full, auditSkills, walk)
sp.Stop()
if err != nil {
return fmt.Errorf("failed to build index: %w", err)
@@ -191,21 +189,32 @@ func cmdHubIndex(args []string) error {
return nil
}
// resolveSourcePath determines the source directory based on mode.
func resolveSourcePath(mode runMode, cwd string) (string, error) {
// resolveHubSource uses the active config's policy, retaining config-free --source use.
func resolveHubSource(mode runMode, cwd, sourcePath string) (string, sourcewalk.Options, error) {
if mode == modeProject {
rt, err := loadProjectRuntime(cwd)
if err != nil {
return "", fmt.Errorf("failed to load project config: %w", err)
if sourcePath != "" {
return sourcePath, sourcewalk.Options{}, nil
}
return "", sourcewalk.Options{}, fmt.Errorf("failed to load project config: %w", err)
}
return rt.sourcePath, nil
if sourcePath == "" {
sourcePath = rt.sourcePath
}
return sourcePath, config.SkillsWalk(rt.config.FollowSourceLinks, sourcePath, rt.targets), nil
}
cfg, err := config.Load()
if err != nil {
return "", fmt.Errorf("failed to load config: %w", err)
if sourcePath != "" {
return sourcePath, sourcewalk.Options{}, nil
}
return "", sourcewalk.Options{}, fmt.Errorf("failed to load config: %w", err)
}
return cfg.EffectiveSkillsSource(), nil
if sourcePath == "" {
sourcePath = cfg.EffectiveSkillsSource()
}
return sourcePath, config.SkillsWalk(cfg.FollowSourceLinks, sourcePath, cfg.Targets), nil
}
func printHubHelp() {
+94 -1
View File
@@ -1,6 +1,15 @@
package main
import "testing"
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"skillshare/internal/config"
"skillshare/internal/hub"
)
func TestLooksLikeURLOrPath_SSH(t *testing.T) {
cases := map[string]bool{
@@ -18,3 +27,87 @@ func TestLooksLikeURLOrPath_SSH(t *testing.T) {
}
}
}
func TestHubIndexFollowedSourceLinks(t *testing.T) {
for _, project := range []bool{false, true} {
for _, follow := range []bool{false, true} {
for _, explicit := range []bool{false, true} {
t.Run(fmt.Sprintf("project=%t/follow=%t/explicit=%t", project, follow, explicit), func(t *testing.T) {
root := t.TempDir()
t.Chdir(root)
t.Setenv("SKILLSHARE_CONFIG", filepath.Join(root, "config.yaml"))
source := filepath.Join(root, "skills")
cfg := &config.Config{Source: source, FollowSourceLinks: follow}
if err := cfg.Save(); err != nil {
t.Fatal(err)
}
if project {
cfg := &config.ProjectConfig{Sources: config.ProjectSources{Skills: source}, FollowSourceLinks: follow}
if err := cfg.Save(root); err != nil {
t.Fatal(err)
}
}
if explicit {
source = filepath.Join(root, "custom-source")
}
checkout := t.TempDir()
for _, dir := range []string{source, filepath.Join(checkout, "linked-skill")} {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
}
if err := os.WriteFile(filepath.Join(checkout, "linked-skill", "SKILL.md"), []byte("# Linked skill\n"), 0644); err != nil {
t.Fatal(err)
}
if err := os.Symlink(checkout, filepath.Join(source, "linked")); err != nil {
t.Fatal(err)
}
output := filepath.Join(root, "index.json")
args := []string{"-g", "-o", output}
if project {
args[0] = "-p"
}
if explicit {
args = append(args, "--source", source)
}
captureStdoutStderr(t, func() {
if err := cmdHubIndex(args); err != nil {
t.Errorf("hub index: %v", err)
}
})
data, err := os.ReadFile(output)
if err != nil {
t.Fatal(err)
}
var index hub.Index
if err := json.Unmarshal(data, &index); err != nil {
t.Fatal(err)
}
want := 0
if follow {
want = 1
}
if len(index.Skills) != want || (follow && index.Skills[0].Name != "linked-skill") {
t.Fatalf("follow=%t index=%s", follow, data)
}
})
}
}
}
}
func TestHubIndexExplicitSourceWithoutConfig(t *testing.T) {
root := t.TempDir()
t.Setenv("SKILLSHARE_CONFIG", filepath.Join(root, "missing.yaml"))
if err := os.WriteFile(filepath.Join(root, "SKILL.md"), []byte("# Skill\n"), 0644); err != nil {
t.Fatal(err)
}
captureStdoutStderr(t, func() {
if err := cmdHubIndex([]string{"-g", "--source", root}); err != nil {
t.Errorf("explicit source requires config: %v", err)
}
})
if _, err := os.Stat(filepath.Join(root, "skillshare-hub.json")); err != nil {
t.Fatal(err)
}
}