Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/cmd/config-context-delete.go
2497 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"log/slog"
9
10
"github.com/gitpod-io/local-app/pkg/auth"
11
"github.com/gitpod-io/local-app/pkg/config"
12
"github.com/spf13/cobra"
13
)
14
15
var configContextDeleteCmd = &cobra.Command{
16
Use: "delete <name>",
17
Short: "Deletes a context",
18
Args: cobra.ExactArgs(1),
19
RunE: func(cmd *cobra.Command, args []string) (err error) {
20
cmd.SilenceUsage = true
21
22
targetContext := args[0]
23
cfg := config.FromContext(cmd.Context())
24
25
var update bool
26
defer func() {
27
if err == nil && update {
28
slog.Debug("saving config", "filename", cfg.Filename)
29
err = config.SaveConfig(cfg.Filename, cfg)
30
}
31
}()
32
33
if cfg.ActiveContext == targetContext {
34
slog.Info("deleting active context - use `gitpod config use-context` to set a new active context")
35
cfg.ActiveContext = ""
36
update = true
37
}
38
39
gpctx := cfg.Contexts[targetContext]
40
if gpctx == nil {
41
return nil
42
}
43
delete(cfg.Contexts, targetContext)
44
update = true
45
46
err = auth.DeleteToken(gpctx.Host.String())
47
if err != nil {
48
slog.Warn("did not delete token from keyring", "err", err)
49
err = nil
50
}
51
52
return nil
53
},
54
}
55
56
func init() {
57
configContextCmd.AddCommand(configContextDeleteCmd)
58
}
59
60