|
| 1 | +package update |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/blang/semver" |
| 8 | + "github.com/rhysd/go-github-selfupdate/selfupdate" |
| 9 | + "github.com/stuartleeks/devcontainer-cli/internal/pkg/config" |
| 10 | +) |
| 11 | + |
| 12 | +func CheckForUpdate(currentVersion string) (*selfupdate.Release, error) { |
| 13 | + |
| 14 | + latest, found, err := selfupdate.DetectLatest("stuartleeks/devcontainer-cli") |
| 15 | + if err != nil { |
| 16 | + return nil, fmt.Errorf("Error occurred while detecting version: %v", err) |
| 17 | + } |
| 18 | + |
| 19 | + v, err := semver.Parse(currentVersion) |
| 20 | + if err != nil { |
| 21 | + return nil, fmt.Errorf("Error occurred while parsing version: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + if !found || latest.Version.LTE(v) { |
| 25 | + return nil, nil |
| 26 | + } |
| 27 | + return latest, nil |
| 28 | +} |
| 29 | +func PeriodicCheckForUpdate(currentVersion string) { |
| 30 | + const checkInterval time.Duration = 24 * time.Hour |
| 31 | + |
| 32 | + lastCheck := config.GetLastUpdateCheck() |
| 33 | + |
| 34 | + if time.Now().Before(lastCheck.Add(checkInterval)) { |
| 35 | + return |
| 36 | + } |
| 37 | + fmt.Println("Checking for updates...") |
| 38 | + latest, err := CheckForUpdate(currentVersion) |
| 39 | + if err != nil { |
| 40 | + fmt.Printf("Error checking for updates: %s", err) |
| 41 | + } |
| 42 | + |
| 43 | + config.SetLastUpdateCheck(time.Now()) |
| 44 | + if err = config.SaveConfig(); err != nil { |
| 45 | + fmt.Printf("Error saving last update check time: :%s\n", err) |
| 46 | + } |
| 47 | + |
| 48 | + if latest == nil { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + fmt.Printf("\n\n UPDATE AVAILABLE: %s \n \n Release notes: %s\n", latest.Version, latest.ReleaseNotes) |
| 53 | + fmt.Printf("Run `devcontainer udpate` to apply the update\n\n") |
| 54 | +} |
0 commit comments