...

Source file src/k8s.io/kubectl/pkg/cmd/config/current_context.go

Documentation: k8s.io/kubectl/pkg/cmd/config

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"k8s.io/client-go/tools/clientcmd"
    26  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    27  	"k8s.io/kubectl/pkg/util/i18n"
    28  	"k8s.io/kubectl/pkg/util/templates"
    29  )
    30  
    31  // CurrentContextOptions holds the command-line options for 'config current-context' sub command
    32  type CurrentContextOptions struct {
    33  	ConfigAccess clientcmd.ConfigAccess
    34  }
    35  
    36  var (
    37  	currentContextLong = templates.LongDesc(i18n.T(`
    38  		Display the current-context.`))
    39  
    40  	currentContextExample = templates.Examples(`
    41  		# Display the current-context
    42  		kubectl config current-context`)
    43  )
    44  
    45  // NewCmdConfigCurrentContext returns a Command instance for 'config current-context' sub command
    46  func NewCmdConfigCurrentContext(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
    47  	options := &CurrentContextOptions{ConfigAccess: configAccess}
    48  
    49  	cmd := &cobra.Command{
    50  		Use:     "current-context",
    51  		Short:   i18n.T("Display the current-context"),
    52  		Long:    currentContextLong,
    53  		Example: currentContextExample,
    54  		Run: func(cmd *cobra.Command, args []string) {
    55  			cmdutil.CheckErr(RunCurrentContext(out, options))
    56  		},
    57  	}
    58  
    59  	return cmd
    60  }
    61  
    62  // RunCurrentContext performs the execution of 'config current-context' sub command
    63  func RunCurrentContext(out io.Writer, options *CurrentContextOptions) error {
    64  	config, err := options.ConfigAccess.GetStartingConfig()
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	if config.CurrentContext == "" {
    70  		err = fmt.Errorf("current-context is not set")
    71  		return err
    72  	}
    73  
    74  	fmt.Fprintf(out, "%s\n", config.CurrentContext)
    75  	return nil
    76  }
    77  

View as plain text