Add a -i flag to force showing 'index.html' in the output listing; by default do not show them.

This commit is contained in:
Pim van Pelt
2025-12-03 12:23:40 +01:00
parent 7829000c55
commit 16fa899b91
4 changed files with 42 additions and 29 deletions

View File

@@ -125,6 +125,11 @@ func processS3Bucket(s3Config *S3Config, opts *indexgen.Options) error {
continue
}
// Skip index.html files unless ShowIndexFiles is enabled
if !opts.ShowIndexFiles && strings.HasSuffix(keyName, opts.OutputFile) {
continue
}
// Simple glob matching for filter
if opts.Filter != "*" && opts.Filter != "" {
matched, err := filepath.Match(opts.Filter, keyName)
@@ -339,6 +344,7 @@ func main() {
var directory string
var s3URL string
var dryRun bool
var showIndexFiles bool
// Set defaults
opts.DirAppend = true
@@ -352,6 +358,7 @@ func main() {
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")
flag.BoolVar(&showIndexFiles, "i", false, "show index.html files in directory listings")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Generate directory index files (recursive is ON, hidden files included by default).\n")
@@ -391,8 +398,9 @@ func main() {
}
}
// Set dry run flag
// Set dry run and show index files flags
opts.DryRun = dryRun
opts.ShowIndexFiles = showIndexFiles
if s3URL != "" {
// Parse S3 URL

View File

@@ -158,15 +158,16 @@ var ExtensionTypes = map[string]string{
}
type Options struct {
TopDir string
Filter string
OutputFile string
DirAppend bool
Recursive bool
IncludeHidden bool
ExcludeRegex *regexp.Regexp
Verbose bool
DryRun bool
TopDir string
Filter string
OutputFile string
DirAppend bool
Recursive bool
IncludeHidden bool
ExcludeRegex *regexp.Regexp
Verbose bool
DryRun bool
ShowIndexFiles bool
}
type FileEntry struct {
@@ -275,7 +276,7 @@ func ReadDirEntries(dirPath string, opts *Options) ([]FileEntry, error) {
for _, file := range files {
fileName := file.Name()
if strings.EqualFold(fileName, opts.OutputFile) {
if !opts.ShowIndexFiles && strings.EqualFold(fileName, opts.OutputFile) {
continue
}

View File

@@ -101,11 +101,13 @@ func TestHTMLTemplate(t *testing.T) {
Entries []FileEntry
DirAppend bool
OutputFile string
IsRoot bool
}{
DirName: "test-dir",
Entries: []FileEntry{},
DirAppend: false,
OutputFile: "index.html",
IsRoot: false,
}
var buf bytes.Buffer
@@ -161,11 +163,13 @@ func TestHTMLTemplateWithEntries(t *testing.T) {
Entries []FileEntry
DirAppend bool
OutputFile string
IsRoot bool
}{
DirName: "test-dir",
Entries: entries,
DirAppend: false,
OutputFile: "index.html",
IsRoot: false,
}
var buf bytes.Buffer

View File

@@ -245,9 +245,9 @@ func TestProcessDirWithDirAppend(t *testing.T) {
htmlContent := string(content)
// Check that directory links include index.html (URL escaped)
if !strings.Contains(htmlContent, "subdir%2Findex.html") {
t.Errorf("Directory links should include index.html when DirAppend is true. Expected subdir%%2Findex.html in content")
// Check that directory links include index.html
if !strings.Contains(htmlContent, "subdir/index.html") {
t.Errorf("Directory links should include index.html when DirAppend is true. Expected subdir/index.html in content")
}
}