github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/select.go (about)

     1  /*
     2   * Copyright 2018-2022 The NATS Authors
     3   * Licensed under the Apache License, Version 2.0 (the "License");
     4   * you may not use this file except in compliance with the License.
     5   * You may obtain a copy of the License at
     6   *
     7   * http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15  
    16  package cmd
    17  
    18  import (
    19  	"github.com/spf13/cobra"
    20  )
    21  
    22  func init() {
    23  	GetRootCmd().AddCommand(createSelecteCmd())
    24  }
    25  
    26  func createSelecteCmd() *cobra.Command {
    27  	cmd := &cobra.Command{
    28  		Use:   "select",
    29  		Short: "Set the current operator or account",
    30  	}
    31  	cmd.AddCommand(selectOperatorCmd())
    32  	cmd.AddCommand(selectAccountCmd())
    33  	return cmd
    34  }
    35  
    36  func selectOperatorCmd() *cobra.Command {
    37  	return &cobra.Command{
    38  		Use:   "operator",
    39  		Short: "set the operator",
    40  		Args: func(cmd *cobra.Command, args []string) error {
    41  			if err := cobra.ExactArgs(1)(cmd, args); err != nil {
    42  				return err
    43  			}
    44  
    45  			return nil
    46  		},
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			current := GetConfig()
    49  			if err := current.ContextConfig.Update("", args[0], ""); err != nil {
    50  				return err
    51  			}
    52  
    53  			return current.Save()
    54  		},
    55  	}
    56  }
    57  
    58  func selectAccountCmd() *cobra.Command {
    59  	return &cobra.Command{
    60  		Use:   "account",
    61  		Short: "set the account",
    62  		Args: func(cmd *cobra.Command, args []string) error {
    63  			if err := cobra.ExactArgs(1)(cmd, args); err != nil {
    64  				return err
    65  			}
    66  
    67  			return nil
    68  		},
    69  		RunE: func(cmd *cobra.Command, args []string) error {
    70  			current := GetConfig()
    71  			if err := current.ContextConfig.Update("", "", args[0]); err != nil {
    72  				return err
    73  			}
    74  
    75  			return current.Save()
    76  		},
    77  	}
    78  }