diff --git a/Makefile b/Makefile index 1a664e1a..28b53c01 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ GIT_COMMIT = $(shell git rev-parse HEAD 2> /dev/null || echo unknown) CLC_VERSION ?= v0.0.0-CUSTOMBUILD -IS_CHECK_VERSION ?= enabled -LDFLAGS = "-s -w -X 'github.com/hazelcast/hazelcast-commandline-client/internal.GitCommit=$(GIT_COMMIT)' -X 'github.com/hazelcast/hazelcast-commandline-client/internal.Version=$(CLC_VERSION)' -X 'github.com/hazelcast/hazelcast-go-client/internal.ClientType=CLC' -X 'github.com/hazelcast/hazelcast-go-client/internal.ClientVersion=$(CLC_VERSION)' -X 'github.com/hazelcast/hazelcast-commandline-client/internal.IsCheckVersion=$(ENABLE_CHECK_VERSION)'" +CLC_SKIP_UPDATE_CHECK ?= 0 +LDFLAGS = "-s -w -X 'github.com/hazelcast/hazelcast-commandline-client/internal.GitCommit=$(GIT_COMMIT)' -X 'github.com/hazelcast/hazelcast-commandline-client/internal.Version=$(CLC_VERSION)' -X 'github.com/hazelcast/hazelcast-go-client/internal.ClientType=CLC' -X 'github.com/hazelcast/hazelcast-go-client/internal.ClientVersion=$(CLC_VERSION)' -X 'github.com/hazelcast/hazelcast-commandline-client/internal.SkipUpdateCheck=$(CLC_SKIP_UPDATE_CHECK)'" MAIN_CMD_HELP ?= Hazelcast CLC LDFLAGS = -s -w -X 'github.com/hazelcast/hazelcast-commandline-client/clc/cmd.MainCommandShortHelp=$(MAIN_CMD_HELP)' -X 'github.com/hazelcast/hazelcast-commandline-client/internal.GitCommit=$(GIT_COMMIT)' -X 'github.com/hazelcast/hazelcast-commandline-client/internal.Version=$(CLC_VERSION)' -X 'github.com/hazelcast/hazelcast-go-client/internal.ClientType=CLC' -X 'github.com/hazelcast/hazelcast-go-client/internal.ClientVersion=$(CLC_VERSION)' TEST_FLAGS ?= -count 1 -timeout 30m -race diff --git a/base/commands/update_check.go b/base/commands/update_check.go index 721f10d7..adf75dd6 100644 --- a/base/commands/update_check.go +++ b/base/commands/update_check.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "strconv" "strings" "time" @@ -11,7 +12,6 @@ import ( "github.com/hazelcast/hazelcast-commandline-client/clc/paths" "github.com/hazelcast/hazelcast-commandline-client/clc/store" "github.com/hazelcast/hazelcast-commandline-client/internal" - . "github.com/hazelcast/hazelcast-commandline-client/internal/check" "github.com/hazelcast/hazelcast-commandline-client/internal/plug" ) @@ -25,40 +25,50 @@ https://github.com/hazelcast/hazelcast-commandline-client/releases/%s ` -const updateCheckKey = "update.nextCheckTime" - -const checkInterval = time.Hour * 24 * 7 +const ( + updateCheckKey = "update.nextCheckTime" + updateVersionKey = "update.latestVersion" + checkInterval = time.Hour * 24 * 7 +) func maybePrintNewerVersion(ec plug.ExecContext) error { - sa := store.NewStoreAccessor(paths.Store(), ec.Logger()) - isSkip, err := isSkipNewerVersion(sa) - if err != nil { - return err - } - if isSkip { - return nil - } - v, err := internal.LatestReleaseVersion() + sa := store.NewStoreAccessor(filepath.Join(paths.Caches(), "version_update"), ec.Logger()) + shouldSkip, err := shouldSkipNewerVersion(sa) if err != nil { return err } - if v != "" && internal.CheckVersion(trimVersion(v), ">", trimVersion(internal.Version)) { - I2(fmt.Fprintf(ec.Stdout(), newVersionWarning, v)) + var latest string + if shouldSkip { + v, err := sa.WithLock(func(s *store.Store) (any, error) { + return s.GetEntry([]byte(updateVersionKey)) + }) + if err != nil { + return err + } + latest = string(v.([]byte)) + } else { + latest, err = internal.LatestReleaseVersion() + if err != nil { + return err + } + if err = updateVersionAndNextCheckTime(sa, latest); err != nil { + return err + } } - if err = updateNextCheckTime(sa); err != nil { - return err + if latest != "" && internal.CheckVersion(trimVersion(latest), ">", trimVersion(internal.Version)) { + ec.PrintlnUnnecessary(fmt.Sprintf(newVersionWarning, latest)) } return nil } -func isSkipNewerVersion(sa *store.StoreAccessor) (bool, error) { +func shouldSkipNewerVersion(sa *store.StoreAccessor) (bool, error) { if internal.Version == internal.UnknownVersion { return true, nil } if strings.Contains(internal.Version, internal.CustomBuildSuffix) { return true, nil } - if internal.IsCheckVersion == "disabled" { + if internal.SkipUpdateCheck == "1" { return true, nil } if os.Getenv(skipUpdateCheck) == "1" { @@ -89,10 +99,14 @@ func trimVersion(v string) string { return strings.TrimPrefix(strings.Split(v, "-")[0], "v") } -func updateNextCheckTime(sa *store.StoreAccessor) error { +func updateVersionAndNextCheckTime(sa *store.StoreAccessor, v string) error { _, err := sa.WithLock(func(s *store.Store) (any, error) { - return nil, s.SetEntry([]byte(updateCheckKey), + err := s.SetEntry([]byte(updateCheckKey), []byte(strconv.FormatInt(time.Now().Add(checkInterval).Unix(), 10))) + if err != nil { + return nil, err + } + return nil, s.SetEntry([]byte(updateVersionKey), []byte(v)) }) return err } diff --git a/clc/paths/paths.go b/clc/paths/paths.go index 29826e4b..8b5320ca 100644 --- a/clc/paths/paths.go +++ b/clc/paths/paths.go @@ -51,10 +51,6 @@ func Templates() string { return filepath.Join(Home(), "templates") } -func Store() string { - return filepath.Join(Home(), "store") -} - func Caches() string { return filepath.Join(Home(), "caches") } diff --git a/docs/modules/ROOT/pages/environment-variables.adoc b/docs/modules/ROOT/pages/environment-variables.adoc index 24fefaeb..7c966613 100644 --- a/docs/modules/ROOT/pages/environment-variables.adoc +++ b/docs/modules/ROOT/pages/environment-variables.adoc @@ -63,6 +63,10 @@ The following environment variables are experimental and may be removed in a fut |Disables color output if set to `1`. |`0` (false) +|CLC_SKIP_UPDATE_CHECK +|Disables checking new version of CLC. +|`0` (false) + |=== diff --git a/internal/version.go b/internal/version.go index cd2670bf..4ec6e9dd 100644 --- a/internal/version.go +++ b/internal/version.go @@ -11,9 +11,9 @@ const CustomBuildSuffix = "CUSTOMBUILD" // being initialized at compile-time. var ( - GitCommit string - Version = UnknownVersion - IsCheckVersion = "disabled" + GitCommit string + Version = UnknownVersion + SkipUpdateCheck = "0" ) // CheckVersion checks whether left OP right condition holds.