package qver import ( "os" "time" ) // An UpdateFunc updates the version and state variables. It is called // by Version.Get(). If there is an error, it should return the old // version and state along with the error. type UpdateFunc func(version uint32, state interface{}) (uint32, interface{}, error) // UpdateOnFileMod returns a state variable and an UpdateFunc that // bumps the version when a file or directory is modified. The return // values are to be passed to the Update() option. func UpdateOnFileMod(path string) (state interface{}, f UpdateFunc) { return time.Now(), func(ver uint32, state interface{}) (uint32, interface{}, error) { mtime := state.(time.Time) info, err := os.Stat(path) if err != nil { return ver, mtime, err } if info.ModTime().After(mtime) { mtime = info.ModTime() ver++ } return ver, mtime, err } }