Files
skillshare/internal/projectdir/projectdir.go
T
Rusty ShacklefordandWillie 60e19f96d5 feat(project): support a visible skillshare/ project directory (#256) (#258)
* fix(project): refuse to re-init a project that lost its config.yaml

Every project-mode command auto-initializes when .skillshare/config.yaml
is missing. That is right for a repository with no project directory at
all, and for the shared skills repo produced by 'init -p --config local',
which gitignores config.yaml so each developer regenerates their own.

It was wrong for the case in between. A .skillshare/ that already held
skills or agents, whose config.yaml was simply gone, was re-initialized
into an empty config. Every configured target was dropped, the target
symlinks were left behind pointing at a project the config no longer
described, and the command exited 0. Nothing said anything had happened.

Add ensureProjectConfig, which replaces the open-coded auto-init at every
project command entry point. It initializes as before when there is no
project directory, and when config.yaml is gitignored, so both intentional
cases keep working untouched. When the directory holds skills or agents
and its config is neither present nor gitignored, it now returns an error
pointing at version control or an explicit 'skillshare init -p' instead of
writing over the project.

The gitignore check is treated as inconclusive on error, so a repository
that cannot be inspected is guarded rather than overwritten.

* feat(project): support a visible skillshare/ project directory

Project mode is identified only by .skillshare/config.yaml, and trash,
backups, logs and audit-rules.yaml anchor to that same hidden directory.
The sources option (#153) made project content relocatable, but the
marker and operational state stay hidden, so repositories that treat
skills as reviewable content still carry a hidden directory whose only
job is to hold config.yaml.

Resolve the project directory through a new leaf package that checks
.skillshare/config.yaml first and skillshare/config.yaml second, and
derive every config, state, source-default and gitignore path from the
result. The hidden name wins whenever both exist, so an existing project
never moves and no migration is needed. The package imports nothing else
internal because internal/audit cannot import internal/config without a
cycle.

Add init -p --visible to create the visible layout. Default init -p
behaviour and output are unchanged.

Two ambiguities the visible name introduces:

- The global config directory is also called skillshare
  (<config-home>/skillshare), so the two places that infer scope from a
  config path now exclude the global location by full path rather than
  by directory name. TestLogDir_Global asserted a fixture path without
  declaring a config home, which hid this; it now sets XDG_CONFIG_HOME.
- An unrelated directory named skillshare/ must not be adopted as a
  partially initialized project, so the visible name is only claimed
  when it carries a marker of its own: a skills/ or agents/ directory,
  or a .gitignore that ignores config.yaml.

The missing-config guard added in the previous commit is generalized to
resolve through the new package, so it covers both layouts. The
gitignored-config marker matters there too. A repository shared by
'init -p --visible --config local' before any skill was committed clones
with nothing but that .gitignore, because git drops the empty skills/
and agents/ directories; recognizing it keeps the repair pointed at the
visible directory instead of writing a hidden one beside it.

Refs #256

* fix(project): reconcile visible directory support with main

Consolidate the missing-config guard and regression tests already merged in #260. Preserve both directory layouts and document and test resyncing target links after a rename. Follow-up for #258.

---------

Co-authored-by: Willie <egg358358@gmail.com>
2026-09-05 12:07:00 +08:00

146 lines
4.6 KiB
Go

// Package projectdir resolves the directory that marks a skillshare project.
//
// A project root is identified by a config.yaml inside a recognized directory.
// ".skillshare" is the default. "skillshare" is an alternative for repositories
// that treat skills as reviewable content rather than tool state.
//
// The hidden name is always resolved first, so a repository that already has
// .skillshare/ keeps working unchanged even if a visible directory also exists.
//
// This package has no dependencies on other internal packages so that every
// layer (config, audit, trash, backup, oplog, CLI, server) can resolve project
// paths the same way.
package projectdir
import (
"bufio"
"os"
"path/filepath"
"strings"
)
const (
// Default is the directory name used by "skillshare init -p".
Default = ".skillshare"
// Visible is the directory name used by "skillshare init -p --visible".
Visible = "skillshare"
// ConfigFileName is the file whose presence marks a project directory.
ConfigFileName = "config.yaml"
)
// names lists recognized project directory names in resolution order.
var names = []string{Default, Visible}
// Names returns the recognized project directory names in resolution order.
func Names() []string {
return append([]string(nil), names...)
}
// IsName reports whether name is a recognized project directory name.
func IsName(name string) bool {
for _, candidate := range names {
if name == candidate {
return true
}
}
return false
}
// Find returns the absolute path of the project directory under projectRoot
// that holds a config.yaml. The second return value is false when projectRoot
// is not an initialized project.
func Find(projectRoot string) (string, bool) {
for _, name := range names {
dir := filepath.Join(projectRoot, name)
if info, err := os.Stat(filepath.Join(dir, ConfigFileName)); err == nil && !info.IsDir() {
return dir, true
}
}
return "", false
}
// FindPartial returns a project directory that exists but holds no config.yaml,
// which is how a shared skills repository looks after a clone that gitignored
// the config. Only the hidden default is accepted on name alone; the visible
// name must also carry a marker of its own, so an unrelated directory called
// "skillshare" is never mistaken for a project.
//
// Two markers count: a skills/ or agents/ directory, and a .gitignore that
// ignores config.yaml. The second is needed because a repository shared before
// any skill was committed clones with nothing else — git carries the .gitignore
// but drops the empty skills/ and agents/ directories.
func FindPartial(projectRoot string) (string, bool) {
for _, name := range names {
dir := filepath.Join(projectRoot, name)
info, err := os.Stat(dir)
if err != nil || !info.IsDir() {
continue
}
if name == Default || hasResourceDir(dir) || hasGitignoredConfig(dir) {
return dir, true
}
}
return "", false
}
func hasResourceDir(dir string) bool {
for _, name := range []string{"skills", "agents"} {
if info, err := os.Stat(filepath.Join(dir, name)); err == nil && info.IsDir() {
return true
}
}
return false
}
// hasGitignoredConfig reports whether dir has a .gitignore listing config.yaml,
// the marker written by "init -p --config local".
//
// This repeats what install.GitignoreContains does for that one pattern rather
// than calling it, because this package must not depend on other internal
// packages. An unreadable .gitignore is treated as absent: the marker only ever
// widens what counts as a project, so failing to read it leaves the directory
// unclaimed rather than claiming it wrongly.
func hasGitignoredConfig(dir string) bool {
f, err := os.Open(filepath.Join(dir, ".gitignore"))
if err != nil {
return false
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.TrimSpace(scanner.Text()) == ConfigFileName {
return true
}
}
return false
}
// Resolve returns the project directory to use for projectRoot. An initialized
// project resolves to its own directory; anything else resolves to the default
// name so new projects keep the current layout.
func Resolve(projectRoot string) string {
if dir, ok := Find(projectRoot); ok {
return dir
}
if dir, ok := FindPartial(projectRoot); ok {
return dir
}
return filepath.Join(projectRoot, Default)
}
// ConfigPath returns the project config path for projectRoot.
func ConfigPath(projectRoot string) string {
return filepath.Join(Resolve(projectRoot), ConfigFileName)
}
// Name returns the directory name to create for a new project.
func Name(visible bool) string {
if visible {
return Visible
}
return Default
}