diff --git a/completions/completions.go b/completions/completions.go index e98f852..a8c9478 100644 --- a/completions/completions.go +++ b/completions/completions.go @@ -3,17 +3,12 @@ package completions import ( "fmt" "io" - "io/ioutil" - "path/filepath" - "runtime" ) type Completion interface { GenerateCompletions(w io.Writer) error } -type Zsh struct{} - var completions = map[string]Completion{ "zsh": &Zsh{}, } @@ -27,20 +22,3 @@ func GetCompletions(shell string) (Completion, error) { return completions, nil } - -func (z *Zsh) GenerateCompletions(w io.Writer) error { - _, filename, _, _ := runtime.Caller(0) - dir := filepath.Dir(filename) - filePath := filepath.Join(dir, "mage.zsh") - - data, err := ioutil.ReadFile(filePath) - if err != nil { - return err - } - - _, err = fmt.Fprint(w, string(data)) - if err != nil { - return err - } - return nil -} diff --git a/completions/completions_test.go b/completions/completions_test.go index 71e0314..e49364d 100644 --- a/completions/completions_test.go +++ b/completions/completions_test.go @@ -3,7 +3,6 @@ package completions import ( "bytes" "io" - "io/ioutil" "testing" ) @@ -58,11 +57,6 @@ func TestGenerateCompletionsFails(t *testing.T) { } func TestGenerateCompletions(t *testing.T) { - zshTestData, err := ioutil.ReadFile("mage.zsh") - if err != nil { - t.Fatalf("expected to get test data, but got error: %v", err) - } - testCases := []struct { name string cmpl Completion @@ -72,7 +66,7 @@ func TestGenerateCompletions(t *testing.T) { { name: "zsh", cmpl: &Zsh{}, - expected: string(zshTestData), + expected: string(ZshCompletions), err: false, }, } diff --git a/completions/mage.zsh b/completions/zsh.go similarity index 89% rename from completions/mage.zsh rename to completions/zsh.go index 4f3a0dd..94d1afc 100644 --- a/completions/mage.zsh +++ b/completions/zsh.go @@ -1,4 +1,11 @@ -#compdef mage +package completions + +import ( + "fmt" + "io" +) + +var ZshCompletions string = `#compdef mage local curcontext="$curcontext" state line ret=1 typeset -A opt_args @@ -65,3 +72,15 @@ return ret # sh-basic-offset: 2 # End: # vim: ft=zsh sw=2 ts=2 et +` + +type Zsh struct{} + +func (z *Zsh) GenerateCompletions(w io.Writer) error { + _, err := fmt.Fprint(w, string(ZshCompletions)) + + if err != nil { + return err + } + return nil +} diff --git a/mage/main_test.go b/mage/main_test.go index f93db65..4abdf1e 100644 --- a/mage/main_test.go +++ b/mage/main_test.go @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/magefile/mage/completions" "github.com/magefile/mage/internal" "github.com/magefile/mage/mg" ) @@ -902,10 +903,7 @@ func TestCompletionFlag(t *testing.T) { t.Fatalf("expected code 0, but got %v, %s", code, stderr) } actual := stdout.String() - expectedData, err := ioutil.ReadFile("../completions/mage.zsh") - if err != nil { - t.Fatal(err) - } + expectedData := completions.ZshCompletions expected := string(expectedData) if actual != expected {