embed template file, improve cmd arg handling
This commit is contained in:
parent
59230b7ad5
commit
d5206362fa
1 changed files with 14 additions and 2 deletions
16
main.go
16
main.go
|
@ -12,6 +12,7 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -30,6 +31,9 @@ type (
|
|||
}
|
||||
)
|
||||
|
||||
//go:embed templates/dir.html
|
||||
var dirHTML string
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 { printHelp() }
|
||||
|
||||
|
@ -37,7 +41,7 @@ func main() {
|
|||
port := 8080
|
||||
root := "/"
|
||||
|
||||
filesDir := "."
|
||||
filesDir := ""
|
||||
i := 1
|
||||
for {
|
||||
if i >= len(os.Args) { break }
|
||||
|
@ -79,17 +83,25 @@ func main() {
|
|||
if !strings.HasSuffix(root, "/") { root += "/" }
|
||||
|
||||
default:
|
||||
if len(filesDir) > 0 {
|
||||
fmt.Fprintf(os.Stderr, "unsupported argument: %s\n", os.Args[i])
|
||||
os.Exit(1)
|
||||
}
|
||||
filesDir = os.Args[i]
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
if len(filesDir) == 0 {
|
||||
filesDir = "."
|
||||
}
|
||||
|
||||
ignoredFiles := []string{
|
||||
".",
|
||||
".DS_Store",
|
||||
}
|
||||
|
||||
dirTemplate, err := template.ParseGlob("./templates/dir.html")
|
||||
dirTemplate, err := template.New("dir").Parse(dirHTML)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fatal: failed to parse directory template: %v\n", err)
|
||||
os.Exit(1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue