Skip to content

Commit

Permalink
update alias cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Dec 4, 2020
1 parent dd1a866 commit a4ad16d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
dist/
kubecm

target
target
tmp
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ test: fmt vet lint
go test -race -coverprofile=coverage.txt -covermode=atomic ./cmd/...

doc-gen:
rm -r docs/tmp/cli/*
ifneq ($(wildcard "tmp/cli"),)
rm -r tmp/cli/*
endif
go run hack/docgen/gen.go

GOLANGCILINT_VERSION ?= v1.29.0
Expand Down
56 changes: 39 additions & 17 deletions cmd/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package cmd
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -29,8 +31,11 @@ func (al *AliasCommand) Init() {
}
al.command.DisableFlagsInUseLine = true
al.command.Flags().StringP("out", "o", "", "output to ~/.zshrc or ~/.bash_profile")
al.command.MarkFlagRequired("out")
}

const SourceCmd = "[[ ! -f ~/.kubecm ]] || source ~/.kubecm"

func (al *AliasCommand) runAlias(command *cobra.Command, args []string) error {
config, err := clientcmd.LoadFromFile(cfgFile)
if err != nil {
Expand All @@ -49,21 +54,34 @@ alias %s='kubectl --context %s'`
}
output, _ := al.command.Flags().GetString("out")
result := fmt.Sprintf(allTemp, tmp)
err = updateFile(result, filepath.Join(homeDir(), ".kubecm"))
if err != nil {
return err
}
switch output {
case "bash":
err = writeAppend(result, filepath.Join(homeDir(), ".bash_profile"))
err = writeAppend(SourceCmd, filepath.Join(homeDir(), ".bash_profile"))
if err != nil {
return err
}
al.command.Println("「.bash_profile」 write successful!")
al.command.Println("「.bash_profile」 write successful!\nPlease enter command `source .bash_profile`")
case "zsh":
err = writeAppend(result, filepath.Join(homeDir(), ".zshrc"))
err = writeAppend(SourceCmd, filepath.Join(homeDir(), ".zshrc"))
if err != nil {
return err
}
al.command.Println("「.zshrc」 write successful!")
al.command.Println("「.zshrc」 write successful!\nPlease enter command `source .zshrc`")
default:
al.command.Print(result)
al.command.PrintErrln("Parameter error! Please input bash or zsh")
}

return nil
}

func updateFile(cxt, path string) error {
err := ioutil.WriteFile(path, []byte(cxt), 0644)
if err != nil {
return err
}
return nil
}
Expand All @@ -74,24 +92,28 @@ func writeAppend(context, path string) error {
return err
}
defer f.Close()
write := bufio.NewWriter(f)
//strings.TrimSpace(context)
_, _ = write.WriteString(context)
err = write.Flush()
if err != nil {
return err
var exist bool
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if ok := strings.EqualFold(SourceCmd, line); ok {
exist = true
break
}
}
if !exist {
write := bufio.NewWriter(f)
_, _ = write.WriteString(context+"\n")
err = write.Flush()
if err != nil {
return err
}
}
return nil
}

func aliasExample() string {
return `
# dev
alias k-dev='kubectl --context dev'
# test
alias k-test='kubectl --context test'
# prod
alias k-prod='kubectl --context prod'
$ kubecm alias -o zsh
# add context to ~/.zshrc
$ kubecm alias -o bash
Expand Down
8 changes: 1 addition & 7 deletions docs/en-us/cli/kubecm_alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ kubecm alias

```
# dev
alias k-dev='kubectl --context dev'
# test
alias k-test='kubectl --context test'
# prod
alias k-prod='kubectl --context prod'
$ kubecm alias -o zsh
# add context to ~/.zshrc
$ kubecm alias -o bash
Expand All @@ -37,5 +31,5 @@ $ kubecm alias -o bash
### Options inherited from parent commands

```
--config string path of kubeconfig (default "/Users/guoxudong/.kube/config")
--config string path of kubeconfig (default "/Users/saybot/.kube/config")
```
6 changes: 0 additions & 6 deletions docs/zh-cn/cli/kubecm_alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ kubecm alias

```
# dev
alias k-dev='kubectl --context dev'
# test
alias k-test='kubectl --context test'
# prod
alias k-prod='kubectl --context prod'
$ kubecm alias -o zsh
# add context to ~/.zshrc
$ kubecm alias -o bash
Expand Down
2 changes: 1 addition & 1 deletion hack/docgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func main() {
kubecm := cmd.NewBaseCommand().CobraCmd()
err := doc.GenMarkdownTree(kubecm, "./docs/tmp/cli/")
err := doc.GenMarkdownTree(kubecm, "./tmp/cli/")
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit a4ad16d

Please sign in to comment.