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-modify.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
"fmt"
9
"net/url"
10
11
"github.com/gitpod-io/local-app/pkg/config"
12
"github.com/gitpod-io/local-app/pkg/prettyprint"
13
"github.com/spf13/cobra"
14
)
15
16
var configContextModifyCmd = &cobra.Command{
17
Use: "modify <name | --current>",
18
Short: "Modifies a context entry in the gitpod CLI config",
19
Aliases: []string{"add-context"},
20
Args: cobra.MaximumNArgs(1),
21
RunE: func(cmd *cobra.Command, args []string) error {
22
cmd.SilenceUsage = true
23
24
cfg := config.FromContext(cmd.Context())
25
var targetContext string
26
if configContextModifyOpts.Current {
27
if len(args) > 0 {
28
return prettyprint.AddResolution(fmt.Errorf("cannot use --current and specify a context name"),
29
"modify current context with `{gitpod} config set-context --current`",
30
"modify/create a different context with `{gitpod} config set-context <name>`",
31
)
32
}
33
targetContext = cfg.ActiveContext
34
} else {
35
if len(args) == 0 {
36
return prettyprint.AddResolution(fmt.Errorf("must specify a context name or use --current"),
37
"modify current context with `{gitpod} config set-context --current`",
38
"modify/create a different context with `{gitpod} config set-context <name>`",
39
)
40
}
41
targetContext = args[0]
42
}
43
44
gpctx := cfg.Contexts[targetContext]
45
if gpctx == nil {
46
gpctx = &config.ConnectionContext{}
47
cfg.Contexts[targetContext] = gpctx
48
}
49
if cmd.Flags().Changed("host") {
50
host, err := url.Parse(configContextModifyOpts.Host)
51
if err != nil {
52
return fmt.Errorf("invalid host: %w", err)
53
}
54
gpctx.Host = &config.YamlURL{URL: host}
55
}
56
if cmd.Flags().Changed("organization-id") {
57
gpctx.OrganizationID = configContextModifyOpts.OrganizationID
58
}
59
if cmd.Flags().Changed("token") {
60
gpctx.Token = configContextModifyOpts.Token
61
}
62
63
err := config.SaveConfig(cfg.Filename, cfg)
64
if err != nil {
65
return err
66
}
67
return nil
68
},
69
}
70
71
var configContextModifyOpts struct {
72
Current bool
73
Host string
74
OrganizationID string
75
Token string
76
}
77
78
func init() {
79
configContextCmd.AddCommand(configContextModifyCmd)
80
81
configContextModifyCmd.Flags().BoolVar(&configContextModifyOpts.Current, "current", false, "modify the current context")
82
configContextModifyCmd.Flags().StringVar(&configContextModifyOpts.Host, "host", "", "the host to use for the context")
83
configContextModifyCmd.Flags().StringVar(&configContextModifyOpts.OrganizationID, "organization-id", "", "the organization ID to use for the context")
84
configContextModifyCmd.Flags().StringVar(&configContextModifyOpts.Token, "token", "", "the token to use for the context")
85
}
86
87