From e784c65a345ca10e40c5dbd52c0cbf73da98f832 Mon Sep 17 00:00:00 2001 From: Fabio Forni Date: Thu, 14 Dec 2023 19:33:02 +0100 Subject: [PATCH] stepwriter: DryRun: Check if the symlink source file exists --- installer/stepwriter/dryrun.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/installer/stepwriter/dryrun.go b/installer/stepwriter/dryrun.go index 28a4ba1..3306ec6 100644 --- a/installer/stepwriter/dryrun.go +++ b/installer/stepwriter/dryrun.go @@ -6,6 +6,7 @@ package stepwriter import ( "fmt" "io" + "io/fs" "os" "strings" @@ -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 { @@ -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 } @@ -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 {