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

     1  package install
     2  
     3  import "fmt"
     4  
     5  // (un)install in bash
     6  // basically adds/remove from .bashrc:
     7  //
     8  // complete -C </path/to/completion/command> <command>
     9  type bash struct {
    10  	rc string
    11  }
    12  
    13  func (b bash) IsInstalled(cmd, bin string) bool {
    14  	completeCmd := b.cmd(cmd, bin)
    15  	return lineInFile(b.rc, completeCmd)
    16  }
    17  
    18  func (b bash) Install(cmd, bin string) error {
    19  	if b.IsInstalled(cmd, bin) {
    20  		return fmt.Errorf("already installed in %s", b.rc)
    21  	}
    22  	completeCmd := b.cmd(cmd, bin)
    23  	return appendToFile(b.rc, completeCmd)
    24  }
    25  
    26  func (b bash) Uninstall(cmd, bin string) error {
    27  	if !b.IsInstalled(cmd, bin) {
    28  		return fmt.Errorf("does not installed in %s", b.rc)
    29  	}
    30  
    31  	completeCmd := b.cmd(cmd, bin)
    32  	return removeFromFile(b.rc, completeCmd)
    33  }
    34  
    35  func (bash) cmd(cmd, bin string) string {
    36  	return fmt.Sprintf("complete -C %s %s", bin, cmd)
    37  }