Files
skillshare/internal/config/local_projects.go
T
Willieandsim 51107fb1ac fix(projects): save new projects in config.yaml again (#793)
* fix(projects): save new projects in config.yaml again

v0.29.0 wrote every project add, edit and remove to a machine-local
registry, so a project added on one machine never reached config.yaml
or the other machines that share it (#792). Most people share one
config.yaml and expect a new project to follow it.

config.yaml is the default again. Machine-local storage stays as an
explicit choice: --machine-local on add, or projects_storage:
machine-local for every new project, with --shared to override it.
The new share command moves a local project into config.yaml, and
localize still copies one to this machine. Edits and removals stay in
the layer that owns the project; removing a local copy of a
config.yaml project keeps config.yaml's version unless --shared asks
to remove both, so one machine cannot silently drop a project for
the others. Exclusions written by v0.29.0 are left as they are.

The dashboard picks storage once, in the Add project dialog, and
offers the two removal choices only where both copies exist. Moving a
project between layers stays a CLI command, so the detail page keeps
one job.

Closes #792

* fix(projects): recheck both layers after a confirmation, sync the saved key

remove and share ask before taking the config lock, then compared only
the merged entry after locking. A share or localize from another
terminal moves a project between layers without changing that entry, so
a remove confirmed for this machine's copy could then delete config.yaml's
declaration for every machine, and a confirmed share could overwrite
config.yaml settings changed meanwhile. The recheck now compares the
owning layer and config.yaml's declaration too (ProjectLayerState).

Re-adding a project that v0.29.0 hid restores config.yaml's own key,
which can be spelled differently from the folder given to add (~/app
versus an absolute path). add --sync looked the project up by the given
spelling and failed with "project not found" after saving it; it now
syncs the key that was saved.

* test(projects): make the layer tests hold on Windows

The mutation-sequence test looked the shared project up by
filepath.Join, but the fixture spells config.yaml's key $ROOT/app;
on Windows the two spellings differ, so the lookup found nothing.
It now asks the config for the key it uses.

The remove save-failure test blocked the local registry, but removing a
config.yaml project now writes config.yaml. On Linux the registry check
still failed with ENOTDIR first, which hid the mismatch; on Windows that
reads as a missing file and the save went through. The test now blocks
config.yaml, as the edit save-failure test does, and its hook no longer
shadows the config path with the target path.

---------

Co-authored-by: sim <sim@example.invalid>
2026-11-06 23:49:30 +08:00

362 lines
9.7 KiB
Go

package config
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"maps"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"gopkg.in/yaml.v3"
"skillshare/internal/utils"
)
// ProjectStateDir keeps registrations outside the shared config directory,
// including Windows, where StateDir otherwise defaults to roaming AppData.
func ProjectStateDir() string { return projectStateDir(runtime.GOOS) }
func projectStateDir(goos string) string {
if os.Getenv("XDG_STATE_HOME") != "" || goos != "windows" {
return StateDir()
}
local := os.Getenv("LOCALAPPDATA")
if local == "" {
home, _ := os.UserHomeDir()
local = filepath.Join(home, "AppData", "Local")
}
return filepath.Join(local, "skillshare", "state")
}
// ProjectConfigIdentity is shared by registry and lock namespaces. Config links
// to one destination share state; lexical project-root links remain separate.
func ProjectConfigIdentity(path string) (string, error) {
return projectConfigIdentity(path, runtime.GOOS)
}
func projectConfigIdentity(path, goos string) (string, error) {
dest, err := utils.ConfigWritePath(path, false)
if err != nil {
return "", err
}
dest, err = filepath.Abs(dest)
if err != nil {
return "", err
}
// The automatic Windows directory migration is an alias, not a new config.
if goos == "windows" && os.Getenv("SKILLSHARE_CONFIG") == "" {
home, _ := os.UserHomeDir()
legacy := filepath.Join(home, ".config", "skillshare", "config.yaml")
legacy, err = utils.ConfigWritePath(legacy, false)
if err != nil {
return "", err
}
legacy, err = filepath.Abs(legacy)
if err != nil {
return "", err
}
if utils.PathsEqual(dest, legacy) {
dest, err = utils.ConfigWritePath(ConfigPath(), false)
if err != nil {
return "", err
}
dest, err = filepath.Abs(dest)
if err != nil {
return "", err
}
}
}
dest = filepath.Clean(dest)
if goos == "windows" {
dest = strings.ToLower(dest)
}
sum := sha256.Sum256([]byte(dest))
return hex.EncodeToString(sum[:8]), nil
}
// LocalProjectsPath returns the selected config's machine-local registry file.
// It never creates directories, locks or files.
func LocalProjectsPath(path string) (string, error) {
id, err := ProjectConfigIdentity(path)
if err != nil {
return "", err
}
return filepath.Join(ProjectStateDir(), "projects", id+".yaml"), nil
}
type localProjects struct {
Version int `yaml:"version"`
Projects map[string]ManagedProject `yaml:"projects,omitempty"`
Excluded []string `yaml:"excluded,omitempty"`
}
type projectOverlay struct {
shared map[string]ManagedProject
origins map[string]string
state localProjects
path, configPath, destination string
raw, sharedRaw []byte
}
func projectRootIdentity(root string) (string, error) {
abs := expandPath(root)
if strings.ContainsRune(root, 0) || !filepath.IsAbs(abs) {
return "", fmt.Errorf("projects: %s must be an absolute path or start with ~", root)
}
abs = filepath.Clean(abs)
if runtime.GOOS == "windows" {
abs = strings.ToLower(abs)
}
return abs, nil
}
func projectRootIndex(entries map[string]ManagedProject) (map[string]string, error) {
index := map[string]string{}
for _, root := range slices.Sorted(maps.Keys(entries)) {
id, err := projectRootIdentity(root)
if err != nil {
return nil, err
}
if other, ok := index[id]; ok {
return nil, fmt.Errorf("projects: %s and %s declare the same root", other, root)
}
index[id] = root
}
return index, nil
}
func mergeLocalProjects(shared map[string]ManagedProject, state localProjects) (map[string]ManagedProject, map[string]string, error) {
sharedIndex, err := projectRootIndex(shared)
if err != nil {
return nil, nil, err
}
localIndex, err := projectRootIndex(state.Projects)
if err != nil {
return nil, nil, err
}
excluded := map[string]bool{}
for _, root := range state.Excluded {
id, err := projectRootIdentity(root)
if err != nil {
return nil, nil, err
}
excluded[id] = true
}
effective := map[string]ManagedProject{}
origins := map[string]string{}
for id, root := range sharedIndex {
if _, local := localIndex[id]; local || excluded[id] {
continue
}
effective[root] = shared[root]
origins[root] = "shared"
}
for root, p := range state.Projects {
effective[root] = p
origins[root] = "local"
}
return effective, origins, nil
}
// readLocalProjects refuses redirection of a registry or its namespace folder.
func readLocalProjects(path string) ([]byte, error) {
for _, p := range []string{filepath.Dir(path), path} {
info, err := os.Lstat(p)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if err == nil && info.Mode()&os.ModeSymlink != 0 {
return nil, fmt.Errorf("local project registry must not be a symlink: %s", p)
}
}
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
return nil, nil
}
return data, err
}
func decodeLocalProjects(data []byte) (localProjects, error) {
state := localProjects{Version: 1, Projects: map[string]ManagedProject{}}
if data == nil {
return state, nil
}
state.Version = 0
dec := yaml.NewDecoder(bytes.NewReader(data))
dec.KnownFields(true)
if err := dec.Decode(&state); err != nil {
return state, fmt.Errorf("invalid local project registry: %w", err)
}
if dec.Decode(new(yaml.Node)) != io.EOF {
return state, fmt.Errorf("local project registry must contain one YAML document")
}
if state.Version != 1 {
return state, fmt.Errorf("unsupported local project registry version %d", state.Version)
}
if state.Projects == nil {
state.Projects = map[string]ManagedProject{}
}
return state, nil
}
func (c *Config) loadLocalProjects(path string, sharedRaw []byte) error {
localPath, err := LocalProjectsPath(path)
if err != nil {
return err
}
raw, err := readLocalProjects(localPath)
if err != nil {
return err
}
state, err := decodeLocalProjects(raw)
if err != nil {
return fmt.Errorf("%s: %w", localPath, err)
}
dest, err := utils.ConfigWritePath(path, false)
if err != nil {
return err
}
dest, err = filepath.Abs(dest)
if err != nil {
return err
}
overlay := &projectOverlay{shared: maps.Clone(c.Projects), state: state, path: localPath, configPath: path, destination: dest, raw: raw, sharedRaw: sharedRaw}
c.Projects, overlay.origins, err = mergeLocalProjects(overlay.shared, state)
if err != nil {
return err
}
c.projectOverlay = overlay
return nil
}
// ProjectOrigin identifies the winning declaration and the file that owns it.
func (c *Config) ProjectOrigin(root string) (string, string) {
if o := c.projectOverlay; o != nil {
if o.origins[root] == "local" {
return "local", o.path
}
return "shared", o.configPath
}
return "shared", ReadOnlyConfigPath()
}
// Values of projects_storage.
const (
ProjectsStorageShared = "shared"
ProjectsStorageMachineLocal = "machine-local"
)
// NewProjectOrigin is the origin projects_storage gives a new project.
func (c *Config) NewProjectOrigin() string {
if c.ProjectsStorage == ProjectsStorageMachineLocal {
return "local"
}
return "shared"
}
// HasSharedProject reports whether config.yaml declares root, including a
// declaration this machine overrides or hides.
func (c *Config) HasSharedProject(root string) bool {
shared := c.Projects
if c.projectOverlay != nil {
shared = c.projectOverlay.shared
}
_, ok := layerKey(shared, root)
return ok
}
// ProjectLayerState is root's declaration in each layer. A command that asks
// before it locks compares it again afterwards: a share or localize from
// another terminal can move the project between layers without changing the
// merged entry.
type ProjectLayerState struct {
Effective ManagedProject
Origin string
Shared *ManagedProject
}
// ProjectLayerState reports root's effective entry, owning layer and
// config.yaml declaration.
func (c *Config) ProjectLayerState(root string) ProjectLayerState {
state := ProjectLayerState{Effective: c.Projects[root]}
state.Origin, _ = c.ProjectOrigin(root)
shared := c.Projects
if c.projectOverlay != nil {
shared = c.projectOverlay.shared
}
if key, ok := layerKey(shared, root); ok {
p := shared[key]
state.Shared = &p
}
return state
}
// ProjectKey returns the key c.Projects uses for root.
func (c *Config) ProjectKey(root string) (string, bool) { return layerKey(c.Projects, root) }
// HasLocalProject includes exclusions, so conversion cannot discard ordinary
// targets while their replacement declaration would remain hidden.
func (c *Config) HasLocalProject(root string) bool {
o := c.projectOverlay
if o == nil {
return false
}
id, err := projectRootIdentity(root)
if err != nil {
return false
}
for key := range o.state.Projects {
other, _ := projectRootIdentity(key)
if id == other {
return true
}
}
for _, key := range o.state.Excluded {
other, _ := projectRootIdentity(key)
if id == other {
return true
}
}
return false
}
// sharedProjectDeclarations projects effective edits back onto shared intent.
// Hidden declarations survive; local overrides and exclusions never leak out.
func (c *Config) sharedProjectDeclarations() map[string]ManagedProject {
o := c.projectOverlay
if o == nil {
return c.Projects
}
out := maps.Clone(o.shared)
if out == nil {
out = map[string]ManagedProject{}
}
for root, origin := range o.origins {
if origin != "shared" {
continue
}
if p, ok := c.Projects[root]; ok {
out[root] = p
} else {
delete(out, root)
}
}
for root, p := range c.Projects {
if o.origins[root] == "" {
out[root] = p
}
}
return out
}
// DecodeReadOnly validates a replacement shared document against this machine's
// local registry without requiring the current shared YAML to be valid.
func DecodeReadOnly(path string, data []byte) (*Config, error) {
return decodeConfig(path, data, false)
}