github.com/alexandrestein/complete@v1.1.2-0.20180313112007-cc6c1c3aa2ce/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) Install(cmd, bin string) error {
    15  	completeCmd := z.cmd(cmd, bin)
    16  	if lineInFile(z.rc, completeCmd) {
    17  		return fmt.Errorf("already installed in %s", z.rc)
    18  	}
    19  
    20  	bashCompInit := "autoload -U +X bashcompinit && bashcompinit"
    21  	if !lineInFile(z.rc, bashCompInit) {
    22  		completeCmd = bashCompInit + "\n" + completeCmd
    23  	}
    24  
    25  	return appendToFile(z.rc, completeCmd)
    26  }
    27  
    28  func (z zsh) Uninstall(cmd, bin string) error {
    29  	completeCmd := z.cmd(cmd, bin)
    30  	if !lineInFile(z.rc, completeCmd) {
    31  		return fmt.Errorf("does not installed in %s", z.rc)
    32  	}
    33  
    34  	return removeFromFile(z.rc, completeCmd)
    35  }
    36  
    37  func (zsh) cmd(cmd, bin string) string {
    38  	return fmt.Sprintf("complete -o nospace -C %s %s", bin, cmd)
    39  }