gitpod/components/local-app/cmd/version-update.go
Christian Weichel e4bc514a10
[gitpod-cli] Add auto-updating capabilities (#19056)
* Add version command

* Restructure config package

* Bring back config get and config set

* Support login host without protocol scheme

* Add autoupdate functionality

* Generate update manifest during build

* Better update failure behavior

* Add latest to version command

* Add version update command

* Use cannonical semver form
2023-11-13 18:57:51 +02:00

55 lines
1.2 KiB
Go

// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package cmd
import (
"context"
"time"
"github.com/gitpod-io/local-app/pkg/config"
"github.com/gitpod-io/local-app/pkg/constants"
"github.com/gitpod-io/local-app/pkg/selfupdate"
"github.com/sagikazarmark/slog-shim"
"github.com/spf13/cobra"
)
var versionUpdateCmd = &cobra.Command{
Use: "update",
Short: "Updates the CLI to the latest version",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
dlctx, cancel := context.WithTimeout(cmd.Context(), 30*time.Second)
defer cancel()
cfg := config.FromContext(cmd.Context())
gpctx, err := cfg.GetActiveContext()
if err != nil {
return err
}
mf, err := selfupdate.DownloadManifest(dlctx, gpctx.Host.URL.String())
if err != nil {
return err
}
if !selfupdate.NeedsUpdate(constants.Version, mf) {
slog.Info("already up to date")
return nil
}
slog.Info("updating to latest version " + mf.Version.String())
err = selfupdate.ReplaceSelf(dlctx, mf)
if err != nil {
return err
}
return nil
},
}
func init() {
versionCmd.AddCommand(versionUpdateCmd)
}