Fix up logging a little

This commit is contained in:
2025-03-10 18:30:09 +01:00
parent 62bfb91246
commit 6359705714
5 changed files with 162 additions and 57 deletions

View File

@@ -2,7 +2,6 @@ package main
import (
"fmt"
"log"
"os"
"path/filepath"
"slices"
@@ -157,7 +156,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
return
}
if info.Mode().IsRegular() && info.Name() == filepath.Base(instruction.Source) {
log.Printf("Overwriting existing file %s%s%s", TargetColor, instruction.Target, DefaultColor)
LogTarget("Overwriting existing file %s", instruction.Target)
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)
@@ -167,7 +166,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
}
if isSymlink {
log.Printf("Removing symlink at %s%s%s", TargetColor, instruction.Target, DefaultColor)
LogTarget("Removing symlink at %s", instruction.Target)
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)
@@ -178,7 +177,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
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)
LogImportant("Deleting (!!!) %s", instruction.Target)
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)
@@ -210,7 +209,9 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
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)
LogInfo("Created symlink between %s and %s",
FormatSourcePath(instruction.Source),
FormatTargetPath(instruction.Target))
status <- nil
}
@@ -237,12 +238,13 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
toRemove := []int{}
for i, link := range config.Links {
if strings.Contains(link.Source, "*") {
log.Printf("Expanding wildcard source %s%s%s in YAML file %s%s%s", SourceColor, link.Source, DefaultColor, SourceColor, filename, DefaultColor)
LogSource("Expanding wildcard source %s in YAML file %s", link.Source, filename)
newlinks, err := ExpandWildcard(link.Source, workdir, link.Target)
if err != nil {
return nil, fmt.Errorf("error expanding wildcard: %w", err)
}
log.Printf("Expanded wildcard source %s%s%s in YAML file %s%s%s to %d links", SourceColor, link.Source, DefaultColor, SourceColor, filename, DefaultColor, len(newlinks))
LogInfo("Expanded wildcard source %s in YAML file %s to %d links",
FormatSourcePath(link.Source), FormatSourcePath(filename), len(newlinks))
config.Links = append(config.Links, newlinks...)
toRemove = append(toRemove, i)
}
@@ -287,6 +289,7 @@ func ExpandWildcard(source, workdir, target string) (links []LinkInstruction, er
links = append(links, link)
}
LogInfo("Expanded wildcard source %s to %d links", FormatSourcePath(source), len(links))
return
}