@@ -16,7 +16,7 @@ import (
1616func CopyFile (src , dst string ) error {
1717 sourceFileStat , err := os .Stat (src )
1818 if err != nil {
19- return err
19+ return fmt . Errorf ( "stat source file %s: %w" , src , err )
2020 }
2121
2222 if ! sourceFileStat .Mode ().IsRegular () {
@@ -25,25 +25,24 @@ func CopyFile(src, dst string) error {
2525
2626 sourceFile , err := os .Open (src )
2727 if err != nil {
28- return err
28+ return fmt . Errorf ( "open source file %s: %w" , src , err )
2929 }
3030 defer sourceFile .Close ()
3131
3232 destinationFile , err := os .Create (dst )
3333 if err != nil {
34- return err
34+ return fmt . Errorf ( "create destination file %s: %w" , dst , err )
3535 }
3636 defer destinationFile .Close ()
3737
3838 _ , err = io .Copy (destinationFile , sourceFile )
3939 if err != nil {
40- return err
40+ return fmt . Errorf ( "copy to %s: %w" , dst , err )
4141 }
4242
43- // Preserve the file permissions from the source file
4443 err = os .Chmod (dst , sourceFileStat .Mode ())
4544 if err != nil {
46- return err
45+ return fmt . Errorf ( "chmod %s: %w" , dst , err )
4746 }
4847
4948 return nil
@@ -69,7 +68,7 @@ func WriteEmbeddedFile(fsys fs.FS, srcPath, dstPath string, mode os.FileMode) er
6968 return fmt .Errorf ("reading embedded file %s: %w" , srcPath , err )
7069 }
7170 if err := os .WriteFile (dstPath , data , mode ); err != nil {
72- return err
71+ return fmt . Errorf ( "write file %s: %w" , dstPath , err )
7372 }
7473 return os .Chmod (dstPath , mode )
7574}
@@ -78,11 +77,11 @@ func WriteEmbeddedFile(fsys fs.FS, srcPath, dstPath string, mode os.FileMode) er
7877func WriteEmbeddedDir (fsys fs.FS , srcDir , dstDir string ) error {
7978 return fs .WalkDir (fsys , srcDir , func (path string , d fs.DirEntry , err error ) error {
8079 if err != nil {
81- return err
80+ return fmt . Errorf ( "walk embedded dir: %w" , err )
8281 }
8382 relPath , err := filepath .Rel (srcDir , path )
8483 if err != nil {
85- return err
84+ return fmt . Errorf ( "compute relative path for %s: %w" , path , err )
8685 }
8786 target := filepath .Join (dstDir , relPath )
8887
@@ -99,19 +98,19 @@ func WriteEmbeddedDir(fsys fs.FS, srcDir, dstDir string) error {
9998func CopyDir (src , dst string ) error {
10099 srcInfo , err := os .Stat (src )
101100 if err != nil {
102- return err
101+ return fmt . Errorf ( "stat source dir %s: %w" , src , err )
103102 }
104103 if ! srcInfo .IsDir () {
105104 return fmt .Errorf ("%s is not a directory" , src )
106105 }
107106
108107 if err := os .MkdirAll (dst , 0o755 ); err != nil {
109- return err
108+ return fmt . Errorf ( "create destination dir %s: %w" , dst , err )
110109 }
111110
112111 entries , err := os .ReadDir (src )
113112 if err != nil {
114- return err
113+ return fmt . Errorf ( "read source dir %s: %w" , src , err )
115114 }
116115
117116 for _ , entry := range entries {
@@ -120,11 +119,11 @@ func CopyDir(src, dst string) error {
120119
121120 if entry .IsDir () {
122121 if err := CopyDir (srcPath , dstPath ); err != nil {
123- return err
122+ return fmt . Errorf ( "copy subdir %s: %w" , entry . Name (), err )
124123 }
125124 } else {
126125 if err := CopyFile (srcPath , dstPath ); err != nil {
127- return err
126+ return fmt . Errorf ( "copy file %s: %w" , entry . Name (), err )
128127 }
129128 }
130129 }
@@ -143,7 +142,7 @@ func RemovePathIfExists(targetPath string) error {
143142 if os .IsNotExist (err ) {
144143 return nil
145144 }
146- return err
145+ return fmt . Errorf ( "lstat %s: %w" , targetPath , err )
147146 }
148147
149148 if info .IsDir () {
@@ -157,7 +156,7 @@ func RemovePathIfExists(targetPath string) error {
157156func FileContainsIncludeMakefileCommon (makefilePath string ) (bool , error ) {
158157 data , err := os .ReadFile (makefilePath )
159158 if err != nil {
160- return false , err
159+ return false , fmt . Errorf ( "read %s: %w" , makefilePath , err )
161160 }
162161 contents := string (data )
163162 // We keep this simple to avoid regex: look for lines with 'include' and 'Makefile-common'
@@ -177,7 +176,7 @@ func FileContainsIncludeMakefileCommon(makefilePath string) (bool, error) {
177176func PrependLineToFile (filePath , line string ) error {
178177 data , err := os .ReadFile (filePath )
179178 if err != nil {
180- return err
179+ return fmt . Errorf ( "read %s: %w" , filePath , err )
181180 }
182181
183182 mode := os .FileMode (0o644 )
@@ -208,7 +207,6 @@ func WriteYAMLWithIndent(data interface{}, filePath string) error {
208207 return fmt .Errorf ("failed to encode YAML to %s: %w" , filePath , err )
209208 }
210209
211- // Set file permissions to 0644
212210 if err := os .Chmod (filePath , 0o644 ); err != nil {
213211 return fmt .Errorf ("failed to set permissions on %s: %w" , filePath , err )
214212 }
0 commit comments