Skip to content

Commit 336637a

Browse files
committed
Split last update check out from config
Avoids updates to the config file for internal state tracking
1 parent 774487c commit 336637a

File tree

3 files changed

+89
-16
lines changed

3 files changed

+89
-16
lines changed

internal/pkg/config/config.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7-
"time"
87

9-
"github.com/spf13/viper"
8+
viperlib "github.com/spf13/viper"
109
)
1110

1211
var initialised bool = false
12+
var viper *viperlib.Viper = viperlib.New()
1313

1414
// EnsureInitialised reads the config. Will quit if config is invalid
1515
func EnsureInitialised() {
@@ -25,7 +25,7 @@ func EnsureInitialised() {
2525

2626
// TODO - allow env var for config
2727
if err := viper.ReadInConfig(); err != nil {
28-
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
28+
if _, ok := err.(viperlib.ConfigFileNotFoundError); ok {
2929
// Config file not found; ignore error if desired
3030
} else {
3131
fmt.Printf("Error loading config file: %s\n", err)
@@ -36,7 +36,10 @@ func EnsureInitialised() {
3636
}
3737
}
3838
func getConfigPath() string {
39-
var path string
39+
path := os.Getenv("DEVCONTAINERX_CONFIG_PATH")
40+
if path != "" {
41+
return path
42+
}
4043
if os.Getenv("HOME") != "" {
4144
path = filepath.Join("$HOME", ".devcontainer-cli/")
4245
} else {
@@ -58,14 +61,6 @@ func GetExperimentalFeaturesEnabled() bool {
5861
EnsureInitialised()
5962
return viper.GetBool("experimental")
6063
}
61-
func GetLastUpdateCheck() time.Time {
62-
EnsureInitialised()
63-
return viper.GetTime("lastUpdateCheck")
64-
}
65-
func SetLastUpdateCheck(t time.Time) {
66-
EnsureInitialised()
67-
viper.Set("lastUpdateCheck", t)
68-
}
6964
func GetAll() map[string]interface{} {
7065
EnsureInitialised()
7166
return viper.AllSettings()

internal/pkg/status/status.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package status
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"time"
8+
9+
viperlib "github.com/spf13/viper"
10+
)
11+
12+
var initialised bool = false
13+
var viper *viperlib.Viper = viperlib.New()
14+
15+
// EnsureInitialised reads the config. Will quit if config is invalid
16+
func EnsureInitialised() {
17+
if !initialised {
18+
viper.SetConfigName("devcontainer-cli-status")
19+
viper.SetConfigType("json")
20+
21+
viper.AddConfigPath(getConfigPath())
22+
23+
// TODO - allow env var for config
24+
if err := viper.ReadInConfig(); err != nil {
25+
if _, ok := err.(viperlib.ConfigFileNotFoundError); ok {
26+
// Config file not found; ignore error if desired
27+
} else {
28+
fmt.Printf("Error loading status file: %s\n", err)
29+
os.Exit(1)
30+
}
31+
}
32+
initialised = true
33+
}
34+
}
35+
func getConfigPath() string {
36+
path := os.Getenv("DEVCONTAINERX_STATUS_PATH")
37+
if path != "" {
38+
return path
39+
}
40+
if os.Getenv("HOME") != "" {
41+
path = filepath.Join("$HOME", ".devcontainer-cli/")
42+
} else {
43+
// if HOME not set, assume Windows and use USERPROFILE env var
44+
path = filepath.Join("$USERPROFILE", ".devcontainer-cli/")
45+
}
46+
return os.ExpandEnv(path)
47+
}
48+
49+
func GetLastUpdateCheck() time.Time {
50+
EnsureInitialised()
51+
return viper.GetTime("lastUpdateCheck")
52+
}
53+
func SetLastUpdateCheck(t time.Time) {
54+
EnsureInitialised()
55+
viper.Set("lastUpdateCheck", t)
56+
}
57+
func GetAll() map[string]interface{} {
58+
EnsureInitialised()
59+
return viper.AllSettings()
60+
}
61+
62+
func SaveStatus() error {
63+
EnsureInitialised()
64+
configPath := getConfigPath()
65+
configPath = os.ExpandEnv(configPath)
66+
if err := os.MkdirAll(configPath, 0755); err != nil {
67+
return err
68+
}
69+
configFilePath := filepath.Join(configPath, "devcontainer-cli-status.json")
70+
fmt.Printf("HERE: %q\n", configFilePath)
71+
return viper.WriteConfigAs(configFilePath)
72+
}

internal/pkg/update/update.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package update
22

33
import (
44
"fmt"
5+
"os"
56
"time"
67

78
"github.com/blang/semver"
89
"github.com/rhysd/go-github-selfupdate/selfupdate"
9-
"github.com/stuartleeks/devcontainer-cli/internal/pkg/config"
10+
"github.com/stuartleeks/devcontainer-cli/internal/pkg/status"
1011
)
1112

1213
func CheckForUpdate(currentVersion string) (*selfupdate.Release, error) {
@@ -29,7 +30,12 @@ func CheckForUpdate(currentVersion string) (*selfupdate.Release, error) {
2930
func PeriodicCheckForUpdate(currentVersion string) {
3031
const checkInterval time.Duration = 24 * time.Hour
3132

32-
lastCheck := config.GetLastUpdateCheck()
33+
if os.Getenv("DEVCONTAINERX_SKIP_UPDATE") != "" {
34+
// Skip update check
35+
return
36+
}
37+
38+
lastCheck := status.GetLastUpdateCheck()
3339

3440
if time.Now().Before(lastCheck.Add(checkInterval)) {
3541
return
@@ -40,8 +46,8 @@ func PeriodicCheckForUpdate(currentVersion string) {
4046
fmt.Printf("Error checking for updates: %s", err)
4147
}
4248

43-
config.SetLastUpdateCheck(time.Now())
44-
if err = config.SaveConfig(); err != nil {
49+
status.SetLastUpdateCheck(time.Now())
50+
if err = status.SaveStatus(); err != nil {
4551
fmt.Printf("Error saving last update check time: :%s\n", err)
4652
}
4753

0 commit comments

Comments
 (0)