embed template file, improve cmd arg handling

This commit is contained in:
ari melody 2025-07-25 17:02:01 +01:00
parent 59230b7ad5
commit d5206362fa
Signed by: ari
GPG key ID: 60B5F0386E3DDB7E

16
main.go
View file

@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
_ "embed"
) )
type ( type (
@ -30,6 +31,9 @@ type (
} }
) )
//go:embed templates/dir.html
var dirHTML string
func main() { func main() {
if len(os.Args) < 2 { printHelp() } if len(os.Args) < 2 { printHelp() }
@ -37,7 +41,7 @@ func main() {
port := 8080 port := 8080
root := "/" root := "/"
filesDir := "." filesDir := ""
i := 1 i := 1
for { for {
if i >= len(os.Args) { break } if i >= len(os.Args) { break }
@ -79,17 +83,25 @@ func main() {
if !strings.HasSuffix(root, "/") { root += "/" } if !strings.HasSuffix(root, "/") { root += "/" }
default: default:
if len(filesDir) > 0 {
fmt.Fprintf(os.Stderr, "unsupported argument: %s\n", os.Args[i])
os.Exit(1)
}
filesDir = os.Args[i] filesDir = os.Args[i]
} }
i++ i++
} }
if len(filesDir) == 0 {
filesDir = "."
}
ignoredFiles := []string{ ignoredFiles := []string{
".", ".",
".DS_Store", ".DS_Store",
} }
dirTemplate, err := template.ParseGlob("./templates/dir.html") dirTemplate, err := template.New("dir").Parse(dirHTML)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "fatal: failed to parse directory template: %v\n", err) fmt.Fprintf(os.Stderr, "fatal: failed to parse directory template: %v\n", err)
os.Exit(1) os.Exit(1)