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

Commit

Permalink
Ensure generated template modtime is Unix epoch
Browse files Browse the repository at this point in the history
vsfgen which we use for maintaining an embedded copy of our templates
does also include the modtime of the files.

This poses however problem in combination with git, as in a project
managed by git the modtime of a file depends on the time it was changed
through a local git action (e.g. a checkout from a different state
which results in a file write on disk) instead of the time of the
actual change.

This commit adds a wrapper around `http.FileSystem` that modifies
the file mod times to always return Unix epoch. So that the only
things resulting in a new `generated_template.gogen.go` are folder
and/or file content changes.
  • Loading branch information
hiddeco committed Sep 24, 2019
1 parent fac59ef commit ce2ba3a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
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 Down Expand Up @@ -54,3 +57,38 @@ func main() {
usage()
}
}

// modTimeFS is a wrapper to ensure written mod times are always in
// Unix epoch. This is to ensure the `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)
}
12 changes: 6 additions & 6 deletions pkg/install/generated_templates.gogen.go

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

0 comments on commit ce2ba3a

Please sign in to comment.