Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate zsh plugin #32

Open
wants to merge 13 commits into
base: generate-aliases
Choose a base branch
from
Open
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,24 @@ All commands with the same comment will be grouped together in the generated fil
# Installation

To grab a bunch of aliases without customization get the
`aliases` file and source it in your `bashrc` or `zshrc` file.

## Bash Example
`aliases.sh` file and source it in your `bashrc` or `zshrc` file.

```bash
curl https://raw.githubusercontent.com/Dbz/kube-aliases/master/aliases -o ${HOME}/.kube-aliases
echo "source ${HOME}/.kube-aliases" >> .bashrc
curl https://raw.githubusercontent.com/Dbz/kube-aliases/master/aliases.sh -o ${HOME}/.kube-aliases.sh
echo "source ${HOME}/.kube-aliases.sh" >> .bashrc
```

## Zsh Example
## Oh-My-Zsh Example

To install this as a plugin:

```bash
curl https://raw.githubusercontent.com/Dbz/kube-aliases/master/aliases -o ${HOME}/.kube-aliases
echo "source ${HOME}/.kube-aliases" >> .zshrc
git clone https://github.com/Dbz/kube-aliases.git ~/.oh-my-zsh/custom/plugins/kube-aliases
echo "plugins+=(kube-aliases)" >> ~/.zshrc
```

If you set the `ZSH_CUSTOM` environment variable, then you should modify the git clone directory to be `$ZSH_CUSTOM/plugins/kube-aliases`.

## Generate Aliases

Coming soon
Expand Down
501 changes: 0 additions & 501 deletions aliases

This file was deleted.

957 changes: 957 additions & 0 deletions aliases.ps1

Large diffs are not rendered by default.

520 changes: 520 additions & 0 deletions aliases.sh

Large diffs are not rendered by default.

74 changes: 44 additions & 30 deletions aliases.yaml
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
resources:
certificatesigningrequests:
certificatesigningrequest:
short: csr
clusterrolebindings:
clusterrolebinding:
short: crb
clusterroles:
clusterrole:
short: cr
componentstatus:
short: cs
configmaps:
configmap:
short: cm
controllerrevisions:
controllerrevision:
short: crv
cronjobs:
cronjob:
short: cj
daemonsets:
daemonset:
short: ds
deployments:
deployment:
short: d
endpoints:
endpoint:
short: ep
events:
event:
short: ev
horizontalpodautoscalers:
horizontalpodautoscaler:
short: hpas
ingress:
short: i
jobs:
job:
short: j
limitranges:
limitrange:
short: lr
namespaces:
namespace:
short: ns
networkpolicies:
networkpolicie:
short: np
nodes:
node:
short: n
persistentvolume:
short: pv
persistentvolumeclaims:
persistentvolumeclaim:
short: pvc
poddisruptionbudgets:
poddisruptionbudget:
short: pdb
podpreset:
short: pp
pods:
pod:
short: p
podsecuritypolicies:
podsecuritypolicy:
short: psp
podtemplates:
podtemplate:
short: pt
replicasets:
replicaset:
short: rs
replicationcontrollers:
replicationcontroller:
short: krc
resourcequotas:
resourcequota:
short: rq
rolebindings:
rolebinding:
short: rb
roles:
role:
short: r
secrets:
secret:
short: sc
serviceaccounts:
serviceaccount:
short: sa
services:
service:
short: s
statefulsets:
statefulset:
short: ss
storageclass:
short: scls

cmds:
- short: c
Expand Down Expand Up @@ -114,6 +116,15 @@ additional:
- short: k
cmd: "kubectl"
comment: "CLI aliases."
- short: kg
cmd: "kubectl get"
patmessina marked this conversation as resolved.
Show resolved Hide resolved
comment: "CLI aliases."
- short: ke
cmd: "kubectl edit"
comment: "CLI aliases."
- short: kd
cmd: "kubectl describe"
comment: "CLI aliases."
- short: kctx
cmd: "kubectx"
comment: "CLI aliases."
Expand All @@ -126,6 +137,9 @@ additional:
- short: kaf
cmd: "kubectl apply -f"
comment: "Pushing/modifying configs."
- short: kdf
cmd: "kubectl describe -f"
comment: "Pushing/modifying configs."
- short: kgf
cmd: "kubectl get -f"
comment: "Pushing/modifying configs."
Expand Down
3 changes: 2 additions & 1 deletion generate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cmds:
additional:
- short: wkgp
cmd: "watch kubectl get pods"
comment: "Additional pod commands."
```

Which will generate the following file
Expand All @@ -35,7 +36,7 @@ by running

```bash
make
bin/generate-kube-aliases alias.yaml aliases
bin/generate-kube-aliases alias.yaml aliases.sh
```

# Contributing
Expand Down
33 changes: 20 additions & 13 deletions generate/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ import (
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "generate-kube-aliases source-file target-file",
Short: "Generate zsh aliases for Kubectl",
Example: "generate-kube-aliases alias.yaml aliases.zsh",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
err := generate.Generate(args[0], args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
}
var (
rootCmd = &cobra.Command{
Use: "generate-kube-aliases source-file target-file",
Short: "Generate zsh aliases for Kubectl",
Example: "generate-kube-aliases alias.yaml aliases.zsh",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
err := generate.Generate(args[0], args[1], powershell)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
}
powershell bool
)

func Execute() {
if err := rootCmd.Execute(); err != nil {
Expand All @@ -32,6 +35,10 @@ func Execute() {

func init() {
cobra.OnInitialize(OnInitialize)

rootCmd.Flags().BoolVarP(&powershell, "powershell",
"p", false,
"generate powershell aliases instead on linux shell aliases")
}

func OnInitialize() {
Expand Down
30 changes: 25 additions & 5 deletions generate/pkg/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/Dbz/kube-aliases/generate/pkg/types"
)

func Generate(filePath, targetPath string) error {
func Generate(filePath, targetPath string, powershell bool) error {
file, err := ioutil.ReadFile(filePath)
if err != nil {
return fmt.Errorf("error reading file %s with error %s",
Expand All @@ -31,7 +31,13 @@ func Generate(filePath, targetPath string) error {
}
defer aliasFile.Close()

_, err = io.WriteString(aliasFile, "#!/bin/bash\n")
if err != nil {
return fmt.Errorf("Warning: could not write shebang: %v", err)
}

var aliasBuilder strings.Builder

var aliasCMDs types.AliasCMDs
for r := range aliases.Resources {

Expand All @@ -55,7 +61,7 @@ func Generate(filePath, targetPath string) error {
// Take care of any additional aliases
aliasCMDs.CMDs = aliases.Additional

err = GenerateAlias(&aliasBuilder, &aliasCMDs)
err = GenerateAlias(&aliasBuilder, &aliasCMDs, powershell)
if err != nil {
return err
}
Expand All @@ -70,7 +76,8 @@ func Generate(filePath, targetPath string) error {
}

// GenerateAlias -- TODO document
func GenerateAlias(w io.Writer, aliases *types.AliasCMDs) error {
func GenerateAlias(w io.Writer,
aliases *types.AliasCMDs, powershell bool) error {
aliases.AliasNames = make(map[string]string)
var lastResource string
for _, alias := range aliases.Aliases {
Expand Down Expand Up @@ -104,7 +111,14 @@ func GenerateAlias(w io.Writer, aliases *types.AliasCMDs) error {
lastResource = alias.Resource
}

fmt.Fprintf(w, "alias %v=\"%v\"\n", aliasName, aliasCommand)
if !powershell {
fmt.Fprintf(w, "alias %v=\"%v\"\n", aliasName, aliasCommand)
} else {
ps1prefix := "ps1_"
fmt.Fprintf(w, "function %v%v {%v}\n", ps1prefix, aliasName, aliasCommand)
fmt.Fprintf(w, "New-Alias -Name %v -Value %v%v\n", aliasName, ps1prefix, aliasName)
}

}

var comment string
Expand All @@ -121,7 +135,13 @@ func GenerateAlias(w io.Writer, aliases *types.AliasCMDs) error {
comment = alias.Comment
}

fmt.Fprintf(w, "alias %v=\"%v\"\n", alias.Short, alias.CMD)
if !powershell {
fmt.Fprintf(w, "alias %v=\"%v\"\n", alias.Short, alias.CMD)
} else {
ps1prefix := "ps1_"
fmt.Fprintf(w, "function %v%v {%v}\n", ps1prefix, alias.Short, alias.CMD)
fmt.Fprintf(w, "New-Alias -Name %v -Value %v%v\n", alias.Short, ps1prefix, alias.Short)
}
}
return nil
}
4 changes: 2 additions & 2 deletions generate/pkg/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestGenerateDuplicates(t *testing.T) {

for _, tc := range tt {

err := Generate(tc.aliasYAML, tc.outputFile)
err := Generate(tc.aliasYAML, tc.outputFile, false)

if tc.err == "" {
require.NoError(t, err)
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestGenerateAlias(t *testing.T) {

for _, c := range tcs {
var b strings.Builder
err := GenerateAlias(&b, c.Input)
err := GenerateAlias(&b, c.Input, false)
recieved := b.String()
if err != nil && c.Err == nil {
t.Errorf("Received err generating aliases: {%v}", err)
Expand Down
3 changes: 3 additions & 0 deletions kube-aliases.plugin.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SHELL_NAME=$(basename $SHELL)

source ./aliases.sh