mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
When flatten=true, SyncExtra maps subdirectory files to target root using filepath.Base(). First file wins on collision, subsequent are skipped with warnings. Includes config validation, sync-time guard, CheckSyncStatus collision handling, and CollectExtraFiles support. Ref: #97
66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"slices"
|
|
)
|
|
|
|
var extraNameRegex = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]*$`)
|
|
|
|
var reservedExtraNames = map[string]bool{
|
|
"skills": true,
|
|
"extras": true,
|
|
}
|
|
|
|
// ValidateExtraName checks that a name is safe for use as an extras directory.
|
|
// It rejects empty names, reserved words, and names that don't match the
|
|
// allowed character set (alphanumeric, hyphens, underscores; must start with
|
|
// alphanumeric).
|
|
func ValidateExtraName(name string) error {
|
|
if name == "" {
|
|
return fmt.Errorf("extra name cannot be empty")
|
|
}
|
|
if reservedExtraNames[name] {
|
|
return fmt.Errorf("extra name %q is reserved", name)
|
|
}
|
|
if !extraNameRegex.MatchString(name) {
|
|
return fmt.Errorf("extra name %q is invalid: must start with a letter or digit and contain only letters, digits, hyphens, or underscores", name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ExtraSyncModes is the authoritative list of valid extras sync modes.
|
|
// Same values as ValidSyncModes; kept as a separate variable for API stability.
|
|
var ExtraSyncModes = ValidSyncModes
|
|
|
|
// ValidateExtraMode checks that mode is a valid sync mode.
|
|
// Empty string is allowed (defaults to "merge" at runtime).
|
|
func ValidateExtraMode(mode string) error {
|
|
if mode == "" {
|
|
return nil
|
|
}
|
|
if !slices.Contains(ExtraSyncModes, mode) {
|
|
return fmt.Errorf("invalid mode %q: must be merge, copy, or symlink", mode)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateExtraFlatten checks that flatten is not used with symlink mode.
|
|
func ValidateExtraFlatten(flatten bool, mode string) error {
|
|
if flatten && mode == "symlink" {
|
|
return fmt.Errorf("flatten cannot be used with symlink mode (symlink links the entire directory)")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateExtraNameUnique checks that the name doesn't duplicate an existing extra.
|
|
func ValidateExtraNameUnique(name string, existing []ExtraConfig) error {
|
|
for _, e := range existing {
|
|
if e.Name == name {
|
|
return fmt.Errorf("extra name %q already exists", name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|