mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
52f21e7261
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6524 Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com> GitOrigin-RevId: 919262c2618a1319441ce3730802c06bd5bd450a
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package update
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"time"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
|
)
|
|
|
|
const (
|
|
autoCheckInterval = 2 * time.Hour
|
|
timeLayout = time.RFC1123
|
|
)
|
|
|
|
func getTimeFromFileIfExists(path string) time.Time {
|
|
lastUpdateCheckTime, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return time.Time{}
|
|
}
|
|
timeInFile, err := time.Parse(timeLayout, string(lastUpdateCheckTime))
|
|
if err != nil {
|
|
return time.Time{}
|
|
}
|
|
return timeInFile
|
|
}
|
|
|
|
func writeTimeToFile(path string, inputTime time.Time) error {
|
|
var op errors.Op = "update.writeTimeToFile"
|
|
err := ioutil.WriteFile(path, []byte(inputTime.Format(timeLayout)), 0644)
|
|
if err != nil {
|
|
return errors.E(op, fmt.Errorf("failed writing current time to file: %w", err))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ShouldRunCheck checks the file f for a timestamp and returns true
|
|
// if the last update check was >= autoCheckInterval .
|
|
func ShouldRunCheck(f string) bool {
|
|
lastUpdateTime := getTimeFromFileIfExists(f)
|
|
return time.Since(lastUpdateTime) >= autoCheckInterval
|
|
}
|