Skip to content

Commit

Permalink
stepwriter: DryRun: Check if the symlink source file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
livingsilver94 committed Dec 14, 2023
1 parent 0e17802 commit e784c65
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion installer/stepwriter/dryrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package stepwriter
import (
"fmt"
"io"
"io/fs"
"os"
"strings"

Expand All @@ -17,6 +18,10 @@ type DryRun struct {
// Dest is where messages are written to.
// When nil, it defaults to [os.Stdout].
Dest io.Writer

// FS is the filesystem from where files are fetched.
// When nil, it defaults to the current directory.
FS fs.FS
}

func (d DryRun) Setup(script string) error {
Expand All @@ -30,7 +35,12 @@ func (d DryRun) InstallPackages(fullCmd []string) error {
}

func (d DryRun) SymlinkFile(dst service.FilePath, src string) error {
_, err := d.printf("%s\t➜ %s", src, dst.Path)
ok, err := d.fileAccessible(src)
if !ok {
return err
}

_, err = d.printf("%s\t➜ %s", src, dst.Path)
if err != nil {
return err
}
Expand Down Expand Up @@ -68,6 +78,20 @@ func (d DryRun) Finalize(script string) error {
return err
}

func (d DryRun) fileAccessible(path string) (bool, error) {
f := d.FS
if f == nil {
f = os.DirFS(".")
}
file, err := f.Open(path)
if err != nil {
_, err = d.printf("Error opening %s: %s", path, err)
return false, err
}
defer file.Close()
return true, nil
}

func (d DryRun) print(a ...any) (n int, err error) {
dest := d.Dest
if dest == nil {
Expand Down

0 comments on commit e784c65

Please sign in to comment.