Skip to content

Commit

Permalink
support for custom Kubernetes cluster name shortening with regex
Browse files Browse the repository at this point in the history
Adds two new flags:

* - `-shorten-kube-names-regex-match`
* - `-shorten-kube-names-regex-template` (defaults to `${1}`)

Examples:

Assume a kubernetes cluster named `myorg-region-profile-cluster-01`

Using the default template:
```console
powerline-go -shorten-kube-names-regex-match '^myorg-\w+-\w+-(.*)'
```
result: `cluster-01`

Using a custom template:
```console
powerline-go -shorten-kube-names-regex-match '^myorg-(\w+)-\w+-(.*)' -shorten-kube-names-regex-template '${1}/${2}'
```
result: `region/cluster-01`
  • Loading branch information
joemiller committed May 6, 2022
1 parent a7e4485 commit 3570f87
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 101 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ Usage of powerline-go:
(default "fancy")
-duration string
The elapsed clock-time of the previous command
-duration-low-precision
Use low precision timing for duration with milliseconds as maximum resolution
-duration-min string
The minimal time a command has to take before the duration segment is shown (default "0")
-east-asian-width
Expand Down Expand Up @@ -280,6 +282,11 @@ Usage of powerline-go:
Shortens names for EKS Kube clusters.
-shorten-gke-names
Shortens names for GKE Kube clusters.
-shorten-kube-names-regex-match string
Shortens names for Kube clusters matching a custom regex.
-shorten-kube-names-regex-template string
String template to use with -shorten-kube-names-regex-match.
(default "${1}")
-static-prompt-indicator
Always show the prompt indicator with the default color, never with the error color
-theme string
Expand Down
90 changes: 50 additions & 40 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,48 @@ import (
)

type arguments struct {
CwdMode *string
CwdMaxDepth *int
CwdMaxDirSize *int
ColorizeHostname *bool
HostnameOnlyIfSSH *bool
SshAlternateIcon *bool
EastAsianWidth *bool
PromptOnNewLine *bool
StaticPromptIndicator *bool
VenvNameSizeLimit *int
GitAssumeUnchangedSize *int64
GitDisableStats *string
GitMode *string
Jobs *int
Mode *string
Theme *string
Shell *string
Modules *string
ModulesRight *string
Priority *string
MaxWidthPercentage *int
TruncateSegmentWidth *int
PrevError *int
NumericExitCodes *bool
IgnoreRepos *string
ShortenGKENames *bool
ShortenEKSNames *bool
ShortenOpenshiftNames *bool
ShellVar *string
ShellVarNoWarnEmpty *bool
TrimADDomain *bool
PathAliases *string
Duration *string
DurationMin *string
DurationLowPrecision *bool
Eval *bool
Condensed *bool
IgnoreWarnings *bool
Time *string
ViMode *string
CwdMode *string
CwdMaxDepth *int
CwdMaxDirSize *int
ColorizeHostname *bool
HostnameOnlyIfSSH *bool
SshAlternateIcon *bool
EastAsianWidth *bool
PromptOnNewLine *bool
StaticPromptIndicator *bool
VenvNameSizeLimit *int
GitAssumeUnchangedSize *int64
GitDisableStats *string
GitMode *string
Jobs *int
Mode *string
Theme *string
Shell *string
Modules *string
ModulesRight *string
Priority *string
MaxWidthPercentage *int
TruncateSegmentWidth *int
PrevError *int
NumericExitCodes *bool
IgnoreRepos *string
ShortenGKENames *bool
ShortenEKSNames *bool
ShortenKubeNamesRegexMatch *string
ShortenKubeNamesRegexTemplate *string
ShortenOpenshiftNames *bool
ShellVar *string
ShellVarNoWarnEmpty *bool
TrimADDomain *bool
PathAliases *string
Duration *string
DurationMin *string
DurationLowPrecision *bool
Eval *bool
Condensed *bool
IgnoreWarnings *bool
Time *string
ViMode *string
}

var args = arguments{
Expand Down Expand Up @@ -173,6 +175,14 @@ var args = arguments{
"shorten-openshift-names",
defaults.ShortenOpenshiftNames,
comments("Shortens names for Openshift Kube clusters.")),
ShortenKubeNamesRegexMatch: flag.String(
"shorten-kube-names-regex-match",
"",
comments("Shortens names for Kube clusters matching a custom regex.")),
ShortenKubeNamesRegexTemplate: flag.String(
"shorten-kube-names-regex-template",
defaults.ShortenKubeNamesRegexTemplate,
commentsWithDefaults("String template to use with -shorten-kube-names-regex-match.")),
ShellVar: flag.String(
"shell-var",
defaults.ShellVar,
Expand Down
88 changes: 45 additions & 43 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,51 @@ type ThemeMap map[string]Theme
type AliasMap map[string]string

type Config struct {
CwdMode string `json:"cwd-mode"`
CwdMaxDepth int `json:"cwd-max-depth"`
CwdMaxDirSize int `json:"cwd-max-dir-size"`
ColorizeHostname bool `json:"colorize-hostname"`
HostnameOnlyIfSSH bool `json:"hostname-only-if-ssh"`
SshAlternateIcon bool `json:"alternate-ssh-icon"`
EastAsianWidth bool `json:"east-asian-width"`
PromptOnNewLine bool `json:"newline"`
StaticPromptIndicator bool `json:"static-prompt-indicator"`
VenvNameSizeLimit int `json:"venv-name-size-limit"`
Jobs int `json:"-"`
GitAssumeUnchangedSize int64 `json:"git-assume-unchanged-size"`
GitDisableStats []string `json:"git-disable-stats"`
GitMode string `json:"git-mode"`
Mode string `json:"mode"`
Theme string `json:"theme"`
Shell string `json:"shell"`
Modules []string `json:"modules"`
ModulesRight []string `json:"modules-right"`
Priority []string `json:"priority"`
MaxWidthPercentage int `json:"max-width-percentage"`
TruncateSegmentWidth int `json:"truncate-segment-width"`
PrevError int `json:"-"`
NumericExitCodes bool `json:"numeric-exit-codes"`
IgnoreRepos []string `json:"ignore-repos"`
ShortenGKENames bool `json:"shorten-gke-names"`
ShortenEKSNames bool `json:"shorten-eks-names"`
ShortenOpenshiftNames bool `json:"shorten-openshift-names"`
ShellVar string `json:"shell-var"`
ShellVarNoWarnEmpty bool `json:"shell-var-no-warn-empty"`
TrimADDomain bool `json:"trim-ad-domain"`
PathAliases AliasMap `json:"path-aliases"`
Duration string `json:"-"`
DurationMin string `json:"duration-min"`
DurationLowPrecision bool `json:"duration-low-precision"`
Eval bool `json:"eval"`
Condensed bool `json:"condensed"`
IgnoreWarnings bool `json:"ignore-warnings"`
Modes SymbolMap `json:"modes"`
Shells ShellMap `json:"shells"`
Themes ThemeMap `json:"themes"`
Time string `json:"-"`
ViMode string `json:"vi-mode"`
CwdMode string `json:"cwd-mode"`
CwdMaxDepth int `json:"cwd-max-depth"`
CwdMaxDirSize int `json:"cwd-max-dir-size"`
ColorizeHostname bool `json:"colorize-hostname"`
HostnameOnlyIfSSH bool `json:"hostname-only-if-ssh"`
SshAlternateIcon bool `json:"alternate-ssh-icon"`
EastAsianWidth bool `json:"east-asian-width"`
PromptOnNewLine bool `json:"newline"`
StaticPromptIndicator bool `json:"static-prompt-indicator"`
VenvNameSizeLimit int `json:"venv-name-size-limit"`
Jobs int `json:"-"`
GitAssumeUnchangedSize int64 `json:"git-assume-unchanged-size"`
GitDisableStats []string `json:"git-disable-stats"`
GitMode string `json:"git-mode"`
Mode string `json:"mode"`
Theme string `json:"theme"`
Shell string `json:"shell"`
Modules []string `json:"modules"`
ModulesRight []string `json:"modules-right"`
Priority []string `json:"priority"`
MaxWidthPercentage int `json:"max-width-percentage"`
TruncateSegmentWidth int `json:"truncate-segment-width"`
PrevError int `json:"-"`
NumericExitCodes bool `json:"numeric-exit-codes"`
IgnoreRepos []string `json:"ignore-repos"`
ShortenGKENames bool `json:"shorten-gke-names"`
ShortenEKSNames bool `json:"shorten-eks-names"`
ShortenOpenshiftNames bool `json:"shorten-openshift-names"`
ShortenKubeNamesRegexMatch string `json:"shorten-kube-names-regex-match"`
ShortenKubeNamesRegexTemplate string `json:"shorten-kube-names-regex-template"`
ShellVar string `json:"shell-var"`
ShellVarNoWarnEmpty bool `json:"shell-var-no-warn-empty"`
TrimADDomain bool `json:"trim-ad-domain"`
PathAliases AliasMap `json:"path-aliases"`
Duration string `json:"-"`
DurationMin string `json:"duration-min"`
DurationLowPrecision bool `json:"duration-low-precision"`
Eval bool `json:"eval"`
Condensed bool `json:"condensed"`
IgnoreWarnings bool `json:"ignore-warnings"`
Modes SymbolMap `json:"modes"`
Shells ShellMap `json:"shells"`
Themes ThemeMap `json:"themes"`
Time string `json:"-"`
ViMode string `json:"vi-mode"`
}

func (mode *SymbolTemplate) UnmarshalJSON(data []byte) error {
Expand Down
36 changes: 19 additions & 17 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,25 @@ var defaults = Config{
"exit",
"cwd-path",
},
MaxWidthPercentage: 0,
TruncateSegmentWidth: 16,
PrevError: 0,
NumericExitCodes: false,
IgnoreRepos: []string{},
ShortenGKENames: false,
ShortenEKSNames: false,
ShellVar: "",
ShellVarNoWarnEmpty: false,
TrimADDomain: false,
PathAliases: AliasMap{},
Duration: "",
DurationMin: "0",
DurationLowPrecision: false,
Eval: false,
Condensed: false,
IgnoreWarnings: false,
MaxWidthPercentage: 0,
TruncateSegmentWidth: 16,
PrevError: 0,
NumericExitCodes: false,
IgnoreRepos: []string{},
ShortenGKENames: false,
ShortenEKSNames: false,
ShortenKubeNamesRegexMatch: "",
ShortenKubeNamesRegexTemplate: "${1}",
ShellVar: "",
ShellVarNoWarnEmpty: false,
TrimADDomain: false,
PathAliases: AliasMap{},
Duration: "",
DurationMin: "0",
DurationLowPrecision: false,
Eval: false,
Condensed: false,
IgnoreWarnings: false,
Modes: SymbolMap{
"compatible": {
Lock: "RO",
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ func main() {
cfg.ShortenEKSNames = *args.ShortenEKSNames
case "shorten-openshift-names":
cfg.ShortenOpenshiftNames = *args.ShortenOpenshiftNames
case "shorten-kube-names-regex-match":
cfg.ShortenKubeNamesRegexMatch = *args.ShortenKubeNamesRegexMatch
case "shorten-kube-names-regex-template":
cfg.ShortenKubeNamesRegexTemplate = *args.ShortenKubeNamesRegexTemplate
case "shell-var":
cfg.ShellVar = *args.ShellVar
case "shell-var-no-warn-empty":
Expand Down
10 changes: 9 additions & 1 deletion segment-kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package main

import (
"fmt"
pwl "github.com/justjanne/powerline-go/powerline"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"

pwl "github.com/justjanne/powerline-go/powerline"

"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -106,6 +107,13 @@ func segmentKube(p *powerline) []pwl.Segment {
if arnMatches := arnRe.FindStringSubmatch(cluster); arnMatches != nil && p.cfg.ShortenEKSNames {
cluster = arnMatches[1]
}

// Shorten Kubernetes cluster names using a custom regex and optionally a custom string template
if p.cfg.ShortenKubeNamesRegexMatch != "" {
nameRe := regexp.MustCompile(p.cfg.ShortenKubeNamesRegexMatch)
cluster = nameRe.ReplaceAllString(cluster, p.cfg.ShortenKubeNamesRegexTemplate)
}

segments := []pwl.Segment{}
// Only draw the icon once
kubeIconHasBeenDrawnYet := false
Expand Down

0 comments on commit 3570f87

Please sign in to comment.