mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Flat target naming turns a source skill under a dot directory (e.g. .system/example) into a hidden target entry (.system__example). Target scans skipped every hidden name, so diff, doctor, status and the dashboard reported these synced skills as missing, and prune never removed them after the source skill was deleted. Hidden entries are now skipped only when the target manifest does not record them as managed, so files like .skillshare-manifest.json or .DS_Store are still ignored. Refs #294
82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package sync
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// ManifestFile is the filename for the sync manifest.
|
|
const ManifestFile = ".skillshare-manifest.json"
|
|
|
|
// Manifest tracks which skills are managed by skillshare in a target directory.
|
|
// Used by both merge mode (values: "symlink") and copy mode (values: SHA-256 checksum).
|
|
type Manifest struct {
|
|
Managed map[string]string `json:"managed"` // flatName → "symlink" (merge) or SHA-256 checksum (copy)
|
|
Mtimes map[string]int64 `json:"mtimes,omitempty"` // flatName → source dir max mtime (UnixNano), copy mode only
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// ReadManifest reads the manifest from a target directory.
|
|
// Returns an empty manifest if the file does not exist.
|
|
func ReadManifest(targetPath string) (*Manifest, error) {
|
|
p := filepath.Join(targetPath, ManifestFile)
|
|
data, err := os.ReadFile(p)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return &Manifest{Managed: make(map[string]string), Mtimes: make(map[string]int64)}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
var m Manifest
|
|
if err := json.Unmarshal(data, &m); err != nil {
|
|
// Corrupt manifest — treat as empty so next sync rebuilds it.
|
|
return &Manifest{Managed: make(map[string]string), Mtimes: make(map[string]int64)}, nil
|
|
}
|
|
if m.Managed == nil {
|
|
m.Managed = make(map[string]string)
|
|
}
|
|
if m.Mtimes == nil {
|
|
m.Mtimes = make(map[string]int64)
|
|
}
|
|
return &m, nil
|
|
}
|
|
|
|
// SkipsHidden reports whether a target scan should skip name. Hidden entries
|
|
// are skipped unless the manifest records them as managed (e.g. a source skill
|
|
// under ".system/" synced as ".system__example").
|
|
func (m *Manifest) SkipsHidden(name string) bool {
|
|
if !utils.IsHidden(name) {
|
|
return false
|
|
}
|
|
if m == nil {
|
|
return true
|
|
}
|
|
_, managed := m.Managed[name]
|
|
return !managed
|
|
}
|
|
|
|
// WriteManifest writes the manifest to a target directory.
|
|
func WriteManifest(targetPath string, m *Manifest) error {
|
|
m.UpdatedAt = time.Now()
|
|
data, err := json.MarshalIndent(m, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(filepath.Join(targetPath, ManifestFile), data, 0644)
|
|
}
|
|
|
|
// RemoveManifest deletes the manifest file from a target directory.
|
|
// It is a no-op if the file does not exist.
|
|
func RemoveManifest(targetPath string) error {
|
|
err := os.Remove(filepath.Join(targetPath, ManifestFile))
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|