-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command line autocomplete to the fs commands (#1622)
## Changes This PR adds autocomplete for cat, cp, ls, mkdir and rm. The new completer can do completion for any `Filer`. The command completion for the `sync` command can be moved to use this general completer as a follow-up. ## Tests - Tested manually against a workspace - Unit tests
- Loading branch information
1 parent
d3d828d
commit 65f4aad
Showing
13 changed files
with
532 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
_ "github.com/databricks/cli/cmd/fs" | ||
"github.com/databricks/cli/libs/filer" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func setupCompletionFile(t *testing.T, f filer.Filer) { | ||
err := f.Write(context.Background(), "dir1/file1.txt", strings.NewReader("abc"), filer.CreateParentDirectories) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestAccFsCompletion(t *testing.T) { | ||
f, tmpDir := setupDbfsFiler(t) | ||
setupCompletionFile(t, f) | ||
|
||
stdout, _ := RequireSuccessfulRun(t, "__complete", "fs", "ls", tmpDir) | ||
expectedOutput := fmt.Sprintf("%s/dir1/\n:2\n", tmpDir) | ||
assert.Equal(t, expectedOutput, stdout.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package completer | ||
|
||
import ( | ||
"context" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/databricks/cli/libs/filer" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type completer struct { | ||
ctx context.Context | ||
|
||
// The filer to use for completing remote or local paths. | ||
filer filer.Filer | ||
|
||
// CompletePath will only return directories when onlyDirs is true. | ||
onlyDirs bool | ||
|
||
// Prefix to prepend to completions. | ||
prefix string | ||
|
||
// Whether the path is local or remote. If the path is local we use the `filepath` | ||
// package for path manipulation. Otherwise we use the `path` package. | ||
isLocalPath bool | ||
} | ||
|
||
// General completer that takes a filer to complete remote paths when TAB-ing through a path. | ||
func New(ctx context.Context, filer filer.Filer, onlyDirs bool) *completer { | ||
return &completer{ctx: ctx, filer: filer, onlyDirs: onlyDirs, prefix: "", isLocalPath: true} | ||
} | ||
|
||
func (c *completer) SetPrefix(p string) { | ||
c.prefix = p | ||
} | ||
|
||
func (c *completer) SetIsLocalPath(i bool) { | ||
c.isLocalPath = i | ||
} | ||
|
||
func (c *completer) CompletePath(p string) ([]string, cobra.ShellCompDirective, error) { | ||
trailingSeparator := "/" | ||
joinFunc := path.Join | ||
|
||
// Use filepath functions if we are in a local path. | ||
if c.isLocalPath { | ||
joinFunc = filepath.Join | ||
trailingSeparator = string(filepath.Separator) | ||
} | ||
|
||
// If the user is TAB-ing their way through a path and the | ||
// path ends in a trailing slash, we should list nested directories. | ||
// If the path is incomplete, however, then we should list adjacent | ||
// directories. | ||
dirPath := p | ||
if !strings.HasSuffix(p, trailingSeparator) { | ||
dirPath = path.Dir(p) | ||
} | ||
|
||
entries, err := c.filer.ReadDir(c.ctx, dirPath) | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError, err | ||
} | ||
|
||
completions := []string{} | ||
for _, entry := range entries { | ||
if c.onlyDirs && !entry.IsDir() { | ||
continue | ||
} | ||
|
||
// Join directory path and entry name | ||
completion := joinFunc(dirPath, entry.Name()) | ||
|
||
// Prepend prefix if it has been set | ||
if c.prefix != "" { | ||
completion = joinFunc(c.prefix, completion) | ||
} | ||
|
||
// Add trailing separator for directories. | ||
if entry.IsDir() { | ||
completion += trailingSeparator | ||
} | ||
|
||
completions = append(completions, completion) | ||
} | ||
|
||
// If the path is local, we add the dbfs:/ prefix suggestion as an option | ||
if c.isLocalPath { | ||
completions = append(completions, "dbfs:/") | ||
} | ||
|
||
return completions, cobra.ShellCompDirectiveNoSpace, err | ||
} |
Oops, something went wrong.