256 lines
8.1 KiB
Go
256 lines
8.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type LinkInstruction struct {
|
|
Source string `yaml:"source"`
|
|
Target string `yaml:"target"`
|
|
Force bool `yaml:"force,omitempty"`
|
|
Hard bool `yaml:"hard,omitempty"`
|
|
Delete bool `yaml:"delete,omitempty"`
|
|
}
|
|
|
|
type YAMLConfig struct {
|
|
Links []LinkInstruction `yaml:"links"`
|
|
}
|
|
|
|
func (instruction *LinkInstruction) Tidy() {
|
|
instruction.Source = strings.ReplaceAll(instruction.Source, "\"", "")
|
|
instruction.Source = strings.ReplaceAll(instruction.Source, "\\", "/")
|
|
instruction.Source = strings.TrimSpace(instruction.Source)
|
|
|
|
instruction.Target = strings.ReplaceAll(instruction.Target, "\"", "")
|
|
instruction.Target = strings.ReplaceAll(instruction.Target, "\\", "/")
|
|
instruction.Target = strings.TrimSpace(instruction.Target)
|
|
}
|
|
|
|
func (instruction *LinkInstruction) String() string {
|
|
var flags []string
|
|
if instruction.Force {
|
|
flags = append(flags, "force=true")
|
|
}
|
|
if instruction.Hard {
|
|
flags = append(flags, "hard=true")
|
|
}
|
|
if instruction.Delete {
|
|
flags = append(flags, "delete=true")
|
|
}
|
|
|
|
flagsStr := ""
|
|
if len(flags) > 0 {
|
|
flagsStr = " [" + strings.Join(flags, ", ") + "]"
|
|
}
|
|
|
|
return fmt.Sprintf("%s%s%s → %s%s%s%s",
|
|
SourceColor, instruction.Source, DefaultColor,
|
|
TargetColor, instruction.Target, DefaultColor,
|
|
flagsStr)
|
|
}
|
|
|
|
func ParseInstruction(line, workdir string) (LinkInstruction, error) {
|
|
line = strings.TrimSpace(line)
|
|
if strings.HasPrefix(line, "#") {
|
|
return LinkInstruction{}, fmt.Errorf("comment line")
|
|
}
|
|
|
|
parts := strings.Split(line, deliminer)
|
|
instruction := LinkInstruction{}
|
|
|
|
if len(parts) < 2 {
|
|
return instruction, fmt.Errorf("invalid format - not enough parameters (must have at least source and target)")
|
|
}
|
|
|
|
instruction.Source = strings.TrimSpace(parts[0])
|
|
instruction.Target = strings.TrimSpace(parts[1])
|
|
|
|
for i := 2; i < len(parts); i++ {
|
|
flagPart := strings.TrimSpace(parts[i])
|
|
|
|
// Support for legacy format (backward compatibility)
|
|
if !strings.Contains(flagPart, "=") {
|
|
// Legacy format: positional boolean flags
|
|
switch i {
|
|
case 2: // Force flag (3rd position)
|
|
instruction.Force = isTrue(flagPart)
|
|
case 3: // Hard flag (4th position)
|
|
instruction.Hard = isTrue(flagPart)
|
|
case 4: // Delete flag (5th position)
|
|
instruction.Delete = isTrue(flagPart)
|
|
if instruction.Delete {
|
|
instruction.Force = true // Delete implies Force
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
|
|
// New format: named flags (name=value)
|
|
nameValue := strings.SplitN(flagPart, "=", 2)
|
|
if len(nameValue) != 2 {
|
|
// Skip malformed flags
|
|
continue
|
|
}
|
|
|
|
flagName := strings.ToLower(strings.TrimSpace(nameValue[0]))
|
|
flagValue := strings.TrimSpace(nameValue[1])
|
|
|
|
switch flagName {
|
|
case "force", "f":
|
|
instruction.Force = isTrue(flagValue)
|
|
case "hard", "h":
|
|
instruction.Hard = isTrue(flagValue)
|
|
case "delete", "d":
|
|
instruction.Delete = isTrue(flagValue)
|
|
if instruction.Delete {
|
|
instruction.Force = true // Delete implies Force
|
|
}
|
|
}
|
|
}
|
|
|
|
instruction.Tidy()
|
|
instruction.Source, _ = ConvertHome(instruction.Source)
|
|
instruction.Target, _ = ConvertHome(instruction.Target)
|
|
|
|
instruction.Source = NormalizePath(instruction.Source, workdir)
|
|
instruction.Target = NormalizePath(instruction.Target, workdir)
|
|
|
|
return instruction, nil
|
|
}
|
|
|
|
func isTrue(value string) bool {
|
|
value = strings.ToLower(strings.TrimSpace(value))
|
|
return value == "true" || value == "t" || value == "yes" || value == "y" || value == "1"
|
|
}
|
|
|
|
func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
|
defer close(status)
|
|
if !FileExists(instruction.Source) {
|
|
status <- fmt.Errorf("instruction source %s%s%s does not exist", SourceColor, instruction.Source, DefaultColor)
|
|
return
|
|
}
|
|
|
|
if !instruction.Force && AreSame(instruction.Source, instruction.Target) {
|
|
status <- fmt.Errorf("source %s%s%s and target %s%s%s are the same, %snothing to do...%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor, PathColor, DefaultColor)
|
|
return
|
|
}
|
|
|
|
if FileExists(instruction.Target) {
|
|
if instruction.Force {
|
|
isSymlink, err := IsSymlink(instruction.Target)
|
|
if err != nil {
|
|
status <- fmt.Errorf("could not determine whether %s%s%s is a sym link or not, stopping; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
|
return
|
|
}
|
|
|
|
if instruction.Hard {
|
|
info, err := os.Stat(instruction.Target)
|
|
if err != nil {
|
|
status <- fmt.Errorf("could not stat %s%s%s, stopping; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
|
return
|
|
}
|
|
if info.Mode().IsRegular() && info.Name() == filepath.Base(instruction.Source) {
|
|
log.Printf("Overwriting existing file %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
|
err := os.Remove(instruction.Target)
|
|
if err != nil {
|
|
status <- fmt.Errorf("could not remove existing file %s%s%s; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
if isSymlink {
|
|
log.Printf("Removing symlink at %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
|
err = os.Remove(instruction.Target)
|
|
if err != nil {
|
|
status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
|
return
|
|
}
|
|
} else {
|
|
if !instruction.Delete {
|
|
status <- fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
|
return
|
|
}
|
|
log.Printf("%sDeleting (!!!)%s %s%s%s", ImportantColor, DefaultColor, TargetColor, instruction.Target, DefaultColor)
|
|
err = os.RemoveAll(instruction.Target)
|
|
if err != nil {
|
|
status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
status <- fmt.Errorf("target %s%s%s exists - handle manually or set the 'forced' flag (3rd field)", TargetColor, instruction.Target, DefaultColor)
|
|
return
|
|
}
|
|
}
|
|
|
|
targetDir := filepath.Dir(instruction.Target)
|
|
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
|
|
err = os.MkdirAll(targetDir, 0755)
|
|
if err != nil {
|
|
status <- fmt.Errorf("failed creating directory %s%s%s due to %s%+v%s", TargetColor, targetDir, DefaultColor, ErrorColor, err, DefaultColor)
|
|
return
|
|
}
|
|
}
|
|
|
|
var err error
|
|
if instruction.Hard {
|
|
err = os.Link(instruction.Source, instruction.Target)
|
|
} else {
|
|
err = os.Symlink(instruction.Source, instruction.Target)
|
|
}
|
|
if err != nil {
|
|
status <- fmt.Errorf("failed creating symlink between %s%s%s and %s%s%s with error %s%+v%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
|
return
|
|
}
|
|
log.Printf("Created symlink between %s%s%s and %s%s%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor)
|
|
|
|
status <- nil
|
|
}
|
|
|
|
func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
|
data, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading YAML file: %w", err)
|
|
}
|
|
|
|
// First try to parse as a list of link instructions
|
|
var config YAMLConfig
|
|
err = yaml.Unmarshal(data, &config)
|
|
if err != nil || len(config.Links) == 0 {
|
|
// If that fails, try parsing as a direct list of instructions
|
|
var instructions []LinkInstruction
|
|
err = yaml.Unmarshal(data, &instructions)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error parsing YAML: %w", err)
|
|
}
|
|
config.Links = instructions
|
|
}
|
|
|
|
for i := range config.Links {
|
|
config.Links[i].Tidy()
|
|
config.Links[i].Source, _ = ConvertHome(config.Links[i].Source)
|
|
config.Links[i].Target, _ = ConvertHome(config.Links[i].Target)
|
|
config.Links[i].Source = NormalizePath(config.Links[i].Source, workdir)
|
|
config.Links[i].Target = NormalizePath(config.Links[i].Target, workdir)
|
|
|
|
// If Delete is true, Force must also be true
|
|
if config.Links[i].Delete {
|
|
config.Links[i].Force = true
|
|
}
|
|
}
|
|
|
|
return config.Links, nil
|
|
}
|
|
|
|
func IsYAMLFile(filename string) bool {
|
|
ext := strings.ToLower(filepath.Ext(filename))
|
|
return ext == ".yaml" || ext == ".yml"
|
|
}
|