2019-02-04 13:51:29 +03:00
|
|
|
package update
|
|
|
|
|
|
|
|
import (
|
2022-11-02 10:19:24 +03:00
|
|
|
"fmt"
|
2019-02-04 13:51:29 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"time"
|
|
|
|
|
2022-11-02 10:19:24 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
2019-02-04 13:51:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2022-11-02 10:19:24 +03:00
|
|
|
var op errors.Op = "update.writeTimeToFile"
|
2019-02-04 13:51:29 +03:00
|
|
|
err := ioutil.WriteFile(path, []byte(inputTime.Format(timeLayout)), 0644)
|
|
|
|
if err != nil {
|
2022-11-02 10:19:24 +03:00
|
|
|
return errors.E(op, fmt.Errorf("failed writing current time to file: %w", err))
|
2019-02-04 13:51:29 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|