github.com/databricks/cli@v0.203.0/bundle/run/runner.go (about)

     1  package run
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/databricks/cli/bundle"
     9  	"github.com/databricks/cli/bundle/run/output"
    10  )
    11  
    12  type key string
    13  
    14  func (k key) Key() string {
    15  	return string(k)
    16  }
    17  
    18  // Runner defines the interface for a runnable resource (or workload).
    19  type Runner interface {
    20  	// Key returns the fully qualified (unique) identifier for this runnable resource.
    21  	// This is used for showing the user hints w.r.t. disambiguation.
    22  	Key() string
    23  
    24  	// Run the underlying worklow.
    25  	Run(ctx context.Context, opts *Options) (output.RunOutput, error)
    26  }
    27  
    28  // Find locates a runner matching the specified argument.
    29  //
    30  // Its behavior is as follows:
    31  //  1. Try to find a resource with <key> identical to the argument.
    32  //  2. Try to find a resource with <type>.<key> identical to the argument.
    33  //
    34  // If an argument resolves to multiple resources, it returns an error.
    35  func Find(b *bundle.Bundle, arg string) (Runner, error) {
    36  	keyOnly, keyWithType := ResourceKeys(b)
    37  	if len(keyWithType) == 0 {
    38  		return nil, fmt.Errorf("bundle defines no resources")
    39  	}
    40  
    41  	runners, ok := keyOnly[arg]
    42  	if !ok {
    43  		runners, ok = keyWithType[arg]
    44  		if !ok {
    45  			return nil, fmt.Errorf("no such resource: %s", arg)
    46  		}
    47  	}
    48  
    49  	if len(runners) != 1 {
    50  		var keys []string
    51  		for _, runner := range runners {
    52  			keys = append(keys, runner.Key())
    53  		}
    54  		return nil, fmt.Errorf("ambiguous: %s (can resolve to all of %s)", arg, strings.Join(keys, ", "))
    55  	}
    56  
    57  	return runners[0], nil
    58  }