From 63a62b67548043725f0e3b6b2b3c5bdf64bd0f9b Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Thu, 25 Aug 2016 13:06:22 -0700 Subject: [PATCH] *: add code to serialize templates into go code Add a small program to iterate over the templates directory and generate a go map of name to file data. This is so we can compile these templates into the dex binary instead of requiring files on disk. Also add a Makefile rule to do this automatically. --- Makefile | 5 ++- server/templates_default_gen.go | 79 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 server/templates_default_gen.go diff --git a/Makefile b/Makefile index 20b7ce10..e22ac57d 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ GOARCH=$(shell go env GOARCH) build: bin/dex bin/example-app -bin/dex: FORCE +bin/dex: FORCE server/templates_default.go @go install -ldflags $(LD_FLAGS) $(REPO_PATH)/cmd/dex bin/example-app: FORCE @@ -42,6 +42,9 @@ lint: golint $$package; \ done +server/templates_default.go: $(wildcard web/templates/**) + @go run server/templates_default_gen.go + .PHONY: docker-build docker-build: bin/dex @docker build -t $(DOCKER_IMAGE) . diff --git a/server/templates_default_gen.go b/server/templates_default_gen.go new file mode 100644 index 00000000..0a5ab78a --- /dev/null +++ b/server/templates_default_gen.go @@ -0,0 +1,79 @@ +// +build ignore + +package main + +import ( + "bytes" + "fmt" + "io/ioutil" + "log" + "os/exec" + "path/filepath" +) + +// ignoreFile uses "git check-ignore" to determine if we should ignore a file. +func ignoreFile(p string) (ok bool, err error) { + err = exec.Command("git", "check-ignore", p).Run() + if err == nil { + return true, nil + } + exitErr, ok := err.(*exec.ExitError) + if ok { + if sys := exitErr.Sys(); sys != nil { + e, ok := sys.(interface { + // Is the returned value something that returns an exit status? + ExitStatus() int + }) + if ok && e.ExitStatus() == 1 { + return false, nil + } + } + } + return false, err +} + +type fileData struct { + name string + data string +} + +func main() { + dir, err := ioutil.ReadDir("web/templates") + if err != nil { + log.Fatal(err) + } + files := []fileData{} + for _, file := range dir { + p := filepath.Join("web/templates", file.Name()) + ignore, err := ignoreFile(p) + if err != nil { + log.Fatal(err) + } + if ignore { + continue + } + + data, err := ioutil.ReadFile(p) + if err != nil { + log.Fatal(err) + } + files = append(files, fileData{file.Name(), string(data)}) + } + + f := new(bytes.Buffer) + + fmt.Fprintln(f, "// This file was generated by the makefile. Do not edit.") + fmt.Fprintln(f) + fmt.Fprintln(f, "package server") + fmt.Fprintln(f) + fmt.Fprintln(f, "// defaultTemplates is a key for file name to file data of the files in web/templates.") + fmt.Fprintln(f, "var defaultTemplates = map[string]string{") + for _, file := range files { + fmt.Fprintf(f, "\t%q: %q,\n", file.name, file.data) + } + fmt.Fprintln(f, "}") + + if err := ioutil.WriteFile("server/templates_default.go", f.Bytes(), 0644); err != nil { + log.Fatal(err) + } +}