Add dryrun flag -n

This commit is contained in:
Pim van Pelt
2025-12-03 00:17:28 +01:00
parent 62db5b2008
commit 45ee30fae1
2 changed files with 48 additions and 15 deletions

View File

@@ -166,6 +166,7 @@ type Options struct {
IncludeHidden bool
ExcludeRegex *regexp.Regexp
Verbose bool
DryRun bool
}
type FileEntry struct {
@@ -194,12 +195,6 @@ func ProcessDir(topDir string, opts *Options) error {
indexPath := filepath.Join(absPath, opts.OutputFile)
file, err := os.Create(indexPath)
if err != nil {
return fmt.Errorf("cannot create file %s: %w", indexPath, err)
}
defer file.Close()
dirName := filepath.Base(absPath)
entries, err := ReadDirEntries(absPath, opts)
@@ -226,9 +221,30 @@ func ProcessDir(topDir string, opts *Options) error {
OutputFile: opts.OutputFile,
}
err = GetHTMLTemplate().Execute(file, templateData)
if err != nil {
return fmt.Errorf("failed to execute template: %w", err)
if opts.DryRun {
// Dry run mode: show what would be written
fmt.Printf("Would write index file: %s\n", indexPath)
fmt.Printf("Directory: %s\n", dirName)
fmt.Printf("Entries found: %d\n", len(entries))
for _, entry := range entries {
entryType := "file"
if entry.IsDir {
entryType = "directory"
}
fmt.Printf(" %s: %s\n", entryType, entry.Name)
}
} else {
// Normal mode: actually write the file
file, err := os.Create(indexPath)
if err != nil {
return fmt.Errorf("cannot create file %s: %w", indexPath, err)
}
defer file.Close()
err = GetHTMLTemplate().Execute(file, templateData)
if err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
}
if opts.Recursive {