Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bundle/resources/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"fmt"
"strings"

"github.com/databricks/cli/bundle/config/resources"

Expand Down Expand Up @@ -98,3 +99,15 @@ func Lookup(b *bundle.Bundle, key string, filters ...Filter) (Reference, error)
panic("unreachable")
}
}

// LookupBySubstring returns all resources whose key contains the given substring.
func LookupBySubstring(b *bundle.Bundle, substr string, filters ...Filter) []Reference {
keyOnly, _ := References(b, filters...)
var matches []Reference
for k, refs := range keyOnly {
if strings.Contains(k, substr) {
matches = append(matches, refs...)
}
}
return matches
}
147 changes: 147 additions & 0 deletions bundle/resources/lookup_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resources

import (
"sort"
"testing"

"github.com/databricks/cli/bundle"
Expand Down Expand Up @@ -128,3 +129,149 @@ func TestLookup_NominalWithFilters(t *testing.T) {
require.Error(t, err)
assert.ErrorContains(t, err, `resource with key "bar" not found`)
}

func TestLookupBySubstring_NoMatches(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"foo": {},
"bar": {},
},
},
},
}

matches := LookupBySubstring(b, "qux")
assert.Empty(t, matches)
}

func TestLookupBySubstring_SingleMatch(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"foo_job": {
JobSettings: jobs.JobSettings{Name: "Foo job"},
},
"bar_job": {},
},
},
},
}

matches := LookupBySubstring(b, "foo")
require.Len(t, matches, 1)
assert.Equal(t, "foo_job", matches[0].Key)
assert.Equal(t, "Foo job", matches[0].Resource.GetName())
}

func TestLookupBySubstring_MultipleMatches(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"my_job_1": {},
"my_job_2": {},
"other": {},
},
},
},
}

matches := LookupBySubstring(b, "my_")
require.Len(t, matches, 2)

keys := []string{matches[0].Key, matches[1].Key}
sort.Strings(keys)
assert.Equal(t, []string{"my_job_1", "my_job_2"}, keys)
}

func TestLookupBySubstring_WithFilters(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"my_job": {},
},
Pipelines: map[string]*resources.Pipeline{
"my_pipeline": {},
},
},
},
}

includeJobs := func(ref Reference) bool {
_, ok := ref.Resource.(*resources.Job)
return ok
}

matches := LookupBySubstring(b, "my_", includeJobs)
require.Len(t, matches, 1)
assert.Equal(t, "my_job", matches[0].Key)
}

func TestLookupBySubstring_MiddleMatch(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"my_foo_job": {
JobSettings: jobs.JobSettings{Name: "My Foo Job"},
},
"bar_job": {},
},
},
},
}

matches := LookupBySubstring(b, "foo")
require.Len(t, matches, 1)
assert.Equal(t, "my_foo_job", matches[0].Key)
}

func TestLookupBySubstring_SuffixMatch(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"my_foo_job": {},
"my_bar_job": {},
"pipeline": {},
},
},
},
}

matches := LookupBySubstring(b, "_job")
require.Len(t, matches, 2)

keys := []string{matches[0].Key, matches[1].Key}
sort.Strings(keys)
assert.Equal(t, []string{"my_bar_job", "my_foo_job"}, keys)
}

func TestLookupBySubstring_ExactMatchAndContains(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"foo": {},
"foobar": {},
"foobaz": {},
"another": {},
},
},
},
}

matches := LookupBySubstring(b, "foo")
require.Len(t, matches, 3)

keys := make([]string, len(matches))
for i, m := range matches {
keys[i] = m.Key
}
sort.Strings(keys)
assert.Equal(t, []string{"foo", "foobar", "foobaz"}, keys)
}
35 changes: 34 additions & 1 deletion cmd/bundle/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,40 @@ func resolveOpenArgument(ctx context.Context, b *bundle.Bundle, args []string) (
return "", errors.New("expected a KEY of the resource to open")
}

return args[0], nil
arg := args[0]

// Check for an exact match first.
completions := resources.Completions(b)
if _, ok := completions[arg]; ok {
return arg, nil
}

// Check for substring matches.
matches := resources.LookupBySubstring(b, arg)
switch {
case len(matches) == 1:
return matches[0].Key, nil
case len(matches) > 1:
if cmdio.IsPromptSupported(ctx) {
// Show a filtered prompt with only matching resources.
inv := make(map[string]string)
for _, ref := range matches {
title := fmt.Sprintf("%s: %s", ref.Description.SingularTitle, ref.Resource.GetName())
inv[title] = ref.Key
}
return cmdio.Select(ctx, inv, "Resource to open")
}

// Non-interactive: return error listing candidates.
keys := make([]string, 0, len(matches))
for _, ref := range matches {
keys = append(keys, ref.Key)
}
return "", fmt.Errorf("multiple resources match %q: %v", arg, keys)
}

// No matches; return the arg as-is and let Lookup handle the "not found" error.
return arg, nil
}

func newOpenCommand() *cobra.Command {
Expand Down
116 changes: 116 additions & 0 deletions cmd/bundle/open_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package bundle

import (
"context"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestResolveOpenArgument_NoArgs_NonInteractive(t *testing.T) {
ctx := cmdio.MockDiscard(context.Background())
b := &bundle.Bundle{}

_, err := resolveOpenArgument(ctx, b, nil)
require.Error(t, err)
assert.ErrorContains(t, err, "expected a KEY of the resource to open")
}

func TestResolveOpenArgument_ExactMatch(t *testing.T) {
ctx := cmdio.MockDiscard(context.Background())
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"my_job": {JobSettings: jobs.JobSettings{Name: "My Job"}},
"my_job_2": {JobSettings: jobs.JobSettings{Name: "My Job 2"}},
},
},
},
}

key, err := resolveOpenArgument(ctx, b, []string{"my_job"})
require.NoError(t, err)
assert.Equal(t, "my_job", key)
}

func TestResolveOpenArgument_SubstringSingleMatch(t *testing.T) {
ctx := cmdio.MockDiscard(context.Background())
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"foo_job": {JobSettings: jobs.JobSettings{Name: "Foo Job"}},
"bar_job": {JobSettings: jobs.JobSettings{Name: "Bar Job"}},
},
},
},
}

key, err := resolveOpenArgument(ctx, b, []string{"foo"})
require.NoError(t, err)
assert.Equal(t, "foo_job", key)
}

func TestResolveOpenArgument_SubstringMultipleMatches_NonInteractive(t *testing.T) {
ctx := cmdio.MockDiscard(context.Background())
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"my_job_1": {JobSettings: jobs.JobSettings{Name: "My Job 1"}},
"my_job_2": {JobSettings: jobs.JobSettings{Name: "My Job 2"}},
"other": {JobSettings: jobs.JobSettings{Name: "Other"}},
},
},
},
}

_, err := resolveOpenArgument(ctx, b, []string{"my_"})
require.Error(t, err)
assert.ErrorContains(t, err, "multiple resources match")
assert.ErrorContains(t, err, "my_job_1")
assert.ErrorContains(t, err, "my_job_2")
}

func TestResolveOpenArgument_SubstringMiddleMatch(t *testing.T) {
ctx := cmdio.MockDiscard(context.Background())
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"my_foo_job": {JobSettings: jobs.JobSettings{Name: "My Foo Job"}},
"bar_job": {JobSettings: jobs.JobSettings{Name: "Bar Job"}},
},
},
},
}

key, err := resolveOpenArgument(ctx, b, []string{"foo"})
require.NoError(t, err)
assert.Equal(t, "my_foo_job", key)
}

func TestResolveOpenArgument_SubstringNoMatch(t *testing.T) {
ctx := cmdio.MockDiscard(context.Background())
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"foo": {JobSettings: jobs.JobSettings{Name: "Foo"}},
},
},
},
}

// No substring match; returns the arg as-is.
key, err := resolveOpenArgument(ctx, b, []string{"zzz"})
require.NoError(t, err)
assert.Equal(t, "zzz", key)
}