Skip to content

Commit

Permalink
Make Lookup return Reference object
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Oct 24, 2024
1 parent 94d3b5e commit 1c55402
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
8 changes: 4 additions & 4 deletions bundle/resources/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ func References(b *bundle.Bundle) (Map, Map) {
// Lookup returns the resource with the specified key.
// 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.
func Lookup(b *bundle.Bundle, key string) (config.ConfigResource, error) {
func Lookup(b *bundle.Bundle, key string) (Reference, error) {
keyOnlyRefs, keyWithTypeRefs := References(b)
refs, ok := keyOnlyRefs[key]
if !ok {
refs, ok = keyWithTypeRefs[key]
if !ok {
return nil, fmt.Errorf("resource with key %q not found", key)
return Reference{}, fmt.Errorf("resource with key %q not found", key)
}
}

switch {
case len(refs) == 1:
return refs[0].Resource, nil
return refs[0], nil
case len(refs) > 1:
return nil, fmt.Errorf("multiple resources with key %q found", key)
return Reference{}, fmt.Errorf("multiple resources with key %q found", key)
default:
panic("unreachable")
}
Expand Down
13 changes: 5 additions & 8 deletions bundle/resources/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ func TestLookup_EmptyBundle(t *testing.T) {
},
}

out, err := Lookup(b, "foo")
_, err := Lookup(b, "foo")
require.Error(t, err)
assert.Nil(t, out)
assert.ErrorContains(t, err, "resource with key \"foo\" not found")
}

Expand All @@ -36,9 +35,8 @@ func TestLookup_NotFound(t *testing.T) {
},
}

out, err := Lookup(b, "qux")
_, err := Lookup(b, "qux")
require.Error(t, err)
assert.Nil(t, out)
assert.ErrorContains(t, err, `resource with key "qux" not found`)
}

Expand All @@ -56,9 +54,8 @@ func TestLookup_MultipleFound(t *testing.T) {
},
}

out, err := Lookup(b, "foo")
_, err := Lookup(b, "foo")
require.Error(t, err)
assert.Nil(t, out)
assert.ErrorContains(t, err, `multiple resources with key "foo" found`)
}

Expand All @@ -81,13 +78,13 @@ func TestLookup_Nominal(t *testing.T) {
out, err := Lookup(b, "foo")
require.NoError(t, err)
if assert.NotNil(t, out) {
assert.Equal(t, "Foo job", out.GetName())
assert.Equal(t, "Foo job", out.Resource.GetName())
}

// Lookup by type and key.
out, err = Lookup(b, "jobs.foo")
require.NoError(t, err)
if assert.NotNil(t, out) {
assert.Equal(t, "Foo job", out.GetName())
assert.Equal(t, "Foo job", out.Resource.GetName())
}
}
6 changes: 3 additions & 3 deletions cmd/bundle/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
// If no arguments are specified, prompt the user to select something to run.
// If no arguments are specified, prompt the user to select the resource to open.
if len(args) == 0 && cmdio.IsPromptSupported(ctx) {
return promptOpenArgument(ctx, b)
}
Expand Down Expand Up @@ -105,13 +105,13 @@ func newOpenCommand() *cobra.Command {
}

// Locate resource to open.
resource, err := resources.Lookup(b, arg)
ref, err := resources.Lookup(b, arg)
if err != nil {
return err
}

// Confirm that the resource has a URL.
url := resource.GetURL()
url := ref.Resource.GetURL()
if url == "" {
return fmt.Errorf("resource does not have a URL associated with it (has it been deployed?)")
}
Expand Down

0 comments on commit 1c55402

Please sign in to comment.