-
Notifications
You must be signed in to change notification settings - Fork 94
Add command line autocomplete to the fs commands #1622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
0095d15
Add autocomplete to fs commands
andersrexdb e8e93fd
Add tests
andersrexdb 300ab07
Lint
andersrexdb ba64fd3
Fix TestGlobFileset
andersrexdb 5d6a348
Comments
andersrexdb e3f6a73
Add onlyDirs
andersrexdb 787d563
Add HasWorkspaceClient
andersrexdb a43b4af
Add file+dir test
andersrexdb 13eb013
PR comments
andersrexdb 3856332
Programmatically add dbfs:/Volumes
andersrexdb c73c09b
PR feedback
andersrexdb 363bd1d
Client
andersrexdb 0716f31
Comment
andersrexdb 0341e8b
Cleanup
andersrexdb a42f121
Support .\ local paths with Windows
andersrexdb 6d10776
Handle local Windows paths
andersrexdb 7c010cc
Support both separators on Windows
andersrexdb e0d2062
Fix test
andersrexdb d990fe2
Fix test 2
andersrexdb 1868791
PR feedback
andersrexdb c4a406c
Remove goroutine
andersrexdb a909d1f
Doc comments
andersrexdb 6091e0d
Use path and filepath for joining paths
andersrexdb 1591896
Use struct for Validate
andersrexdb 84229be
Lowercase validArgs
andersrexdb 058183e
Add integration test
andersrexdb 5df74cf
Fix error
andersrexdb 0803381
DRY up test
andersrexdb fb90be9
Use fakeFiler in tests
andersrexdb d802ba7
Remove PrepFakeFiler
andersrexdb 5261a2c
Remove call to current user
andersrexdb 3514151
Add integration test
andersrexdb 2057aad
Put back GetEnvOrSkipTest
andersrexdb 25e4443
PR feedback
andersrexdb 1b168ae
Cleanup
andersrexdb c2ae3f0
Simplify path.Dir logic
andersrexdb e5b32fc
Comments
andersrexdb 81775a4
Cleanup
andersrexdb 57ba288
Add windows test
andersrexdb 0eac4e7
PR comments
andersrexdb 6c85d51
Fix TestGlobFileset
andersrexdb 324e4ab
Try again
andersrexdb 354f4b9
Dont use ../filer in test
andersrexdb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.