Skip to content

Commit ea9671e

Browse files
authored
Merge pull request #11 from stuartleeks/sl/update-check
Add periodic update check
2 parents 9cf8fdd + ebed3aa commit ea9671e

File tree

4 files changed

+78
-10
lines changed

4 files changed

+78
-10
lines changed

cmd/devcontainer/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"github.com/spf13/cobra"
5+
"github.com/stuartleeks/devcontainer-cli/internal/pkg/update"
56
)
67

78
// Overridden via ldflags
@@ -14,7 +15,12 @@ var (
1415

1516
func main() {
1617

17-
rootCmd := &cobra.Command{Use: "devcontainer"}
18+
rootCmd := &cobra.Command{
19+
Use: "devcontainer",
20+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
21+
update.PeriodicCheckForUpdate(version)
22+
},
23+
}
1824

1925
rootCmd.AddCommand(createCompleteCommand(rootCmd))
2026
rootCmd.AddCommand(createConfigCommand())

cmd/devcontainer/update.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"log"
77
"os"
88

9-
"github.com/blang/semver"
109
"github.com/rhysd/go-github-selfupdate/selfupdate"
1110
"github.com/spf13/cobra"
11+
"github.com/stuartleeks/devcontainer-cli/internal/pkg/update"
1212
)
1313

1414
func createUpdateCommand() *cobra.Command {
@@ -20,19 +20,18 @@ func createUpdateCommand() *cobra.Command {
2020
Use: "update",
2121
Short: "update cli",
2222
Long: "Apply the latest update",
23+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
24+
// do nothing - suppress root PersistentPreRun which does periodic update check
25+
},
2326
Run: func(cmd *cobra.Command, args []string) {
24-
latest, found, err := selfupdate.DetectLatest("stuartleeks/devcontainer-cli")
27+
latest, err := update.CheckForUpdate(version)
2528
if err != nil {
26-
log.Println("Error occurred while detecting version:", err)
29+
log.Println("Error occurred while checking for updates:", err)
2730
return
2831
}
2932

30-
v, err := semver.Parse(version)
31-
if err != nil {
32-
log.Panicln(err.Error())
33-
}
34-
if !found || latest.Version.LTE(v) {
35-
log.Println("Current version is the latest")
33+
if latest == nil {
34+
fmt.Println("No updates available")
3635
return
3736
}
3837

internal/pkg/config/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"fmt"
55
"os"
6+
"time"
67

78
"github.com/spf13/viper"
89
)
@@ -43,6 +44,14 @@ func GetTemplateFolders() []string {
4344
EnsureInitialised()
4445
return viper.GetStringSlice("templatePaths")
4546
}
47+
func GetLastUpdateCheck() time.Time {
48+
EnsureInitialised()
49+
return viper.GetTime("lastUpdateCheck")
50+
}
51+
func SetLastUpdateCheck(t time.Time) {
52+
EnsureInitialised()
53+
viper.Set("lastUpdateCheck", t)
54+
}
4655
func GetAll() map[string]interface{} {
4756
EnsureInitialised()
4857
return viper.AllSettings()

internal/pkg/update/update.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)