Skip to content

Commit

Permalink
Write completion to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
nigimaxx committed Sep 1, 2021
1 parent e581819 commit 041002f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ sudo chmod 666 /var/log/procgo/daemon.log
### Completion

```sh
procgo completion
# Write to some location in FPATH

# Move to some location in FPATH
mv _procgo /usr/local/share/zsh/site-functions
procgo completion > /usr/local/share/zsh/site-functions/_procgo
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ cd ../daemon
go build -o procgo-daemon

cd ..
procgo completion
procgo completion > /usr/local/share/zsh/site-functions/_procgo
18 changes: 8 additions & 10 deletions client/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"bytes"
"os"
"fmt"

"github.com/spf13/cobra"
)
Expand All @@ -19,13 +19,10 @@ var completionCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, _ []string) error {
// code copied from https://github.com/kubernetes/kubectl/blob/031e3ab456e6d0e676aecd163e52fcb4906e1faf/pkg/cmd/completion/completion.go

f, err := os.Create("_procgo")
if err != nil {
return err
}
buf := new(bytes.Buffer)

zshHead := "#compdef procgo\n"
f.Write([]byte(zshHead))
buf.Write([]byte(zshHead))

zshInitialization := `
__procgo_bash_source() {
Expand Down Expand Up @@ -115,18 +112,19 @@ __procgo_convert_bash_to_zsh() {
-e "s/\\\$(type${RWORD}/\$(__procgo_type/g" \
<<'BASH_COMPLETION_EOF'
`
f.Write([]byte(zshInitialization))
buf.Write([]byte(zshInitialization))

buf := new(bytes.Buffer)
cmd.Root().GenBashCompletion(buf)
f.Write(buf.Bytes())

zshTail := `
BASH_COMPLETION_EOF
}
__procgo_bash_source <(__procgo_convert_bash_to_zsh)
`
f.Write([]byte(zshTail))
buf.Write([]byte(zshTail))

fmt.Print(buf.String())

return nil
},
}
3 changes: 1 addition & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ cwd=$(pwd)
ln -s "$cwd/client/procgo" /usr/local/bin
ln -s "$cwd/daemon/procgo-daemon" /usr/local/bin

procgo completion
ln -s "$cwd/_procgo" /usr/local/share/zsh/site-functions
procgo completion > /usr/local/share/zsh/site-functions/_procgo

sudo mkdir /var/log/procgo
sudo touch /var/log/procgo/daemon.log
Expand Down

0 comments on commit 041002f

Please sign in to comment.