...
1
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
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
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
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