Ensure target exists by creating directories

This commit is contained in:
2024-09-11 18:53:22 +02:00
parent e6bb1f0c53
commit ff1af19088

View File

@@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@@ -122,7 +122,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
status <- fmt.Errorf("could not stat %s%s%s, stopping; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor) status <- fmt.Errorf("could not stat %s%s%s, stopping; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
return return
} }
if info.Mode().IsRegular() && info.Name() == path.Base(instruction.Source) { if info.Mode().IsRegular() && info.Name() == filepath.Base(instruction.Source) {
log.Printf("Overwriting existing file %s%s%s", TargetColor, instruction.Target, DefaultColor) log.Printf("Overwriting existing file %s%s%s", TargetColor, instruction.Target, DefaultColor)
err := os.Remove(instruction.Target) err := os.Remove(instruction.Target)
if err != nil { if err != nil {
@@ -149,6 +149,15 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
} }
} }
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 var err error
if instruction.Hard { if instruction.Hard {
err = os.Link(instruction.Source, instruction.Target) err = os.Link(instruction.Source, instruction.Target)