2022-04-03 04:07:16 +00:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
// (un)install in fish
|
|
|
|
|
|
|
|
type fish struct {
|
|
|
|
configDir string
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:14:23 +00:00
|
|
|
func (f fish) IsInstalled(cmd, bin string) bool {
|
|
|
|
completionFile := f.getCompletionFilePath(cmd)
|
2022-04-03 04:07:16 +00:00
|
|
|
if _, err := os.Stat(completionFile); err == nil {
|
2022-12-18 17:14:23 +00:00
|
|
|
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
|
2022-04-03 04:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return createFile(completionFile, completeCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fish) Uninstall(cmd, bin string) error {
|
2022-12-18 17:14:23 +00:00
|
|
|
if !f.IsInstalled(cmd, bin) {
|
2022-04-03 04:07:16 +00:00
|
|
|
return fmt.Errorf("does not installed in %s", f.configDir)
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:14:23 +00:00
|
|
|
completionFile := f.getCompletionFilePath(cmd)
|
2022-04-03 04:07:16 +00:00
|
|
|
return os.Remove(completionFile)
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:14:23 +00:00
|
|
|
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) {
|
2022-04-03 04:07:16 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
params := struct{ Cmd, Bin string }{cmd, bin}
|
2022-12-18 17:14:23 +00:00
|
|
|
tmpl := template.Must(template.New("cmd").Parse(`
|
2022-04-03 04:07:16 +00:00
|
|
|
function __complete_{{.Cmd}}
|
2022-12-18 17:14:23 +00:00
|
|
|
set -lx COMP_LINE (commandline -cp)
|
|
|
|
test -z (commandline -ct)
|
2022-04-03 04:07:16 +00:00
|
|
|
and set COMP_LINE "$COMP_LINE "
|
|
|
|
{{.Bin}}
|
|
|
|
end
|
2022-12-18 17:14:23 +00:00
|
|
|
complete -f -c {{.Cmd}} -a "(__complete_{{.Cmd}})"
|
|
|
|
`))
|
|
|
|
err := tmpl.Execute(&buf, params)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return buf.String(), nil
|
2022-04-03 04:07:16 +00:00
|
|
|
}
|