Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Ensure generated template modtime is Unix epoch #50

Merged
merged 1 commit into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion pkg/install/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"os"
"time"

"github.com/shurcooL/vfsgen"

Expand All @@ -24,7 +25,9 @@ func main() {
}
switch os.Args[1] {
case "embedded-templates":
var fs http.FileSystem = http.Dir("templates/")
var fs http.FileSystem = modTimeFS{
fs: http.Dir("templates/"),
}
err := vfsgen.Generate(fs, vfsgen.Options{
Filename: "generated_templates.gogen.go",
PackageName: "install",
Expand All @@ -50,3 +53,38 @@ func main() {
usage()
}
}

// modTimeFS is a wrapper that rewrites all mod times to Unix epoch.
// This is to ensure `generated_templates.gogen.go` only changes when
// the folder and/or file contents change.
type modTimeFS struct {
fs http.FileSystem
}

func (fs modTimeFS) Open(name string) (http.File, error) {
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
return modTimeFile{f}, nil
}

type modTimeFile struct {
http.File
}

func (f modTimeFile) Stat() (os.FileInfo, error) {
fi, err := f.File.Stat()
if err != nil {
return nil, err
}
return modTimeFileInfo{fi}, nil
}

type modTimeFileInfo struct {
os.FileInfo
}

func (modTimeFileInfo) ModTime() time.Time {
return time.Unix(0, 0)
}
10 changes: 5 additions & 5 deletions pkg/install/generated_templates.gogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.