Path: blob/main/components/local-app/cmd/config-set.go
2497 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"fmt"8"log/slog"9"net/url"1011"github.com/gitpod-io/local-app/pkg/config"12"github.com/spf13/cobra"13)1415var configSetCmd = &cobra.Command{16Use: "set",17Short: "Set an individual config value in the config file",18Long: `Set an individual config value in the config file.1920Example:21# Disable telemetry22gitpod config set --telemetry=false2324# Disable autoupdate25gitpod config set --autoupdate=false2627# Enable telemetry and autoupdate28gitpod config set --telemetry=true --autoupdate=true2930# Set your current context's organization31gitpod config set --organization-id=your-org-id32`,33RunE: func(cmd *cobra.Command, args []string) error {34cmd.SilenceUsage = true3536var update bool37cfg := config.FromContext(cmd.Context())38if cmd.Flags().Changed("autoupdate") {39cfg.Autoupdate = configSetOpts.Autoupdate40update = true41}42if cmd.Flags().Changed("telemetry") {43cfg.Telemetry.Enabled = configSetOpts.Telemetry44update = true45}4647gpctx, ok := cfg.Contexts[cfg.ActiveContext]48if gpctx == nil {49gpctx = &config.ConnectionContext{}50}51var ctxchanged bool52if cmd.Flags().Changed("host") {53host, err := url.Parse(configSetOpts.Host)54if err != nil {55return fmt.Errorf("invalid host: %w", err)56}57gpctx.Host = &config.YamlURL{URL: host}58ctxchanged = true59}60if cmd.Flags().Changed("organization-id") {61gpctx.OrganizationID = configSetOpts.OrganizationID62ctxchanged = true63}64if cmd.Flags().Changed("token") {65gpctx.Token = configSetOpts.Token66ctxchanged = true67}68if ctxchanged && !ok {69return fmt.Errorf("%w - some flags are tied to an active context: --organization-id, --host, --token", config.ErrNoContext)70}71if !update && !ctxchanged {72return cmd.Help()73}7475slog.Debug("updating config")76err := config.SaveConfig(cfg.Filename, cfg)77if err != nil {78return err79}80return nil81},82}8384var configSetOpts struct {85Autoupdate bool86Telemetry bool8788Host string89OrganizationID string90Token string91}9293func init() {94configCmd.AddCommand(configSetCmd)95configSetCmd.Flags().BoolVar(&configSetOpts.Autoupdate, "autoupdate", true, "enable/disable autoupdate")96configSetCmd.Flags().BoolVar(&configSetOpts.Telemetry, "telemetry", true, "enable/disable telemetry")9798configSetCmd.Flags().StringVar(&configSetOpts.Host, "host", "", "the host to use for the context")99configSetCmd.Flags().StringVar(&configSetOpts.OrganizationID, "organization-id", "", "the organization ID to use for the context")100configSetCmd.Flags().StringVar(&configSetOpts.Token, "token", "", "the token to use for the context")101}102103104