|
| 1 | +package vercheck |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | + "go.jetpack.io/devbox/internal/boxcli/usererr" |
| 12 | + "go.jetpack.io/devbox/internal/cloud/envir" |
| 13 | + "go.jetpack.io/devbox/internal/ux" |
| 14 | + "go.jetpack.io/devbox/internal/xdg" |
| 15 | + "golang.org/x/mod/semver" |
| 16 | +) |
| 17 | + |
| 18 | +// Keep this in-sync with latest version in launch.sh. If this version is newer |
| 19 | +// Than the version in launch.sh, we'll print a warning. |
| 20 | +const expectedLauncherVersion = "v0.1.0" |
| 21 | + |
| 22 | +func CheckLauncherVersion(w io.Writer) { |
| 23 | + launcherVersion := os.Getenv("LAUNCHER_VERSION") |
| 24 | + if launcherVersion == "" || envir.IsDevboxCloud() { |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + // If launcherVersion is invalid, this will return 0 and we won't print a warning |
| 29 | + if semver.Compare("v"+launcherVersion, expectedLauncherVersion) < 0 { |
| 30 | + ux.Fwarning( |
| 31 | + w, |
| 32 | + "newer launcher version %s is available (current = v%s), please update "+ |
| 33 | + "using `devbox version update`\n", |
| 34 | + expectedLauncherVersion, |
| 35 | + os.Getenv("LAUNCHER_VERSION"), |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// SelfUpdate updates the devbox launcher and binary. It ignores and deletes the |
| 41 | +// version cache |
| 42 | +func SelfUpdate(stdOut, stdErr io.Writer) error { |
| 43 | + installScript := "" |
| 44 | + if _, err := exec.LookPath("curl"); err == nil { |
| 45 | + installScript = "curl -fsSL https://get.jetpack.io/devbox | bash" |
| 46 | + } else if _, err := exec.LookPath("wget"); err == nil { |
| 47 | + installScript = "wget -qO- https://get.jetpack.io/devbox | bash" |
| 48 | + } else { |
| 49 | + return usererr.New("curl or wget is required to update devbox. Please install either and try again.") |
| 50 | + } |
| 51 | + |
| 52 | + // Delete version cache. Keep this in-sync with whatever logic is in launch.sh |
| 53 | + cacheDir := xdg.CacheSubpath("devbox") |
| 54 | + versionCacheFile := filepath.Join(cacheDir, "latest-version") |
| 55 | + _ = os.Remove(versionCacheFile) |
| 56 | + |
| 57 | + cmd := exec.Command("sh", "-c", installScript) |
| 58 | + cmd.Stdout = stdOut |
| 59 | + cmd.Stderr = stdErr |
| 60 | + if err := cmd.Run(); err != nil { |
| 61 | + return errors.WithStack(err) |
| 62 | + } |
| 63 | + |
| 64 | + fmt.Fprint(stdErr, "Latest version: ") |
| 65 | + exe, err := os.Executable() |
| 66 | + if err != nil { |
| 67 | + return errors.WithStack(err) |
| 68 | + } |
| 69 | + cmd = exec.Command(exe, "version") |
| 70 | + // The output of version is incidental, so just send it all to stdErr |
| 71 | + cmd.Stdout = stdErr |
| 72 | + cmd.Stderr = stdErr |
| 73 | + return errors.WithStack(cmd.Run()) |
| 74 | +} |
0 commit comments