Path: blob/main/components/local-app/cmd/config-context-modify.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"net/url"910"github.com/gitpod-io/local-app/pkg/config"11"github.com/gitpod-io/local-app/pkg/prettyprint"12"github.com/spf13/cobra"13)1415var configContextModifyCmd = &cobra.Command{16Use: "modify <name | --current>",17Short: "Modifies a context entry in the gitpod CLI config",18Aliases: []string{"add-context"},19Args: cobra.MaximumNArgs(1),20RunE: func(cmd *cobra.Command, args []string) error {21cmd.SilenceUsage = true2223cfg := config.FromContext(cmd.Context())24var targetContext string25if configContextModifyOpts.Current {26if len(args) > 0 {27return prettyprint.AddResolution(fmt.Errorf("cannot use --current and specify a context name"),28"modify current context with `{gitpod} config set-context --current`",29"modify/create a different context with `{gitpod} config set-context <name>`",30)31}32targetContext = cfg.ActiveContext33} else {34if len(args) == 0 {35return prettyprint.AddResolution(fmt.Errorf("must specify a context name or use --current"),36"modify current context with `{gitpod} config set-context --current`",37"modify/create a different context with `{gitpod} config set-context <name>`",38)39}40targetContext = args[0]41}4243gpctx := cfg.Contexts[targetContext]44if gpctx == nil {45gpctx = &config.ConnectionContext{}46cfg.Contexts[targetContext] = gpctx47}48if cmd.Flags().Changed("host") {49host, err := url.Parse(configContextModifyOpts.Host)50if err != nil {51return fmt.Errorf("invalid host: %w", err)52}53gpctx.Host = &config.YamlURL{URL: host}54}55if cmd.Flags().Changed("organization-id") {56gpctx.OrganizationID = configContextModifyOpts.OrganizationID57}58if cmd.Flags().Changed("token") {59gpctx.Token = configContextModifyOpts.Token60}6162err := config.SaveConfig(cfg.Filename, cfg)63if err != nil {64return err65}66return nil67},68}6970var configContextModifyOpts struct {71Current bool72Host string73OrganizationID string74Token string75}7677func init() {78configContextCmd.AddCommand(configContextModifyCmd)7980configContextModifyCmd.Flags().BoolVar(&configContextModifyOpts.Current, "current", false, "modify the current context")81configContextModifyCmd.Flags().StringVar(&configContextModifyOpts.Host, "host", "", "the host to use for the context")82configContextModifyCmd.Flags().StringVar(&configContextModifyOpts.OrganizationID, "organization-id", "", "the organization ID to use for the context")83configContextModifyCmd.Flags().StringVar(&configContextModifyOpts.Token, "token", "", "the token to use for the context")84}858687