Add dryrun flag -n
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user