Testing out the tools/cmd pattern with go get tool
This commit is contained in:
parent
f476605ecf
commit
3939a54513
2 changed files with 11 additions and 54 deletions
56
main.go
56
main.go
|
|
@ -2,10 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
|
||||||
"io/fs"
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
@ -18,57 +14,9 @@ import (
|
||||||
|
|
||||||
"zedshaw.games/webapp/api"
|
"zedshaw.games/webapp/api"
|
||||||
"zedshaw.games/webapp/data"
|
"zedshaw.games/webapp/data"
|
||||||
|
"zedshaw.games/webapp/zed"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Fail(err error, format string, v ...any) error {
|
|
||||||
err_format := fmt.Sprintf("ERROR: %v; %s", err, format)
|
|
||||||
log.Printf(err_format, v...)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func RenderPages(pages_path string, target string) {
|
|
||||||
engine := html.New(pages_path, ".html")
|
|
||||||
engine.Load()
|
|
||||||
|
|
||||||
err := filepath.WalkDir(pages_path,
|
|
||||||
func(path string, d fs.DirEntry, err error) error {
|
|
||||||
if !d.IsDir() {
|
|
||||||
if err != nil { return Fail(err, "path: %s", path); }
|
|
||||||
|
|
||||||
dir := filepath.Dir(path)
|
|
||||||
err = os.MkdirAll(dir, 0750)
|
|
||||||
if err != nil {
|
|
||||||
return Fail(err, "making dir %s", dir);
|
|
||||||
}
|
|
||||||
|
|
||||||
split_path := strings.Split(path, string(os.PathSeparator))[1:]
|
|
||||||
source_name := strings.Join(split_path, "/") // Render wants / even on windows
|
|
||||||
ext := filepath.Ext(source_name)
|
|
||||||
template_name, found := strings.CutSuffix(source_name, ext)
|
|
||||||
|
|
||||||
if found && ext == ".html" {
|
|
||||||
prefixed_path := append([]string{target}, split_path...)
|
|
||||||
|
|
||||||
target_path := filepath.Join(prefixed_path...)
|
|
||||||
|
|
||||||
// compare time stamps and skip if not newer
|
|
||||||
|
|
||||||
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE, 0644)
|
|
||||||
if err != nil { return Fail(err, "writing file %s", target_path) }
|
|
||||||
|
|
||||||
err = engine.Render(out, template_name, fiber.Map{"Title": "Hello"})
|
|
||||||
if err != nil { return Fail(err, "failed to render %s", path) }
|
|
||||||
|
|
||||||
log.Printf("RENDER: %s -> %s", template_name, target_path)
|
|
||||||
out.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil { log.Fatalf("can't walk content") }
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
|
|
@ -82,7 +30,7 @@ func main() {
|
||||||
StrictRouting: true,
|
StrictRouting: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
RenderPages("./pages", "./public")
|
zed.RenderPages("./pages", "./public")
|
||||||
|
|
||||||
app.Use(logger.New())
|
app.Use(logger.New())
|
||||||
app.Use(recov.New())
|
app.Use(recov.New())
|
||||||
|
|
|
||||||
9
tools/cmd/dostuff.go
Normal file
9
tools/cmd/dostuff.go
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Println("I DO THING!")
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue