From 45ee30fae15022c8bb53039e8222458831298e38 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Wed, 3 Dec 2025 00:17:28 +0100 Subject: [PATCH] Add dryrun flag -n --- cmd/s3-genindex/main.go | 29 +++++++++++++++++++++++------ internal/indexgen/indexgen.go | 34 +++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/cmd/s3-genindex/main.go b/cmd/s3-genindex/main.go index 4fed48e..a362417 100644 --- a/cmd/s3-genindex/main.go +++ b/cmd/s3-genindex/main.go @@ -167,6 +167,24 @@ func generateS3HTML(entries []indexgen.FileEntry, opts *indexgen.Options) error return entries[i].Name < entries[j].Name }) + // Determine output file + outputFile := opts.OutputFile + if outputFile == "" { + outputFile = indexgen.DefaultOutputFile + } + + if opts.DryRun { + // Dry run mode: show what would be written + fmt.Printf("Would write S3 index file: %s\n", outputFile) + fmt.Printf("S3 bucket: %s\n", opts.TopDir) + fmt.Printf("Objects found: %d\n", len(entries)) + for _, entry := range entries { + fmt.Printf(" object: %s (%s)\n", entry.Name, entry.SizePretty) + } + return nil + } + + // Normal mode: actually write the file // Get the HTML template tmpl := indexgen.GetHTMLTemplate() if tmpl == nil { @@ -186,12 +204,6 @@ func generateS3HTML(entries []indexgen.FileEntry, opts *indexgen.Options) error Hostname: "S3 Bucket", // Could be improved to show actual endpoint } - // Determine output file - outputFile := opts.OutputFile - if outputFile == "" { - outputFile = indexgen.DefaultOutputFile - } - // Create output file file, err := os.Create(outputFile) if err != nil { @@ -217,6 +229,7 @@ func main() { var excludeRegexStr string var directory string var s3URL string + var dryRun bool // Set defaults opts.DirAppend = true @@ -227,6 +240,7 @@ func main() { flag.StringVar(&directory, "d", "", "local directory to process") flag.StringVar(&s3URL, "s3", "", "S3 URL to process") flag.StringVar(&opts.Filter, "f", "*", "only include files matching glob") + flag.BoolVar(&dryRun, "n", false, "dry run: show what would be written without actually writing") flag.StringVar(&excludeRegexStr, "x", "", "exclude files matching regular expression") flag.BoolVar(&opts.Verbose, "v", false, "verbosely list every processed file") @@ -268,6 +282,9 @@ func main() { } } + // Set dry run flag + opts.DryRun = dryRun + if s3URL != "" { // Parse S3 URL s3Config, err := parseS3URL(s3URL) diff --git a/internal/indexgen/indexgen.go b/internal/indexgen/indexgen.go index 45143fa..e122a4e 100644 --- a/internal/indexgen/indexgen.go +++ b/internal/indexgen/indexgen.go @@ -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 {