From 04912c04e3a8ea3dea21b7e26f9534f7742fd69f Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Mon, 5 Sep 2016 18:06:17 -0700 Subject: [PATCH] server: generate string literals instead of escaped strings When compiling the default templates into the source code, use string literals instead of escaped strings to reduce merge conflicts. --- server/templates.go | 4 + server/templates_default.go | 348 +++++++++++++++++++++++++++++++- server/templates_default_gen.go | 8 +- 3 files changed, 354 insertions(+), 6 deletions(-) diff --git a/server/templates.go b/server/templates.go index a0d1ce17..45d75c84 100644 --- a/server/templates.go +++ b/server/templates.go @@ -27,6 +27,10 @@ var requiredTmpls = []string{ // TemplateConfig describes. type TemplateConfig struct { + // TODO(ericchiang): Asking for a directory with a set of templates doesn't indicate + // what the templates should look like and doesn't allow consumers of this package to + // provide their own templates in memory. In the future clean this up. + // Directory of the templates. If empty, these will be loaded from memory. Dir string `yaml:"dir"` diff --git a/server/templates_default.go b/server/templates_default.go index fc272675..63b5aaa9 100644 --- a/server/templates_default.go +++ b/server/templates_default.go @@ -4,9 +4,347 @@ package server // defaultTemplates is a key for file name to file data of the files in web/templates. var defaultTemplates = map[string]string{ - "approval.html": "{{ template \"header.html\" . }}\n\n
\n

Grant Access

\n\n
\n
\n
{{ .Client }} would like to:
\n {{ range $scope := .Scopes }}\n
  • \n
    \n {{ $scope }}\n
    \n
  • \n {{ end }}\n
    \n
    \n\n
    \n
    \n
    \n \n \n \n
    \n
    \n
    \n
    \n \n \n \n
    \n
    \n
    \n\n
    \n\n{{ template \"footer.html\" . }}\n", - "footer.html": " \n \n\n", - "header.html": "\n\n \n \n \n {{ .Issuer }}\n \n \n \n\n \n
    \n
    \n \n
    \n
    \n\n
    \n\n", - "login.html": "{{ template \"header.html\" . }}\n\n
    \n

    Log in to {{ .Issuer }}

    \n\n
    \n {{ range $c := .Connectors }}\n \n {{ end }}\n
    \n\n
    \n\n\n{{ template \"footer.html\" . }}\n", - "password.html": "{{ template \"header.html\" . }}\n\n
    \n

    Log in to Your Account

    \n
    \n
    \n
    \n \n
    \n\t \n
    \n
    \n
    \n \n
    \n\t \n
    \n \n\n {{ if .Invalid }}\n
    \n Invalid username and password.\n
    \n {{ end }}\n\n \n\n
    \n
    \n\n{{ template \"footer.html\" . }}\n", + "approval.html": `{{ template "header.html" . }} + +
    +

    Grant Access

    + +
    +
    +
    {{ .Client }} would like to:
    + {{ range $scope := .Scopes }} +
  • +
    + {{ $scope }} +
    +
  • + {{ end }} +
    +
    + +
    +
    +
    + + + +
    +
    +
    +
    + + + +
    +
    +
    + +
    + +{{ template "footer.html" . }} +`, + "footer.html": `
    + + +`, + "header.html": ` + + + + + {{ .Issuer }} + + + + + + + +
    + +`, + "login.html": `{{ template "header.html" . }} + +
    +

    Log in to {{ .Issuer }}

    + +
    + {{ range $c := .Connectors }} + + {{ end }} +
    + +
    + + +{{ template "footer.html" . }} +`, + "password.html": `{{ template "header.html" . }} + +
    +

    Log in to Your Account

    +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    + + + {{ if .Invalid }} +
    + Invalid username and password. +
    + {{ end }} + + + +
    +
    + +{{ template "footer.html" . }} +`, } diff --git a/server/templates_default_gen.go b/server/templates_default_gen.go index 0a5ab78a..0e46df78 100644 --- a/server/templates_default_gen.go +++ b/server/templates_default_gen.go @@ -32,12 +32,15 @@ func ignoreFile(p string) (ok bool, err error) { return false, err } +// Maps aren't deterministic, use a struct instead. + type fileData struct { name string data string } func main() { + // ReadDir guarentees result in sorted order. dir, err := ioutil.ReadDir("web/templates") if err != nil { log.Fatal(err) @@ -57,6 +60,9 @@ func main() { if err != nil { log.Fatal(err) } + if bytes.Contains(data, []byte{'`'}) { + log.Fatalf("file %s contains escape character '`' and cannot be compiled into go source", p) + } files = append(files, fileData{file.Name(), string(data)}) } @@ -69,7 +75,7 @@ func main() { 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.Fprintf(f, "\t%q: `%s`,\n", file.name, file.data) } fmt.Fprintln(f, "}")