-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[gitpod-cli] Add auto-updating capabilities #19056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0866b9d
Add version command
csweichel 981db06
Restructure config package
csweichel da824ae
Bring back config get and config set
csweichel 565828b
Support login host without protocol scheme
csweichel 8753a77
Add autoupdate functionality
csweichel 38d6093
Generate update manifest during build
csweichel 311b321
Better update failure behavior
csweichel ae29e4a
Add latest to version command
csweichel fd00f51
Add version update command
csweichel 84b02d7
Use cannonical semver form
csweichel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
// 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 ( | ||
"github.com/gitpod-io/local-app/pkg/config" | ||
"github.com/gitpod-io/local-app/pkg/prettyprint" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var configGetCmd = &cobra.Command{ | ||
Use: "get", | ||
Short: "Get an individual config value in the config file", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
|
||
cfg := config.FromContext(cmd.Context()) | ||
|
||
return WriteTabular([]struct { | ||
Telemetry bool `header:"Telemetry"` | ||
Autoupdate bool `header:"Autoupdate"` | ||
}{ | ||
{Telemetry: cfg.Telemetry.Enabled, Autoupdate: cfg.Autoupdate}, | ||
}, configGetOpts.Format, prettyprint.WriterFormatNarrow) | ||
}, | ||
} | ||
|
||
var configGetOpts struct { | ||
Format formatOpts | ||
} | ||
|
||
func init() { | ||
configCmd.AddCommand(configGetCmd) | ||
addFormatFlags(configGetCmd, &configGetOpts.Format) | ||
} |
This file contains hidden or 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,64 @@ | ||
// 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 ( | ||
"log/slog" | ||
|
||
"github.com/gitpod-io/local-app/pkg/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var configSetCmd = &cobra.Command{ | ||
Use: "set", | ||
Short: "Set an individual config value in the config file", | ||
Long: `Set an individual config value in the config file. | ||
|
||
Example: | ||
# Disable telemetry | ||
local-app config set --telemetry=false | ||
|
||
# Disable autoupdate | ||
local-app config set --autoupdate=false | ||
|
||
# Enable telemetry and autoupdate | ||
local-app config set --telemetry=true --autoupdate=true | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
|
||
var update bool | ||
cfg := config.FromContext(cmd.Context()) | ||
if cmd.Flags().Changed("autoupdate") { | ||
cfg.Autoupdate = configSetOpts.Autoupdate | ||
update = true | ||
} | ||
if cmd.Flags().Changed("telemetry") { | ||
cfg.Telemetry.Enabled = configSetOpts.Telemetry | ||
update = true | ||
} | ||
if !update { | ||
return cmd.Help() | ||
} | ||
|
||
slog.Debug("updating config") | ||
err := config.SaveConfig(cfg.Filename, cfg) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
var configSetOpts struct { | ||
Autoupdate bool | ||
Telemetry bool | ||
} | ||
|
||
func init() { | ||
configCmd.AddCommand(configSetCmd) | ||
configSetCmd.Flags().BoolVar(&configSetOpts.Autoupdate, "autoupdate", true, "enable/disable autoupdate") | ||
configSetCmd.Flags().BoolVar(&configSetOpts.Telemetry, "telemetry", true, "enable/disable telemetry") | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,54 @@ | ||
// 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) | ||
} |
This file contains hidden or 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,67 @@ | ||
// 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 ( | ||
"encoding/json" | ||
"net/http" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"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/opencontainers/go-digest" | ||
) | ||
|
||
func TestVersionUpdateCmd(t *testing.T) { | ||
RunCommandTests(t, []CommandTest{ | ||
{ | ||
Name: "happy path", | ||
Commandline: []string{"version", "update"}, | ||
PrepServer: func(mux *http.ServeMux) { | ||
newBinary := []byte("#!/bin/bash\necho hello world") | ||
mux.HandleFunc(selfupdate.GitpodCLIBasePath+"/manifest.json", func(w http.ResponseWriter, r *http.Request) { | ||
mf, err := json.Marshal(selfupdate.Manifest{ | ||
Version: semver.MustParse("v9999.0"), | ||
Binaries: []selfupdate.Binary{ | ||
{ | ||
Filename: "gitpod", | ||
OS: runtime.GOOS, | ||
Arch: runtime.GOARCH, | ||
Digest: digest.FromBytes(newBinary), | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
_, _ = w.Write(mf) | ||
}) | ||
mux.HandleFunc(selfupdate.GitpodCLIBasePath+"/gitpod", func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write(newBinary) | ||
}) | ||
}, | ||
Config: AddActiveTestContext(&config.Config{}), | ||
}, | ||
{ | ||
Name: "no update needed", | ||
Commandline: []string{"version", "update"}, | ||
PrepServer: func(mux *http.ServeMux) { | ||
mux.HandleFunc(selfupdate.GitpodCLIBasePath+"/manifest.json", func(w http.ResponseWriter, r *http.Request) { | ||
mf, err := json.Marshal(selfupdate.Manifest{ | ||
Version: constants.Version, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
_, _ = w.Write(mf) | ||
}) | ||
}, | ||
Config: AddActiveTestContext(&config.Config{}), | ||
}, | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.