Skip to content

Commit f568792

Browse files
committed
Remove empty dirs
1 parent 4ac8637 commit f568792

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

cmd/apps/init.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"io/fs"
89
"maps"
910
"os"
1011
"os/exec"
@@ -1138,9 +1139,36 @@ func copyTemplate(ctx context.Context, src, dest string, vars templateVars) (int
11381139
}
11391140
log.Debugf(ctx, "Copied %d files", fileCount)
11401141

1142+
if err == nil {
1143+
err = removeEmptyDirs(dest)
1144+
}
1145+
11411146
return fileCount, err
11421147
}
11431148

1149+
// removeEmptyDirs removes empty directories under root, deepest-first.
1150+
// It is used to clean up directories that were created eagerly but ended up
1151+
// with no files after conditional template rendering skipped their contents.
1152+
func removeEmptyDirs(root string) error {
1153+
var dirs []string
1154+
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
1155+
if err != nil {
1156+
return err
1157+
}
1158+
if d.IsDir() && path != root {
1159+
dirs = append(dirs, path)
1160+
}
1161+
return nil
1162+
})
1163+
if err != nil {
1164+
return err
1165+
}
1166+
for i := len(dirs) - 1; i >= 0; i-- {
1167+
_ = os.Remove(dirs[i])
1168+
}
1169+
return nil
1170+
}
1171+
11441172
// templateData builds the data map for Go template execution.
11451173
func templateData(vars templateVars) map[string]any {
11461174
// Sort plugin names for deterministic deprecated compat output.

0 commit comments

Comments
 (0)