mirror of https://github.com/databricks/cli.git
Compare commits
No commits in common. "03e9b5fa1a4ef106e1292f137c6fdde94188de9f" and "e6a8d8a4caae643863fa0bf5e510b651eaadfb07" have entirely different histories.
03e9b5fa1a
...
e6a8d8a4ca
|
@ -6,16 +6,21 @@ import (
|
||||||
"github.com/databricks/cli/bundle"
|
"github.com/databricks/cli/bundle"
|
||||||
"github.com/databricks/cli/bundle/config"
|
"github.com/databricks/cli/bundle/config"
|
||||||
"github.com/databricks/cli/bundle/config/resources"
|
"github.com/databricks/cli/bundle/config/resources"
|
||||||
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCompletions_SkipDuplicates(t *testing.T) {
|
func TestCompletions(t *testing.T) {
|
||||||
b := &bundle.Bundle{
|
b := &bundle.Bundle{
|
||||||
Config: config.Root{
|
Config: config.Root{
|
||||||
Resources: config.Resources{
|
Resources: config.Resources{
|
||||||
Jobs: map[string]*resources.Job{
|
Jobs: map[string]*resources.Job{
|
||||||
"foo": {},
|
"foo": {},
|
||||||
"bar": {},
|
"bar": {
|
||||||
|
JobSettings: &jobs.JobSettings{
|
||||||
|
Name: "Bar job",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Pipelines: map[string]*resources.Pipeline{
|
Pipelines: map[string]*resources.Pipeline{
|
||||||
"foo": {},
|
"foo": {},
|
||||||
|
|
|
@ -48,21 +48,21 @@ func References(b *bundle.Bundle) (Map, Map) {
|
||||||
// Lookup returns the resource with the specified key.
|
// Lookup returns the resource with the specified key.
|
||||||
// If the key maps to more than one resource, an error is returned.
|
// If the key maps to more than one resource, an error is returned.
|
||||||
// If the key does not map to any resource, an error is returned.
|
// If the key does not map to any resource, an error is returned.
|
||||||
func Lookup(b *bundle.Bundle, key string) (Reference, error) {
|
func Lookup(b *bundle.Bundle, key string) (config.ConfigResource, error) {
|
||||||
keyOnlyRefs, keyWithTypeRefs := References(b)
|
keyOnlyRefs, keyWithTypeRefs := References(b)
|
||||||
refs, ok := keyOnlyRefs[key]
|
refs, ok := keyOnlyRefs[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
refs, ok = keyWithTypeRefs[key]
|
refs, ok = keyWithTypeRefs[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return Reference{}, fmt.Errorf("resource with key %q not found", key)
|
return nil, fmt.Errorf("resource with key %q not found", key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case len(refs) == 1:
|
case len(refs) == 1:
|
||||||
return refs[0], nil
|
return refs[0].Resource, nil
|
||||||
case len(refs) > 1:
|
case len(refs) > 1:
|
||||||
return Reference{}, fmt.Errorf("multiple resources with key %q found", key)
|
return nil, fmt.Errorf("multiple resources with key %q found", key)
|
||||||
default:
|
default:
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,9 @@ func TestLookup_EmptyBundle(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := Lookup(b, "foo")
|
out, err := Lookup(b, "foo")
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
assert.Nil(t, out)
|
||||||
assert.ErrorContains(t, err, "resource with key \"foo\" not found")
|
assert.ErrorContains(t, err, "resource with key \"foo\" not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,8 +36,9 @@ func TestLookup_NotFound(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := Lookup(b, "qux")
|
out, err := Lookup(b, "qux")
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
assert.Nil(t, out)
|
||||||
assert.ErrorContains(t, err, `resource with key "qux" not found`)
|
assert.ErrorContains(t, err, `resource with key "qux" not found`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,8 +56,9 @@ func TestLookup_MultipleFound(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := Lookup(b, "foo")
|
out, err := Lookup(b, "foo")
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
assert.Nil(t, out)
|
||||||
assert.ErrorContains(t, err, `multiple resources with key "foo" found`)
|
assert.ErrorContains(t, err, `multiple resources with key "foo" found`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,13 +79,15 @@ func TestLookup_Nominal(t *testing.T) {
|
||||||
|
|
||||||
// Lookup by key only.
|
// Lookup by key only.
|
||||||
out, err := Lookup(b, "foo")
|
out, err := Lookup(b, "foo")
|
||||||
if assert.NoError(t, err) {
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "Foo job", out.Resource.GetName())
|
if assert.NotNil(t, out) {
|
||||||
|
assert.Equal(t, "Foo job", out.GetName())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lookup by type and key.
|
// Lookup by type and key.
|
||||||
out, err = Lookup(b, "jobs.foo")
|
out, err = Lookup(b, "jobs.foo")
|
||||||
if assert.NoError(t, err) {
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "Foo job", out.Resource.GetName())
|
if assert.NotNil(t, out) {
|
||||||
|
assert.Equal(t, "Foo job", out.GetName())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ func promptOpenArgument(ctx context.Context, b *bundle.Bundle) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveOpenArgument(ctx context.Context, b *bundle.Bundle, args []string) (string, error) {
|
func resolveOpenArgument(ctx context.Context, b *bundle.Bundle, args []string) (string, error) {
|
||||||
// If no arguments are specified, prompt the user to select the resource to open.
|
// If no arguments are specified, prompt the user to select something to run.
|
||||||
if len(args) == 0 && cmdio.IsPromptSupported(ctx) {
|
if len(args) == 0 && cmdio.IsPromptSupported(ctx) {
|
||||||
return promptOpenArgument(ctx, b)
|
return promptOpenArgument(ctx, b)
|
||||||
}
|
}
|
||||||
|
@ -105,13 +105,13 @@ func newOpenCommand() *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Locate resource to open.
|
// Locate resource to open.
|
||||||
ref, err := resources.Lookup(b, arg)
|
resource, err := resources.Lookup(b, arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Confirm that the resource has a URL.
|
// Confirm that the resource has a URL.
|
||||||
url := ref.Resource.GetURL()
|
url := resource.GetURL()
|
||||||
if url == "" {
|
if url == "" {
|
||||||
return fmt.Errorf("resource does not have a URL associated with it (has it been deployed?)")
|
return fmt.Errorf("resource does not have a URL associated with it (has it been deployed?)")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue