Files
skillshare/internal/skillignore/write.go
T
Willie bf1731dec4 feat(move): move installed skills and folders without losing their records (#520)
Add skillshare move and POST /api/resources/batch/move so skills and
whole folders can change folder inside the source while keeping their
.metadata.json entry, accepted audit findings, lockfile pin, config
group, .gitignore and literal .skillignore lines. Moving by hand lost
the record and made the next install start over. install --into now
refuses a folder inside a skill or a tracked checkout.

Refs #510
2026-10-11 00:43:47 +08:00

104 lines
2.7 KiB
Go

package skillignore
import (
"os"
"path/filepath"
"strings"
)
// AddPattern appends a pattern to a .skillignore file.
// Creates the file (and parent dirs) if it doesn't exist.
// Returns true if the pattern was added, false if it already existed.
func AddPattern(filePath, pattern string) (bool, error) {
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
return false, err
}
existing, _ := os.ReadFile(filePath)
content := string(existing)
// Check for duplicate in a single pass
for _, line := range strings.Split(content, "\n") {
if strings.TrimRight(line, " \t\r") == pattern {
return false, nil
}
}
if len(content) > 0 && !strings.HasSuffix(content, "\n") {
content += "\n"
}
content += pattern + "\n"
return true, os.WriteFile(filePath, []byte(content), 0644)
}
// RemovePattern removes all lines matching the exact pattern from a .skillignore file.
// Returns true if the pattern was found and removed, false if not found.
func RemovePattern(filePath, pattern string) (bool, error) {
data, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
lines := strings.Split(string(data), "\n")
var kept []string
found := false
for _, line := range lines {
trimmed := strings.TrimRight(line, " \t\r")
if trimmed == pattern {
found = true
continue
}
kept = append(kept, line)
}
if !found {
return false, nil
}
result := strings.Join(kept, "\n")
for strings.HasSuffix(result, "\n\n") {
result = strings.TrimSuffix(result, "\n")
}
return true, os.WriteFile(filePath, []byte(result), 0644)
}
// RenamePatterns rewrites every line that is exactly a key of renames, or a
// "!" negation of one, to its value and reports whether any changed. Lines keep
// their place: with negations the last matching rule wins, so a removed and
// re-added line would change what the file means.
func RenamePatterns(content string, renames map[string]string) (string, bool) {
lines := strings.Split(content, "\n")
changed := false
for i, line := range lines {
pattern := strings.TrimRight(line, " \t\r")
negation := strings.HasPrefix(pattern, "!")
if to, ok := renames[strings.TrimPrefix(pattern, "!")]; ok {
if negation {
to = "!" + to
}
lines[i], changed = to, true
}
}
return strings.Join(lines, "\n"), changed
}
// HasPattern returns true if the exact pattern exists in a .skillignore file.
func HasPattern(filePath, pattern string) bool {
data, err := os.ReadFile(filePath)
if err != nil {
return false
}
for _, line := range strings.Split(string(data), "\n") {
if strings.TrimRight(line, " \t\r") == pattern {
return true
}
}
return false
}