github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/prompt.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 9 10 "github.com/olli-ai/jx/v2/pkg/kube" 11 "github.com/spf13/cobra" 12 13 "github.com/fatih/color" 14 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 15 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 16 "github.com/olli-ai/jx/v2/pkg/util" 17 ) 18 19 const ( 20 optionLabelColor = "label-color" 21 optionNamespaceColor = "namespace-color" 22 optionContextColor = "context-color" 23 ) 24 25 // PromptOptions containers the CLI options 26 type PromptOptions struct { 27 *opts.CommonOptions 28 29 NoLabel bool 30 ShowIcon bool 31 32 Prefix string 33 Label string 34 Separator string 35 Divider string 36 Suffix string 37 38 LabelColor []string 39 NamespaceColor []string 40 ContextColor []string 41 } 42 43 var ( 44 get_prompt_long = templates.LongDesc(` 45 Generate a command prompt for the current namespace and Kubernetes context. 46 `) 47 48 get_prompt_example = templates.Examples(` 49 # Generate the current prompt 50 jx prompt 51 52 # Enable the prompt for bash 53 PS1="[\u@\h \W \$(jx prompt)]\$ " 54 55 # Enable the prompt for zsh 56 PROMPT='$(jx prompt)'$PROMPT 57 `) 58 ) 59 60 // NewCmdPrompt creates the new command for: jx get prompt 61 func NewCmdPrompt(commonOpts *opts.CommonOptions) *cobra.Command { 62 options := &PromptOptions{ 63 CommonOptions: commonOpts, 64 } 65 cmd := &cobra.Command{ 66 Use: "prompt", 67 Short: "Generate the command line prompt for the current team and environment", 68 Long: get_prompt_long, 69 Example: get_prompt_example, 70 Run: func(cmd *cobra.Command, args []string) { 71 options.Cmd = cmd 72 options.Args = args 73 err := options.Run() 74 helper.CheckErr(err) 75 }, 76 } 77 cmd.Flags().StringVarP(&options.Prefix, "prefix", "p", "", "The prefix text for the prompt") 78 cmd.Flags().StringVarP(&options.Label, "label", "l", "k8s", "The label for the prompt") 79 cmd.Flags().StringVarP(&options.Separator, "separator", "s", ":", "The separator between the label and the rest of the prompt") 80 cmd.Flags().StringVarP(&options.Divider, "divider", "d", ":", "The divider between the team and environment for the prompt") 81 cmd.Flags().StringVarP(&options.Suffix, "suffix", "x", ">", "The suffix text for the prompt") 82 83 cmd.Flags().StringArrayVarP(&options.LabelColor, optionLabelColor, "", []string{"blue"}, "The color for the label") 84 cmd.Flags().StringArrayVarP(&options.NamespaceColor, optionNamespaceColor, "", []string{"green"}, "The color for the namespace") 85 cmd.Flags().StringArrayVarP(&options.ContextColor, optionContextColor, "", []string{"cyan"}, "The color for the Kubernetes context") 86 87 cmd.Flags().BoolVarP(&options.NoLabel, "no-label", "", false, "Disables the use of the label in the prompt") 88 cmd.Flags().BoolVarP(&options.ShowIcon, "icon", "i", false, "Uses an icon for the label in the prompt") 89 90 return cmd 91 } 92 93 // Run implements this command 94 func (o *PromptOptions) Run() error { 95 config, _, err := o.Kube().LoadConfig() 96 97 context := config.CurrentContext 98 namespace := kube.CurrentNamespace(config) 99 100 // enable color 101 color.NoColor = os.Getenv("TERM") == "dumb" 102 103 label := o.Label 104 separator := o.Separator 105 divider := o.Divider 106 prefix := o.Prefix 107 suffix := o.Suffix 108 109 labelColor, err := util.GetColor(optionLabelColor, o.LabelColor) 110 if err != nil { 111 return err 112 } 113 nsColor, err := util.GetColor(optionLabelColor, o.NamespaceColor) 114 if err != nil { 115 return err 116 } 117 ctxColor, err := util.GetColor(optionLabelColor, o.ContextColor) 118 if err != nil { 119 return err 120 } 121 if o.NoLabel { 122 label = "" 123 separator = "" 124 } else { 125 if o.ShowIcon { 126 label = "☸️ " 127 label = labelColor.Sprint(label) 128 } else { 129 label = labelColor.Sprint(label) 130 } 131 } 132 if namespace == "" { 133 divider = "" 134 } else { 135 namespace = nsColor.Sprint(namespace) 136 } 137 context = ctxColor.Sprint(context) 138 fmt.Fprintf(o.Out, "%s\n", strings.Join([]string{prefix, label, separator, namespace, divider, context, suffix}, "")) 139 return nil 140 }