mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-07 09:36:44 +00:00
![Riley Karson](/assets/img/avatar_default.png)
```bash GO111MODULE=on go get github.com/hashicorp/terraform@pluginsdk-v0.12-early7 GO111MODULE=on go mod vendor GO111MODULE=on go mod tidy ```
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package install
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"text/template"
|
|
)
|
|
|
|
// (un)install in fish
|
|
|
|
type fish struct {
|
|
configDir string
|
|
}
|
|
|
|
func (f fish) Install(cmd, bin string) error {
|
|
completionFile := filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
|
|
completeCmd, err := f.cmd(cmd, bin)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := os.Stat(completionFile); err == nil {
|
|
return fmt.Errorf("already installed at %s", completionFile)
|
|
}
|
|
|
|
return createFile(completionFile, completeCmd)
|
|
}
|
|
|
|
func (f fish) Uninstall(cmd, bin string) error {
|
|
completionFile := filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
|
|
if _, err := os.Stat(completionFile); err != nil {
|
|
return fmt.Errorf("does not installed in %s", f.configDir)
|
|
}
|
|
|
|
return os.Remove(completionFile)
|
|
}
|
|
|
|
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 (string join ' ' (commandline -o))
|
|
test (commandline -ct) = ""
|
|
and set COMP_LINE "$COMP_LINE "
|
|
{{.Bin}}
|
|
end
|
|
complete -c {{.Cmd}} -a "(__complete_{{.Cmd}})"
|
|
`))
|
|
err := tmpl.Execute(&buf, params)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return buf.String(), nil
|
|
}
|