This commit is contained in:
Pim van Pelt
2025-12-02 23:50:07 +01:00
parent c53fa96349
commit f8d9841a68
3 changed files with 36 additions and 36 deletions

View File

@@ -158,28 +158,28 @@ var ExtensionTypes = map[string]string{
} }
type Options struct { type Options struct {
TopDir string TopDir string
Filter string Filter string
OutputFile string OutputFile string
DirAppend bool DirAppend bool
Recursive bool Recursive bool
IncludeHidden bool IncludeHidden bool
ExcludeRegex *regexp.Regexp ExcludeRegex *regexp.Regexp
Verbose bool Verbose bool
} }
type FileEntry struct { type FileEntry struct {
Name string Name string
Path string Path string
IsDir bool IsDir bool
IsSymlink bool IsSymlink bool
Size int64 Size int64
ModTime time.Time ModTime time.Time
IconType string IconType string
CSSClass string CSSClass string
SizePretty string SizePretty string
ModTimeISO string ModTimeISO string
ModTimeHuman string ModTimeHuman string
} }
func ProcessDir(topDir string, opts *Options) error { func ProcessDir(topDir string, opts *Options) error {
@@ -193,7 +193,7 @@ func ProcessDir(topDir string, opts *Options) error {
} }
indexPath := filepath.Join(absPath, opts.OutputFile) indexPath := filepath.Join(absPath, opts.OutputFile)
file, err := os.Create(indexPath) file, err := os.Create(indexPath)
if err != nil { if err != nil {
return fmt.Errorf("cannot create file %s: %w", indexPath, err) return fmt.Errorf("cannot create file %s: %w", indexPath, err)
@@ -201,7 +201,7 @@ func ProcessDir(topDir string, opts *Options) error {
defer file.Close() defer file.Close()
dirName := filepath.Base(absPath) dirName := filepath.Base(absPath)
entries, err := ReadDirEntries(absPath, opts) entries, err := ReadDirEntries(absPath, opts)
if err != nil { if err != nil {
return fmt.Errorf("failed to read directory: %w", err) return fmt.Errorf("failed to read directory: %w", err)
@@ -215,14 +215,14 @@ func ProcessDir(topDir string, opts *Options) error {
}) })
templateData := struct { templateData := struct {
DirName string DirName string
Entries []FileEntry Entries []FileEntry
DirAppend bool DirAppend bool
OutputFile string OutputFile string
}{ }{
DirName: dirName, DirName: dirName,
Entries: entries, Entries: entries,
DirAppend: opts.DirAppend, DirAppend: opts.DirAppend,
OutputFile: opts.OutputFile, OutputFile: opts.OutputFile,
} }
@@ -270,7 +270,7 @@ func ReadDirEntries(dirPath string, opts *Options) ([]FileEntry, error) {
} }
fullPath := filepath.Join(dirPath, fileName) fullPath := filepath.Join(dirPath, fileName)
info, err := file.Info() info, err := file.Info()
if err != nil { if err != nil {
fmt.Printf("*** WARNING *** entry %s is not accessible! SKIPPING! Error: %v\n", fullPath, err) fmt.Printf("*** WARNING *** entry %s is not accessible! SKIPPING! Error: %v\n", fullPath, err)
@@ -282,9 +282,9 @@ func ReadDirEntries(dirPath string, opts *Options) ([]FileEntry, error) {
} }
entry := FileEntry{ entry := FileEntry{
Name: fileName, Name: fileName,
Path: fileName, Path: fileName,
IsDir: file.IsDir(), IsDir: file.IsDir(),
ModTime: info.ModTime(), ModTime: info.ModTime(),
} }
@@ -330,11 +330,11 @@ func GetIconType(fileName string) string {
if iconType, exists := ExtensionTypes[ext]; exists { if iconType, exists := ExtensionTypes[ext]; exists {
return iconType return iconType
} }
if iconType, exists := ExtensionTypes[fileName]; exists { if iconType, exists := ExtensionTypes[fileName]; exists {
return iconType return iconType
} }
return "generic" return "generic"
} }
@@ -972,4 +972,4 @@ const htmlTemplateString = `<!DOCTYPE html>
</div> </div>
</main> </main>
</body> </body>
</html>` </html>`

View File

@@ -382,4 +382,4 @@ func TestFileEntryProperties(t *testing.T) {
if entry.SizePretty != PrettySize(int64(len(content))) { if entry.SizePretty != PrettySize(int64(len(content))) {
t.Errorf("Size pretty mismatch") t.Errorf("Size pretty mismatch")
} }
} }

View File

@@ -351,4 +351,4 @@ func TestProcessDirWithSymlinks(t *testing.T) {
if !strings.Contains(htmlContent, "#symlink") { if !strings.Contains(htmlContent, "#symlink") {
t.Error("index.html should contain symlink icon for symlinked file") t.Error("index.html should contain symlink icon for symlinked file")
} }
} }