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

@@ -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)

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 {