github.com/gechr/complete@v0.0.0-20191016221035-401475e3ce1e/cmd/install/zsh.go (about)

     1  package install
     2  
     3  import "fmt"
     4  
     5  // (un)install in zsh
     6  // basically adds/remove from .zshrc:
     7  //
     8  // autoload -U +X bashcompinit && bashcompinit"
     9  // complete -C </path/to/completion/command> <command>
    10  type zsh struct {
    11  	rc string
    12  }
    13  
    14  func (z zsh) IsInstalled(cmd, bin string) bool {
    15  	completeCmd := z.cmd(cmd, bin)
    16  	return lineInFile(z.rc, completeCmd)
    17  }
    18  
    19  func (z zsh) Install(cmd, bin string) error {
    20  	if z.IsInstalled(cmd, bin) {
    21  		return fmt.Errorf("already installed in %s", z.rc)
    22  	}
    23  
    24  	completeCmd := z.cmd(cmd, bin)
    25  	bashCompInit := "autoload -U +X bashcompinit && bashcompinit"
    26  	if !lineInFile(z.rc, bashCompInit) {
    27  		completeCmd = bashCompInit + "\n" + completeCmd
    28  	}
    29  
    30  	return appendToFile(z.rc, completeCmd)
    31  }
    32  
    33  func (z zsh) Uninstall(cmd, bin string) error {
    34  	if !z.IsInstalled(cmd, bin) {
    35  		return fmt.Errorf("does not installed in %s", z.rc)
    36  	}
    37  
    38  	completeCmd := z.cmd(cmd, bin)
    39  	return removeFromFile(z.rc, completeCmd)
    40  }
    41  
    42  func (zsh) cmd(cmd, bin string) string {
    43  	return fmt.Sprintf("complete -o nospace -C %s %s", bin, cmd)
    44  }