2022-12-15 14:12:47 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/run/output"
|
2022-12-15 14:12:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type key string
|
|
|
|
|
|
|
|
func (k key) Key() string {
|
|
|
|
return string(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runner defines the interface for a runnable resource (or workload).
|
|
|
|
type Runner interface {
|
|
|
|
// Key returns the fully qualified (unique) identifier for this runnable resource.
|
|
|
|
// This is used for showing the user hints w.r.t. disambiguation.
|
|
|
|
Key() string
|
|
|
|
|
2023-09-11 18:03:12 +00:00
|
|
|
// Name returns the resource's name, if defined.
|
|
|
|
Name() string
|
|
|
|
|
2022-12-15 14:12:47 +00:00
|
|
|
// Run the underlying worklow.
|
2023-04-14 12:40:34 +00:00
|
|
|
Run(ctx context.Context, opts *Options) (output.RunOutput, error)
|
2024-02-09 14:33:14 +00:00
|
|
|
|
|
|
|
// Cancel the underlying workflow.
|
|
|
|
Cancel(ctx context.Context) error
|
2024-04-22 11:50:13 +00:00
|
|
|
|
|
|
|
// Runners support parsing and completion of additional positional arguments.
|
|
|
|
argsHandler
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:19:38 +00:00
|
|
|
// Find locates a runner matching the specified argument.
|
2022-12-15 14:12:47 +00:00
|
|
|
//
|
|
|
|
// Its behavior is as follows:
|
2022-12-22 15:19:38 +00:00
|
|
|
// 1. Try to find a resource with <key> identical to the argument.
|
|
|
|
// 2. Try to find a resource with <type>.<key> identical to the argument.
|
2022-12-15 14:12:47 +00:00
|
|
|
//
|
|
|
|
// If an argument resolves to multiple resources, it returns an error.
|
2022-12-22 15:19:38 +00:00
|
|
|
func Find(b *bundle.Bundle, arg string) (Runner, error) {
|
2022-12-15 14:12:47 +00:00
|
|
|
keyOnly, keyWithType := ResourceKeys(b)
|
|
|
|
if len(keyWithType) == 0 {
|
|
|
|
return nil, fmt.Errorf("bundle defines no resources")
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:19:38 +00:00
|
|
|
runners, ok := keyOnly[arg]
|
|
|
|
if !ok {
|
|
|
|
runners, ok = keyWithType[arg]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no such resource: %s", arg)
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:19:38 +00:00
|
|
|
if len(runners) != 1 {
|
|
|
|
var keys []string
|
|
|
|
for _, runner := range runners {
|
|
|
|
keys = append(keys, runner.Key())
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
2022-12-22 15:19:38 +00:00
|
|
|
return nil, fmt.Errorf("ambiguous: %s (can resolve to all of %s)", arg, strings.Join(keys, ", "))
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:19:38 +00:00
|
|
|
return runners[0], nil
|
2022-12-15 14:12:47 +00:00
|
|
|
}
|