Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shihyuho committed Oct 15, 2019
1 parent 3ffc814 commit 6e66b5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
7 changes: 6 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ func (c *ConfFile) UpdateCleanupTime() {

// UpdateCheckUpdatesTime updates check updates time
func (c *ConfFile) UpdateCheckUpdatesTime() {
c.CheckUpdates = time.Now().AddDate(0, 0, CheckUpdatesDueDays)
c.UpdateCheckUpdatesTimeInDays(CheckUpdatesDueDays)
}

// UpdateCheckUpdatesTimeInDays updates check updates time
func (c *ConfFile) UpdateCheckUpdatesTimeInDays(days int) {
c.CheckUpdates = time.Now().AddDate(0, 0, days)
}

// LoadConfFile return a pointer of a ConfFile which read from path
Expand Down
21 changes: 13 additions & 8 deletions pkg/environment/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,41 @@ func CheckForUpdates(log *logrus.Logger, home paths.Home, currentVersion string,
return nil
}
}
if err := checkOnline(log, currentVersion); err != nil {
if updateAvailable, err := checkOnline(log, currentVersion); err != nil {
return err
} else if updateAvailable {
conf.UpdateCheckUpdatesTimeInDays(1) // 如果已經發現可以更新, 一天後需要檢查是否更新了沒
} else {
conf.UpdateCheckUpdatesTime() // 如果沒有任何更新檔, 就可以等久一點再檢查吧
}
conf.UpdateCheckUpdatesTime()
return conf.WriteFile(home.ConfigFile(), 0644)
}

func needsToCheckOnline(dueDate time.Time) bool {
return dueDate.Before(time.Now())
}

func checkOnline(log *logrus.Logger, currentVersion string) error {
func checkOnline(log *logrus.Logger, currentVersion string) (bool, error) {
log.Println("Checking for latest slctl version...")
client := github.NewClient(nil)
ctx := context.Background()
log.Debugf("fetching the latest published release from github.com/%s/%s", owner, repo)
rr, _, err := client.Repositories.GetLatestRelease(ctx, owner, repo)
if err != nil {
return err
return false, err
}
latest := rr.GetTagName()
log.Debugf("found latest version: %s+%s", latest, rr.GetTargetCommitish())
if updateAvailable, err := ver.Revision(latest).IsGreaterThan(currentVersion); err != nil {
return err
} else if updateAvailable {
updateAvailable, err := ver.Revision(latest).IsGreaterThan(currentVersion)
if err != nil {
return false, err
}
if updateAvailable {
log.Printf(`A new version %q is available
It is recommended using package managers to update
Read more: https://github.com/softleader/slctl#upgrade`, latest)
} else {
log.Println("No update available")
}
return nil
return updateAvailable, nil
}

0 comments on commit 6e66b5b

Please sign in to comment.