Skip to content

Commit

Permalink
Merge branch 'DeedleFake-completions-fs'
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratus3D committed Dec 19, 2024
2 parents 9c12b79 + 518a0fa commit d5f0214
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 52 deletions.
2 changes: 1 addition & 1 deletion cmd/asdf/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Main entrypoint for the CLI app
package main

import "github.com/asdf-vm/asdf/cli"
import "github.com/asdf-vm/asdf/internal/cli"

// Replaced with the real version during a typical build
var version = "v-dev"
Expand Down
51 changes: 12 additions & 39 deletions cli/cli.go → internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package cli

import (
_ "embed"
"errors"
"fmt"
"io"
Expand All @@ -14,6 +13,7 @@ import (
"strings"
"text/tabwriter"

"github.com/asdf-vm/asdf/internal/completions"
"github.com/asdf-vm/asdf/internal/config"
"github.com/asdf-vm/asdf/internal/exec"
"github.com/asdf-vm/asdf/internal/execenv"
Expand Down Expand Up @@ -52,7 +52,7 @@ func Execute(version string) {

app := &cli.App{
Name: "asdf",
Version: "0.1.0",
Version: version,
// Not really sure what I should put here, but all the new Golang code will
// likely be written by me.
Copyright: "(c) 2024 Trevor Brown",
Expand Down Expand Up @@ -315,45 +315,18 @@ func Execute(version string) {
}
}

//go:embed completions/asdf.bash
var bashCompletions string

//go:embed completions/asdf.zsh
var zshCompletions string

//go:embed completions/asdf.fish
var fishCompletions string

//go:embed completions/asdf.nu
var nuCompletions string

//go:embed completions/asdf.elv
var elvishCompletions string

func completionCommand(l *log.Logger, shell string) error {
switch shell {
case "bash":
fmt.Print(bashCompletions)
return nil
case "zsh":
fmt.Print(zshCompletions)
return nil
case "fish":
fmt.Print(fishCompletions)
return nil
case "nushell":
fmt.Print(nuCompletions)
return nil
case "elvish":
fmt.Print(elvishCompletions)
return nil
default:
fmtString := `No completions available for shell with name %s
Completions are available for: bash, zsh, fish, nushell, elvish`
msg := fmt.Sprintf(fmtString, shell)
l.Print(msg)
return errors.New(msg)
file, ok := completions.Get(shell)
if !ok {
l.Printf(`No completions available for shell with name %q
Completions are available for: %v`, shell, strings.Join(completions.Names(), ", "))
return errors.New("bad shell name")
}
defer file.Close()

io.Copy(os.Stdout, file)

return nil
}

// This function is a whole mess and needs to be refactored
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions internal/completions/completions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Package completions handles shell completion files.
//
// To add completion support for a shell, simply add a file named
// "asdf.<shell>" to this directory, replacing "<shell>" with the name
// of the shell.
package completions

import (
"embed"
"errors"
"io/fs"
"slices"
"strings"
)

//go:embed asdf.*
var completions embed.FS

// Get returns a file containing completion code for the given shell if it is
// found.
func Get(name string) (fs.File, bool) {
file, err := completions.Open("asdf." + name)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, false
}
panic(err) // This should never happen.
}
return file, true
}

// Names returns a slice of shell names that completion is available for.
func Names() []string {
files, _ := fs.Glob(completions, "asdf.*")
for i, file := range files {
files[i] = strings.TrimPrefix(file, "asdf.")
}
slices.Sort(files)
return files
}
30 changes: 30 additions & 0 deletions internal/completions/completions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package completions

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGet(t *testing.T) {
t.Run("returns file when completion file found with matching name", func(t *testing.T) {
file, found := Get("bash")

info, err := file.Stat()
assert.Nil(t, err)
assert.Equal(t, "asdf.bash", info.Name())

assert.True(t, found)
})

t.Run("returns false when completion file not found", func(t *testing.T) {
_, found := Get("non-existent")
assert.False(t, found)
})
}

func TestNames(t *testing.T) {
t.Run("returns slice of shell names for which completion is available", func(t *testing.T) {
assert.Equal(t, []string{"bash", "elvish", "fish", "nushell", "zsh"}, Names())
})
}
2 changes: 1 addition & 1 deletion internal/execenv/execenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/asdf-vm/asdf/internal/config"
"github.com/asdf-vm/asdf/internal/plugins"
"github.com/asdf-vm/asdf/repotest"
"github.com/asdf-vm/asdf/internal/repotest"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"
"testing"

"github.com/asdf-vm/asdf/repotest"
"github.com/asdf-vm/asdf/internal/repotest"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/stretchr/testify/assert"
Expand Down
2 changes: 1 addition & 1 deletion internal/help/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/asdf-vm/asdf/internal/config"
"github.com/asdf-vm/asdf/internal/plugins"
"github.com/asdf-vm/asdf/repotest"
"github.com/asdf-vm/asdf/internal/repotest"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/installs/installs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/asdf-vm/asdf/internal/config"
"github.com/asdf-vm/asdf/internal/installtest"
"github.com/asdf-vm/asdf/internal/plugins"
"github.com/asdf-vm/asdf/internal/repotest"
"github.com/asdf-vm/asdf/internal/toolversions"
"github.com/asdf-vm/asdf/repotest"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/pluginindex/pluginindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/asdf-vm/asdf/internal/git"
"github.com/asdf-vm/asdf/repotest"
"github.com/asdf-vm/asdf/internal/repotest"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/plugins/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/asdf-vm/asdf/internal/config"
"github.com/asdf-vm/asdf/internal/data"
"github.com/asdf-vm/asdf/repotest"
"github.com/asdf-vm/asdf/internal/repotest"
"github.com/stretchr/testify/assert"
)

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/resolve/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/asdf-vm/asdf/internal/config"
"github.com/asdf-vm/asdf/internal/plugins"
"github.com/asdf-vm/asdf/repotest"
"github.com/asdf-vm/asdf/internal/repotest"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/shims/shims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/asdf-vm/asdf/internal/installs"
"github.com/asdf-vm/asdf/internal/installtest"
"github.com/asdf-vm/asdf/internal/plugins"
"github.com/asdf-vm/asdf/internal/repotest"
"github.com/asdf-vm/asdf/internal/toolversions"
"github.com/asdf-vm/asdf/repotest"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/asdf-vm/asdf/internal/config"
"github.com/asdf-vm/asdf/internal/plugins"
"github.com/asdf-vm/asdf/internal/repotest"
"github.com/asdf-vm/asdf/internal/toolversions"
"github.com/asdf-vm/asdf/repotest"
"github.com/stretchr/testify/assert"
)

Expand Down
6 changes: 3 additions & 3 deletions scripts/lint.bash
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ run_shfmt_stylecheck() {

print.info "Checking .bash with shfmt"
shfmt --language-dialect bash --indent 2 "${shfmt_flag}" \
cli/completions/*.bash \
internal/completions/*.bash \
bin/asdf \
bin/private/asdf-exec \
lib/utils.bash \
Expand All @@ -55,7 +55,7 @@ run_shellcheck_linter() {

print.info "Checking .bash files with Shellcheck"
shellcheck --shell bash --external-sources \
cli/completions/*.bash \
internal/completions/*.bash \
bin/asdf \
bin/private/asdf-exec \
lib/utils.bash \
Expand Down Expand Up @@ -123,7 +123,7 @@ run_fish_linter() {
printf "%s\n" "[WARNING] fish_indent not found. Skipping .fish files."
else
print.info "Checking .fish files with fish_indent"
fish_indent "${flag}" ./cli/completions/asdf.fish
fish_indent "${flag}" ./completions/asdf.fish
fi
}

Expand Down

0 comments on commit d5f0214

Please sign in to comment.