-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67d1845
commit b2ae08d
Showing
224 changed files
with
53,707 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/loft-sh/devpod/pkg/upgrade" | ||
"github.com/loft-sh/log" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// UpgradeCmd is a struct that defines a command call for "upgrade" | ||
type UpgradeCmd struct { | ||
log log.Logger | ||
Version string | ||
} | ||
|
||
// NewUpgradeCmd creates a new upgrade command | ||
func NewUpgradeCmd() *cobra.Command { | ||
cmd := &UpgradeCmd{log: log.GetInstance()} | ||
upgradeCmd := &cobra.Command{ | ||
Use: "upgrade", | ||
Short: "Upgrade the DevPod CLI to the newest version", | ||
Args: cobra.NoArgs, | ||
RunE: cmd.Run, | ||
} | ||
|
||
upgradeCmd.Flags().StringVar(&cmd.Version, "version", "", "The version to update to. Defaults to the latest stable version available") | ||
return upgradeCmd | ||
} | ||
|
||
// Run executes the command logic | ||
func (cmd *UpgradeCmd) Run(*cobra.Command, []string) error { | ||
err := upgrade.Upgrade(cmd.Version, cmd.log) | ||
if err != nil { | ||
return errors.Errorf("unable to upgrade: %v", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package upgrade | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
versionpkg "github.com/loft-sh/devpod/pkg/version" | ||
"github.com/loft-sh/log" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/blang/semver" | ||
"github.com/rhysd/go-github-selfupdate/selfupdate" | ||
) | ||
|
||
// Version holds the current version tag | ||
var version string = strings.TrimPrefix(versionpkg.GetVersion(), "v") | ||
var devVersion string = strings.TrimPrefix(versionpkg.DevVersion, "v") | ||
|
||
var githubSlug = "loft-sh/devpod" | ||
|
||
func PrintNewerVersionWarning() { | ||
if os.Getenv("DEVPOD_SKIP_VERSION_CHECK") != "true" { | ||
// Get version of current binary | ||
latestVersion := NewerVersionAvailable() | ||
if latestVersion != "" { | ||
log.GetInstance().Warnf("There is a newer version of devpod: v%s. Run `devpod upgrade` to upgrade to the newest version.\n", latestVersion) | ||
} | ||
} | ||
} | ||
|
||
var ( | ||
latestVersion string | ||
errLatestVersion error | ||
latestVersionOnce sync.Once | ||
) | ||
|
||
// CheckForNewerVersion checks if there is a newer version on github and returns the newer version | ||
func CheckForNewerVersion() (string, error) { | ||
latestVersionOnce.Do(func() { | ||
latest, found, err := selfupdate.DetectLatest(githubSlug) | ||
if err != nil { | ||
errLatestVersion = err | ||
return | ||
} | ||
|
||
v := semver.MustParse(version) | ||
if !found || latest.Version.Equals(v) { | ||
return | ||
} | ||
|
||
latestVersion = latest.Version.String() | ||
}) | ||
|
||
return latestVersion, errLatestVersion | ||
} | ||
|
||
// NewerVersionAvailable checks if there is a newer version of devpod | ||
func NewerVersionAvailable() string { | ||
if version == devVersion { | ||
return "" | ||
} | ||
|
||
if version != "" { | ||
latestStableVersion, err := CheckForNewerVersion() | ||
if latestStableVersion != "" && err == nil { // Check versions only if newest version could be determined without errors | ||
semverVersion, err := semver.Parse(version) | ||
if err == nil { // Only compare version if version can be parsed | ||
semverLatestStableVersion, err := semver.Parse(latestStableVersion) | ||
if err == nil { // Only compare version if latestStableVersion can be parsed | ||
// If latestStableVersion > version | ||
if semverLatestStableVersion.Compare(semverVersion) == 1 { | ||
return latestStableVersion | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// Upgrade downloads the latest release from github and replaces devpod if a new version is found | ||
func Upgrade(flagVersion string, log log.Logger) error { | ||
updater, err := selfupdate.NewUpdater(selfupdate.Config{ | ||
Filters: []string{"devpod"}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to initialize updater: %w", err) | ||
} | ||
if flagVersion != "" { | ||
release, found, err := updater.DetectVersion(githubSlug, flagVersion) | ||
if err != nil { | ||
return errors.Wrap(err, "find version") | ||
} else if !found { | ||
return fmt.Errorf("devpod version %s couldn't be found", flagVersion) | ||
} | ||
|
||
cmdPath, err := os.Executable() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Infof("Downloading version %s...", flagVersion) | ||
err = updater.UpdateTo(release, cmdPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Donef("Successfully updated devpod to version %s", flagVersion) | ||
return nil | ||
} | ||
|
||
newerVersion, err := CheckForNewerVersion() | ||
if err != nil { | ||
return err | ||
} | ||
if newerVersion == "" { | ||
log.Infof("Current binary is the latest version: %s", version) | ||
return nil | ||
} | ||
|
||
v := semver.MustParse(version) | ||
|
||
log.Info("Downloading newest version...") | ||
latest, err := updater.UpdateSelf(v, githubSlug) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if latest.Version.Equals(v) { | ||
// latest version is the same as current version. It means current binary is up to date. | ||
log.Infof("Current binary is the latest version: %s", version) | ||
} else { | ||
log.Donef("Successfully updated to version %s", latest.Version) | ||
log.Infof("Release note: \n\n%s", latest.ReleaseNotes) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.