Skip to content

Commit

Permalink
embed zsh completions as stringdata
Browse files Browse the repository at this point in the history
  • Loading branch information
kraashen committed Nov 24, 2023
1 parent c173682 commit 08934cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
22 changes: 0 additions & 22 deletions completions/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
Expand All @@ -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
}
8 changes: 1 addition & 7 deletions completions/completions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package completions
import (
"bytes"
"io"
"io/ioutil"
"testing"
)

Expand Down Expand Up @@ -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
Expand All @@ -72,7 +66,7 @@ func TestGenerateCompletions(t *testing.T) {
{
name: "zsh",
cmpl: &Zsh{},
expected: string(zshTestData),
expected: string(ZshCompletions),
err: false,
},
}
Expand Down
21 changes: 20 additions & 1 deletion completions/mage.zsh → completions/zsh.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
6 changes: 2 additions & 4 deletions mage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"
"time"

"github.com/magefile/mage/completions"
"github.com/magefile/mage/internal"
"github.com/magefile/mage/mg"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 08934cb

Please sign in to comment.