Skip to content

Commit 88feade

Browse files
authored
chore: use constants defined in envir in version check (#976)
## Summary 1. replace constants with existing envir constants 2. add new envir constants ## How was it tested?
1 parent e40cb0a commit 88feade

File tree

5 files changed

+43
-36
lines changed

5 files changed

+43
-36
lines changed

internal/boxcli/version.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"runtime"
1010

1111
"github.com/spf13/cobra"
12+
1213
"go.jetpack.io/devbox/internal/build"
14+
"go.jetpack.io/devbox/internal/envir"
1315
"go.jetpack.io/devbox/internal/vercheck"
1416
)
1517

@@ -76,13 +78,12 @@ type versionInfo struct {
7678

7779
func getVersionInfo() *versionInfo {
7880
v := &versionInfo{
79-
Version: build.Version,
80-
Platform: fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH),
81-
Commit: build.Commit,
82-
CommitDate: build.CommitDate,
83-
GoVersion: runtime.Version(),
84-
// Change to env.LauncherVersion. Not doing so to minimize merge conflicts.
85-
LauncherVersion: os.Getenv("LAUNCHER_VERSION"),
81+
Version: build.Version,
82+
Platform: fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH),
83+
Commit: build.Commit,
84+
CommitDate: build.CommitDate,
85+
GoVersion: runtime.Version(),
86+
LauncherVersion: os.Getenv(envir.LauncherVersion),
8687
}
8788

8889
return v

internal/envir/env.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ const (
1010
DevboxFeaturePrefix = "DEVBOX_FEATURE_"
1111
DevboxGateway = "DEVBOX_GATEWAY"
1212
DevboxDoNotUpgradeConfig = "DEVBOX_DONT_UPGRADE_CONFIG"
13-
DevboxRegion = "DEVBOX_REGION"
14-
DevboxSearchHost = "DEVBOX_SEARCH_HOST"
15-
DevboxShellEnabled = "DEVBOX_SHELL_ENABLED"
16-
DevboxShellStartTime = "DEVBOX_SHELL_START_TIME"
17-
DevboxVM = "DEVBOX_VM"
13+
// DevboxLatestVersion is the latest version available of the devbox CLI binary.
14+
// NOTE: it should NOT start with v (like 0.4.8)
15+
DevboxLatestVersion = "DEVBOX_LATEST_VERSION"
16+
DevboxRegion = "DEVBOX_REGION"
17+
DevboxSearchHost = "DEVBOX_SEARCH_HOST"
18+
DevboxShellEnabled = "DEVBOX_SHELL_ENABLED"
19+
DevboxShellStartTime = "DEVBOX_SHELL_START_TIME"
20+
DevboxVM = "DEVBOX_VM"
1821

1922
LauncherVersion = "LAUNCHER_VERSION"
23+
LauncherPath = "LAUNCHER_PATH"
2024

2125
SSHTTY = "SSH_TTY"
2226

internal/envir/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strconv"
99
)
1010

11-
func IsCLICloudShell() bool {
11+
func IsCLICloudShell() bool { // TODO: not used any more
1212
cliCloudShell, _ := strconv.ParseBool(os.Getenv(devboxCLICloudShell))
1313
return cliCloudShell
1414
}

internal/vercheck/vercheck.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,28 @@ import (
2020
"go.jetpack.io/devbox/internal/boxcli/usererr"
2121
"go.jetpack.io/devbox/internal/build"
2222
"go.jetpack.io/devbox/internal/cmdutil"
23+
"go.jetpack.io/devbox/internal/envir"
2324
"go.jetpack.io/devbox/internal/ux"
2425
"go.jetpack.io/devbox/internal/xdg"
2526
)
2627

27-
// Keep this in-sync with latest version in launch.sh. If this version is newer
28-
// than the version in launch.sh, we'll print a notice.
28+
// Keep this in-sync with latest version in launch.sh.
29+
// If this version is newer than the version in launch.sh, we'll print a notice.
2930
const expectedLauncherVersion = "v0.2.0"
3031

3132
// currentDevboxVersion is the version of the devbox CLI binary that is currently running.
32-
// We use this variable so we can mock it in tests.
33+
// We use this variable so that we can mock it in tests.
3334
var currentDevboxVersion = build.Version
3435

35-
// envDevboxLatestVersion is the latest version available of the devbox CLI binary.
36-
// Change to env.DevboxLatestVersion. Not doing so to minimize merge conflicts.
37-
var envDevboxLatestVersion = "DEVBOX_LATEST_VERSION"
38-
3936
// CheckVersion checks the launcher and binary versions and prints a notice if
4037
// they are out of date.
4138
func CheckVersion(w io.Writer) {
4239

43-
// Replace with envir.IsDevboxCloud(). Not doing so to minimize merge conflicts.
44-
if os.Getenv("DEVBOX_REGION") != "" {
40+
if envir.IsDevboxCloud() {
4541
return
4642
}
4743

44+
// check launcher version
4845
launcherNotice := launcherVersionNotice()
4946
if launcherNotice != "" {
5047
// TODO: use ux.FNotice
@@ -53,6 +50,7 @@ func CheckVersion(w io.Writer) {
5350
// fallthrough to alert the user about a new Devbox CLI binary being possibly available
5451
}
5552

53+
// check devbox CLI version
5654
devboxNotice := devboxVersionNotice()
5755
if devboxNotice != "" {
5856
// TODO: use ux.FNotice
@@ -139,7 +137,7 @@ type updatedVersions struct {
139137
// devbox versions.
140138
func triggerUpdate(stdErr io.Writer) (*updatedVersions, error) {
141139

142-
exePath := os.Getenv("LAUNCHER_PATH")
140+
exePath := os.Getenv(envir.LauncherPath)
143141
if exePath == "" {
144142
ux.Fwarning(stdErr, "expected LAUNCHER_PATH to be set. Defaulting to \"devbox\".")
145143
exePath = "devbox"
@@ -227,8 +225,7 @@ func isNewDevboxAvailable() bool {
227225
// currentLauncherAvailable returns launcher's version if it is
228226
// available, or empty string if it is not.
229227
func currentLauncherVersion() string {
230-
// Change to envir.LauncherVersion. Not doing so to minimize merge-conflicts.
231-
launcherVersion := os.Getenv("LAUNCHER_VERSION")
228+
launcherVersion := os.Getenv(envir.LauncherVersion)
232229
if launcherVersion == "" {
233230
return ""
234231
}
@@ -265,7 +262,7 @@ func semverCompare(ver1, ver2 string) int {
265262

266263
// latestVersion returns the latest version available for the binary.
267264
func latestVersion() string {
268-
version := os.Getenv(envDevboxLatestVersion)
265+
version := os.Getenv(envir.DevboxLatestVersion)
269266
if version == "" {
270267
return ""
271268
}

internal/vercheck/vercheck_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1+
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
2+
// Use of this source code is governed by the license in the LICENSE file.
3+
14
package vercheck
25

36
import (
47
"bytes"
58
"strings"
69
"testing"
10+
11+
"go.jetpack.io/devbox/internal/envir"
712
)
813

914
func TestCheckVersion(t *testing.T) {
1015

1116
t.Run("no_devbox_cloud", func(t *testing.T) {
1217
// if devbox cloud
13-
t.Setenv("DEVBOX_REGION", "true")
18+
t.Setenv(envir.DevboxRegion, "true")
1419
buf := new(bytes.Buffer)
1520
CheckVersion(buf)
1621
if buf.String() != "" {
1722
t.Errorf("expected empty string, got %q", buf.String())
1823
}
19-
t.Setenv("DEVBOX_REGION", "")
24+
t.Setenv(envir.DevboxRegion, "")
2025
})
2126

22-
// no envir.LauncherVersion or latest-version env var
27+
// no launcher version or latest-version env var
2328
t.Run("no_launcher_version_or_latest_version", func(t *testing.T) {
24-
t.Setenv("LAUNCHER_VERSION", "")
25-
t.Setenv(envDevboxLatestVersion, "")
29+
t.Setenv(envir.LauncherVersion, "")
30+
t.Setenv(envir.DevboxLatestVersion, "")
2631
buf := new(bytes.Buffer)
2732
CheckVersion(buf)
2833
if buf.String() != "" {
@@ -32,7 +37,7 @@ func TestCheckVersion(t *testing.T) {
3237

3338
t.Run("launcher_version_outdated", func(t *testing.T) {
3439
// set older launcher version
35-
t.Setenv("LAUNCHER_VERSION", "v0.1.0")
40+
t.Setenv(envir.LauncherVersion, "v0.1.0")
3641

3742
buf := new(bytes.Buffer)
3843
CheckVersion(buf)
@@ -43,10 +48,10 @@ func TestCheckVersion(t *testing.T) {
4348

4449
t.Run("binary_version_outdated", func(t *testing.T) {
4550
// set the launcher version so that it is not outdated
46-
t.Setenv("LAUNCHER_VERSION", strings.TrimPrefix(expectedLauncherVersion, "v"))
51+
t.Setenv(envir.LauncherVersion, strings.TrimPrefix(expectedLauncherVersion, "v"))
4752

4853
// set the latest version to be greater the current binary version
49-
t.Setenv(envDevboxLatestVersion, "0.4.9")
54+
t.Setenv(envir.DevboxLatestVersion, "0.4.9")
5055

5156
// mock the existing binary version
5257
currentDevboxVersion = "v0.4.8"
@@ -61,13 +66,13 @@ func TestCheckVersion(t *testing.T) {
6166
t.Run("all_versions_up_to_date", func(t *testing.T) {
6267

6368
// set the launcher version so that it is not outdated
64-
t.Setenv("LAUNCHER_VERSION", strings.TrimPrefix(expectedLauncherVersion, "v"))
69+
t.Setenv(envir.LauncherVersion, strings.TrimPrefix(expectedLauncherVersion, "v"))
6570

6671
// mock the existing binary version
6772
currentDevboxVersion = "v0.4.8"
6873

6974
// set the latest version to the same as the current binary version
70-
t.Setenv(envDevboxLatestVersion, "0.4.8")
75+
t.Setenv(envir.DevboxLatestVersion, "0.4.8")
7176

7277
buf := new(bytes.Buffer)
7378
CheckVersion(buf)

0 commit comments

Comments
 (0)