CopyEmbedAssets copies files in the named embed.FS sourceDir to the local targetDir (full path) Some files may be excluded if they are in the excludedFiles list and contain #ddev-generated.
(fsys embed.FS, sourceDir string, targetDir string, excludedFiles []string)
| 14 | // CopyEmbedAssets copies files in the named embed.FS sourceDir to the local targetDir (full path) |
| 15 | // Some files may be excluded if they are in the excludedFiles list and contain #ddev-generated. |
| 16 | func CopyEmbedAssets(fsys embed.FS, sourceDir string, targetDir string, excludedFiles []string) error { |
| 17 | subdirs, err := fsys.ReadDir(sourceDir) |
| 18 | if err != nil { |
| 19 | return err |
| 20 | } |
| 21 | for _, d := range subdirs { |
| 22 | sourcePath := path.Join(sourceDir, d.Name()) |
| 23 | if d.IsDir() { |
| 24 | err = CopyEmbedAssets(fsys, path.Join(sourceDir, d.Name()), path.Join(targetDir, d.Name()), excludedFiles) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | } else { |
| 29 | localPath := filepath.Join(targetDir, d.Name()) |
| 30 | |
| 31 | // We can overwrite the file if it has the #ddev-generated |
| 32 | // or if it is an empty file. |
| 33 | sigFound, err := FgrepStringInFile(localPath, nodeps.DdevFileSignature) |
| 34 | s, _ := os.Stat(localPath) |
| 35 | if sigFound || (s != nil && s.Size() == 0) || err != nil { |
| 36 | content, err := fsys.ReadFile(sourcePath) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | err = os.MkdirAll(filepath.Dir(localPath), 0755) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | if sigFound { |
| 45 | // If the file already exists and has the same content, don't overwrite it. |
| 46 | if existingContent, err := os.ReadFile(localPath); err == nil { |
| 47 | if bytes.Equal(content, existingContent) { |
| 48 | continue |
| 49 | } |
| 50 | } |
| 51 | // If the file already exists and is excluded, don't overwrite it. |
| 52 | if excludedFiles != nil && slices.Contains(excludedFiles, localPath) { |
| 53 | continue |
| 54 | } |
| 55 | } |
| 56 | err = os.WriteFile(localPath, content, 0755) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return nil |
| 64 | } |
no test coverage detected