summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/cmd.go128
-rw-r--r--cmd/install/bash.go37
-rw-r--r--cmd/install/fish.go69
-rw-r--r--cmd/install/install.go148
-rw-r--r--cmd/install/utils.go140
-rw-r--r--cmd/install/zsh.go44
6 files changed, 0 insertions, 566 deletions
diff --git a/cmd/cmd.go b/cmd/cmd.go
deleted file mode 100644
index b99fe52..0000000
--- a/cmd/cmd.go
+++ /dev/null
@@ -1,128 +0,0 @@
-// Package cmd used for command line options for the complete tool
-package cmd
-
-import (
- "errors"
- "flag"
- "fmt"
- "os"
- "strings"
-
- "github.com/posener/complete/cmd/install"
-)
-
-// CLI for command line
-type CLI struct {
- Name string
- InstallName string
- UninstallName string
-
- install bool
- uninstall bool
- yes bool
-}
-
-const (
- defaultInstallName = "install"
- defaultUninstallName = "uninstall"
-)
-
-// Run is used when running complete in command line mode.
-// this is used when the complete is not completing words, but to
-// install it or uninstall it.
-func (f *CLI) Run() bool {
- err := f.validate()
- if err != nil {
- os.Stderr.WriteString(err.Error() + "\n")
- os.Exit(1)
- }
-
- switch {
- case f.install:
- f.prompt()
- err = install.Install(f.Name)
- case f.uninstall:
- f.prompt()
- err = install.Uninstall(f.Name)
- default:
- // non of the action flags matched,
- // returning false should make the real program execute
- return false
- }
-
- if err != nil {
- fmt.Printf("%s failed! %s\n", f.action(), err)
- os.Exit(3)
- }
- fmt.Println("Done!")
- return true
-}
-
-// prompt use for approval
-// exit if approval was not given
-func (f *CLI) prompt() {
- defer fmt.Println(f.action() + "ing...")
- if f.yes {
- return
- }
- fmt.Printf("%s completion for %s? ", f.action(), f.Name)
- var answer string
- fmt.Scanln(&answer)
-
- switch strings.ToLower(answer) {
- case "y", "yes":
- return
- default:
- fmt.Println("Cancelling...")
- os.Exit(1)
- }
-}
-
-// AddFlags adds the CLI flags to the flag set.
-// If flags is nil, the default command line flags will be taken.
-// Pass non-empty strings as installName and uninstallName to override the default
-// flag names.
-func (f *CLI) AddFlags(flags *flag.FlagSet) {
- if flags == nil {
- flags = flag.CommandLine
- }
-
- if f.InstallName == "" {
- f.InstallName = defaultInstallName
- }
- if f.UninstallName == "" {
- f.UninstallName = defaultUninstallName
- }
-
- if flags.Lookup(f.InstallName) == nil {
- flags.BoolVar(&f.install, f.InstallName, false,
- fmt.Sprintf("Install completion for %s command", f.Name))
- }
- if flags.Lookup(f.UninstallName) == nil {
- flags.BoolVar(&f.uninstall, f.UninstallName, false,
- fmt.Sprintf("Uninstall completion for %s command", f.Name))
- }
- if flags.Lookup("y") == nil {
- flags.BoolVar(&f.yes, "y", false, "Don't prompt user for typing 'yes' when installing completion")
- }
-}
-
-// validate the CLI
-func (f *CLI) validate() error {
- if f.install && f.uninstall {
- return errors.New("Install and uninstall are mutually exclusive")
- }
- return nil
-}
-
-// action name according to the CLI values.
-func (f *CLI) action() string {
- switch {
- case f.install:
- return "Install"
- case f.uninstall:
- return "Uninstall"
- default:
- return "unknown"
- }
-}
diff --git a/cmd/install/bash.go b/cmd/install/bash.go
deleted file mode 100644
index 17c64de..0000000
--- a/cmd/install/bash.go
+++ /dev/null
@@ -1,37 +0,0 @@
-package install
-
-import "fmt"
-
-// (un)install in bash
-// basically adds/remove from .bashrc:
-//
-// complete -C </path/to/completion/command> <command>
-type bash struct {
- rc string
-}
-
-func (b bash) IsInstalled(cmd, bin string) bool {
- completeCmd := b.cmd(cmd, bin)
- return lineInFile(b.rc, completeCmd)
-}
-
-func (b bash) Install(cmd, bin string) error {
- if b.IsInstalled(cmd, bin) {
- return fmt.Errorf("already installed in %s", b.rc)
- }
- completeCmd := b.cmd(cmd, bin)
- return appendToFile(b.rc, completeCmd)
-}
-
-func (b bash) Uninstall(cmd, bin string) error {
- if !b.IsInstalled(cmd, bin) {
- return fmt.Errorf("does not installed in %s", b.rc)
- }
-
- completeCmd := b.cmd(cmd, bin)
- return removeFromFile(b.rc, completeCmd)
-}
-
-func (bash) cmd(cmd, bin string) string {
- return fmt.Sprintf("complete -C %s %s", bin, cmd)
-}
diff --git a/cmd/install/fish.go b/cmd/install/fish.go
deleted file mode 100644
index 2b64bfc..0000000
--- a/cmd/install/fish.go
+++ /dev/null
@@ -1,69 +0,0 @@
-package install
-
-import (
- "bytes"
- "fmt"
- "os"
- "path/filepath"
- "text/template"
-)
-
-// (un)install in fish
-
-type fish struct {
- configDir string
-}
-
-func (f fish) IsInstalled(cmd, bin string) bool {
- completionFile := f.getCompletionFilePath(cmd)
- if _, err := os.Stat(completionFile); err == nil {
- return true
- }
- return false
-}
-
-func (f fish) Install(cmd, bin string) error {
- if f.IsInstalled(cmd, bin) {
- return fmt.Errorf("already installed at %s", f.getCompletionFilePath(cmd))
- }
-
- completionFile := f.getCompletionFilePath(cmd)
- completeCmd, err := f.cmd(cmd, bin)
- if err != nil {
- return err
- }
-
- return createFile(completionFile, completeCmd)
-}
-
-func (f fish) Uninstall(cmd, bin string) error {
- if !f.IsInstalled(cmd, bin) {
- return fmt.Errorf("does not installed in %s", f.configDir)
- }
-
- completionFile := f.getCompletionFilePath(cmd)
- return os.Remove(completionFile)
-}
-
-func (f fish) getCompletionFilePath(cmd string) string {
- return filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
-}
-
-func (f fish) cmd(cmd, bin string) (string, error) {
- var buf bytes.Buffer
- params := struct{ Cmd, Bin string }{cmd, bin}
- tmpl := template.Must(template.New("cmd").Parse(`
-function __complete_{{.Cmd}}
- set -lx COMP_LINE (commandline -cp)
- test -z (commandline -ct)
- and set COMP_LINE "$COMP_LINE "
- {{.Bin}}
-end
-complete -f -c {{.Cmd}} -a "(__complete_{{.Cmd}})"
-`))
- err := tmpl.Execute(&buf, params)
- if err != nil {
- return "", err
- }
- return buf.String(), nil
-}
diff --git a/cmd/install/install.go b/cmd/install/install.go
deleted file mode 100644
index 884c23f..0000000
--- a/cmd/install/install.go
+++ /dev/null
@@ -1,148 +0,0 @@
-package install
-
-import (
- "errors"
- "os"
- "os/user"
- "path/filepath"
- "runtime"
-
- "github.com/hashicorp/go-multierror"
-)
-
-type installer interface {
- IsInstalled(cmd, bin string) bool
- Install(cmd, bin string) error
- Uninstall(cmd, bin string) error
-}
-
-// Install complete command given:
-// cmd: is the command name
-func Install(cmd string) error {
- is := installers()
- if len(is) == 0 {
- return errors.New("Did not find any shells to install")
- }
- bin, err := getBinaryPath()
- if err != nil {
- return err
- }
-
- for _, i := range is {
- errI := i.Install(cmd, bin)
- if errI != nil {
- err = multierror.Append(err, errI)
- }
- }
-
- return err
-}
-
-// IsInstalled returns true if the completion
-// for the given cmd is installed.
-func IsInstalled(cmd string) bool {
- bin, err := getBinaryPath()
- if err != nil {
- return false
- }
-
- for _, i := range installers() {
- installed := i.IsInstalled(cmd, bin)
- if installed {
- return true
- }
- }
-
- return false
-}
-
-// Uninstall complete command given:
-// cmd: is the command name
-func Uninstall(cmd string) error {
- is := installers()
- if len(is) == 0 {
- return errors.New("Did not find any shells to uninstall")
- }
- bin, err := getBinaryPath()
- if err != nil {
- return err
- }
-
- for _, i := range is {
- errI := i.Uninstall(cmd, bin)
- if errI != nil {
- err = multierror.Append(err, errI)
- }
- }
-
- return err
-}
-
-func installers() (i []installer) {
- // The list of bash config files candidates where it is
- // possible to install the completion command.
- var bashConfFiles []string
- switch runtime.GOOS {
- case "darwin":
- bashConfFiles = []string{".bash_profile"}
- default:
- bashConfFiles = []string{".bashrc", ".bash_profile", ".bash_login", ".profile"}
- }
- for _, rc := range bashConfFiles {
- if f := rcFile(rc); f != "" {
- i = append(i, bash{f})
- break
- }
- }
- if f := rcFile(".zshrc"); f != "" {
- i = append(i, zsh{f})
- }
- if d := fishConfigDir(); d != "" {
- i = append(i, fish{d})
- }
- return
-}
-
-func fishConfigDir() string {
- configDir := filepath.Join(getConfigHomePath(), "fish")
- if configDir == "" {
- return ""
- }
- if info, err := os.Stat(configDir); err != nil || !info.IsDir() {
- return ""
- }
- return configDir
-}
-
-func getConfigHomePath() string {
- u, err := user.Current()
- if err != nil {
- return ""
- }
-
- configHome := os.Getenv("XDG_CONFIG_HOME")
- if configHome == "" {
- return filepath.Join(u.HomeDir, ".config")
- }
- return configHome
-}
-
-func getBinaryPath() (string, error) {
- bin, err := os.Executable()
- if err != nil {
- return "", err
- }
- return filepath.Abs(bin)
-}
-
-func rcFile(name string) string {
- u, err := user.Current()
- if err != nil {
- return ""
- }
- path := filepath.Join(u.HomeDir, name)
- if _, err := os.Stat(path); err != nil {
- return ""
- }
- return path
-}
diff --git a/cmd/install/utils.go b/cmd/install/utils.go
deleted file mode 100644
index d34ac8c..0000000
--- a/cmd/install/utils.go
+++ /dev/null
@@ -1,140 +0,0 @@
-package install
-
-import (
- "bufio"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "path/filepath"
-)
-
-func lineInFile(name string, lookFor string) bool {
- f, err := os.Open(name)
- if err != nil {
- return false
- }
- defer f.Close()
- r := bufio.NewReader(f)
- prefix := []byte{}
- for {
- line, isPrefix, err := r.ReadLine()
- if err == io.EOF {
- return false
- }
- if err != nil {
- return false
- }
- if isPrefix {
- prefix = append(prefix, line...)
- continue
- }
- line = append(prefix, line...)
- if string(line) == lookFor {
- return true
- }
- prefix = prefix[:0]
- }
-}
-
-func createFile(name string, content string) error {
- // make sure file directory exists
- if err := os.MkdirAll(filepath.Dir(name), 0775); err != nil {
- return err
- }
-
- // create the file
- f, err := os.Create(name)
- if err != nil {
- return err
- }
- defer f.Close()
-
- // write file content
- _, err = f.WriteString(fmt.Sprintf("%s\n", content))
- return err
-}
-
-func appendToFile(name string, content string) error {
- f, err := os.OpenFile(name, os.O_RDWR|os.O_APPEND, 0)
- if err != nil {
- return err
- }
- defer f.Close()
- _, err = f.WriteString(fmt.Sprintf("\n%s\n", content))
- return err
-}
-
-func removeFromFile(name string, content string) error {
- backup := name + ".bck"
- err := copyFile(name, backup)
- if err != nil {
- return err
- }
- temp, err := removeContentToTempFile(name, content)
- if err != nil {
- return err
- }
-
- err = copyFile(temp, name)
- if err != nil {
- return err
- }
-
- return os.Remove(backup)
-}
-
-func removeContentToTempFile(name, content string) (string, error) {
- rf, err := os.Open(name)
- if err != nil {
- return "", err
- }
- defer rf.Close()
- wf, err := ioutil.TempFile("/tmp", "complete-")
- if err != nil {
- return "", err
- }
- defer wf.Close()
-
- r := bufio.NewReader(rf)
- prefix := []byte{}
- for {
- line, isPrefix, err := r.ReadLine()
- if err == io.EOF {
- break
- }
- if err != nil {
- return "", err
- }
- if isPrefix {
- prefix = append(prefix, line...)
- continue
- }
- line = append(prefix, line...)
- str := string(line)
- if str == content {
- continue
- }
- _, err = wf.WriteString(str + "\n")
- if err != nil {
- return "", err
- }
- prefix = prefix[:0]
- }
- return wf.Name(), nil
-}
-
-func copyFile(src string, dst string) error {
- in, err := os.Open(src)
- if err != nil {
- return err
- }
- defer in.Close()
- out, err := os.Create(dst)
- if err != nil {
- return err
- }
- defer out.Close()
- _, err = io.Copy(out, in)
- return err
-}
diff --git a/cmd/install/zsh.go b/cmd/install/zsh.go
deleted file mode 100644
index 29950ab..0000000
--- a/cmd/install/zsh.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package install
-
-import "fmt"
-
-// (un)install in zsh
-// basically adds/remove from .zshrc:
-//
-// autoload -U +X bashcompinit && bashcompinit"
-// complete -C </path/to/completion/command> <command>
-type zsh struct {
- rc string
-}
-
-func (z zsh) IsInstalled(cmd, bin string) bool {
- completeCmd := z.cmd(cmd, bin)
- return lineInFile(z.rc, completeCmd)
-}
-
-func (z zsh) Install(cmd, bin string) error {
- if z.IsInstalled(cmd, bin) {
- return fmt.Errorf("already installed in %s", z.rc)
- }
-
- completeCmd := z.cmd(cmd, bin)
- bashCompInit := "autoload -U +X bashcompinit && bashcompinit"
- if !lineInFile(z.rc, bashCompInit) {
- completeCmd = bashCompInit + "\n" + completeCmd
- }
-
- return appendToFile(z.rc, completeCmd)
-}
-
-func (z zsh) Uninstall(cmd, bin string) error {
- if !z.IsInstalled(cmd, bin) {
- return fmt.Errorf("does not installed in %s", z.rc)
- }
-
- completeCmd := z.cmd(cmd, bin)
- return removeFromFile(z.rc, completeCmd)
-}
-
-func (zsh) cmd(cmd, bin string) string {
- return fmt.Sprintf("complete -o nospace -C %s %s", bin, cmd)
-}