Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/cmd/config-set.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
"log/slog"
10
"net/url"
11
12
"github.com/gitpod-io/local-app/pkg/config"
13
"github.com/spf13/cobra"
14
)
15
16
var configSetCmd = &cobra.Command{
17
Use: "set",
18
Short: "Set an individual config value in the config file",
19
Long: `Set an individual config value in the config file.
20
21
Example:
22
# Disable telemetry
23
gitpod config set --telemetry=false
24
25
# Disable autoupdate
26
gitpod config set --autoupdate=false
27
28
# Enable telemetry and autoupdate
29
gitpod config set --telemetry=true --autoupdate=true
30
31
# Set your current context's organization
32
gitpod config set --organization-id=your-org-id
33
`,
34
RunE: func(cmd *cobra.Command, args []string) error {
35
cmd.SilenceUsage = true
36
37
var update bool
38
cfg := config.FromContext(cmd.Context())
39
if cmd.Flags().Changed("autoupdate") {
40
cfg.Autoupdate = configSetOpts.Autoupdate
41
update = true
42
}
43
if cmd.Flags().Changed("telemetry") {
44
cfg.Telemetry.Enabled = configSetOpts.Telemetry
45
update = true
46
}
47
48
gpctx, ok := cfg.Contexts[cfg.ActiveContext]
49
if gpctx == nil {
50
gpctx = &config.ConnectionContext{}
51
}
52
var ctxchanged bool
53
if cmd.Flags().Changed("host") {
54
host, err := url.Parse(configSetOpts.Host)
55
if err != nil {
56
return fmt.Errorf("invalid host: %w", err)
57
}
58
gpctx.Host = &config.YamlURL{URL: host}
59
ctxchanged = true
60
}
61
if cmd.Flags().Changed("organization-id") {
62
gpctx.OrganizationID = configSetOpts.OrganizationID
63
ctxchanged = true
64
}
65
if cmd.Flags().Changed("token") {
66
gpctx.Token = configSetOpts.Token
67
ctxchanged = true
68
}
69
if ctxchanged && !ok {
70
return fmt.Errorf("%w - some flags are tied to an active context: --organization-id, --host, --token", config.ErrNoContext)
71
}
72
if !update && !ctxchanged {
73
return cmd.Help()
74
}
75
76
slog.Debug("updating config")
77
err := config.SaveConfig(cfg.Filename, cfg)
78
if err != nil {
79
return err
80
}
81
return nil
82
},
83
}
84
85
var configSetOpts struct {
86
Autoupdate bool
87
Telemetry bool
88
89
Host string
90
OrganizationID string
91
Token string
92
}
93
94
func init() {
95
configCmd.AddCommand(configSetCmd)
96
configSetCmd.Flags().BoolVar(&configSetOpts.Autoupdate, "autoupdate", true, "enable/disable autoupdate")
97
configSetCmd.Flags().BoolVar(&configSetOpts.Telemetry, "telemetry", true, "enable/disable telemetry")
98
99
configSetCmd.Flags().StringVar(&configSetOpts.Host, "host", "", "the host to use for the context")
100
configSetCmd.Flags().StringVar(&configSetOpts.OrganizationID, "organization-id", "", "the organization ID to use for the context")
101
configSetCmd.Flags().StringVar(&configSetOpts.Token, "token", "", "the token to use for the context")
102
}
103
104