2023-06-09 11:56:35 +00:00
|
|
|
package databrickscfg
|
|
|
|
|
|
|
|
import (
|
2023-11-08 14:50:20 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2023-08-14 12:45:08 +00:00
|
|
|
"fmt"
|
2023-11-08 14:50:20 +00:00
|
|
|
"io/fs"
|
2023-08-14 12:45:08 +00:00
|
|
|
"path/filepath"
|
2023-06-09 11:56:35 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-11-08 14:50:20 +00:00
|
|
|
"github.com/databricks/cli/libs/env"
|
2023-06-09 11:56:35 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/config"
|
2023-07-12 10:05:51 +00:00
|
|
|
"github.com/spf13/cobra"
|
2023-06-09 11:56:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Profile holds a subset of the keys in a databrickscfg profile.
|
|
|
|
// It should only be used for prompting and filtering.
|
|
|
|
// Use its name to construct a config.Config.
|
|
|
|
type Profile struct {
|
|
|
|
Name string
|
|
|
|
Host string
|
|
|
|
AccountID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Profile) Cloud() string {
|
|
|
|
cfg := config.Config{Host: p.Host}
|
|
|
|
switch {
|
|
|
|
case cfg.IsAws():
|
|
|
|
return "AWS"
|
|
|
|
case cfg.IsAzure():
|
|
|
|
return "Azure"
|
|
|
|
case cfg.IsGcp():
|
|
|
|
return "GCP"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Profiles []Profile
|
|
|
|
|
|
|
|
func (p Profiles) Names() []string {
|
|
|
|
names := make([]string, len(p))
|
|
|
|
for i, v := range p {
|
|
|
|
names[i] = v.Name
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchCaseInsensitive implements the promptui.Searcher interface.
|
|
|
|
// This allows the user to immediately starting typing to narrow down the list.
|
|
|
|
func (p Profiles) SearchCaseInsensitive(input string, index int) bool {
|
|
|
|
input = strings.ToLower(input)
|
|
|
|
name := strings.ToLower(p[index].Name)
|
|
|
|
host := strings.ToLower(p[index].Host)
|
|
|
|
return strings.Contains(name, input) || strings.Contains(host, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProfileMatchFunction func(Profile) bool
|
|
|
|
|
|
|
|
func MatchWorkspaceProfiles(p Profile) bool {
|
|
|
|
return p.AccountID == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func MatchAccountProfiles(p Profile) bool {
|
|
|
|
return p.Host != "" && p.AccountID != ""
|
|
|
|
}
|
|
|
|
|
2023-07-12 10:05:51 +00:00
|
|
|
func MatchAllProfiles(p Profile) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-14 12:45:08 +00:00
|
|
|
// Get the path to the .databrickscfg file, falling back to the default in the current user's home directory.
|
2023-11-08 14:50:20 +00:00
|
|
|
func GetPath(ctx context.Context) (string, error) {
|
|
|
|
configFile := env.Get(ctx, "DATABRICKS_CONFIG_FILE")
|
2023-08-14 12:45:08 +00:00
|
|
|
if configFile == "" {
|
|
|
|
configFile = "~/.databrickscfg"
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(configFile, "~") {
|
2023-11-29 19:08:27 +00:00
|
|
|
homedir, err := env.UserHomeDir(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-08-14 12:45:08 +00:00
|
|
|
configFile = filepath.Join(homedir, configFile[1:])
|
|
|
|
}
|
|
|
|
return configFile, nil
|
|
|
|
}
|
2023-06-09 11:56:35 +00:00
|
|
|
|
2023-11-08 14:50:20 +00:00
|
|
|
var ErrNoConfiguration = errors.New("no configuration file found")
|
|
|
|
|
|
|
|
func Get(ctx context.Context) (*config.File, error) {
|
|
|
|
path, err := GetPath(ctx)
|
2023-06-09 11:56:35 +00:00
|
|
|
if err != nil {
|
2023-08-14 12:45:08 +00:00
|
|
|
return nil, fmt.Errorf("cannot determine Databricks config file path: %w", err)
|
|
|
|
}
|
2023-11-08 14:50:20 +00:00
|
|
|
configFile, err := config.LoadFile(path)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
// downstreams depend on ErrNoConfiguration. TODO: expose this error through SDK
|
|
|
|
return nil, fmt.Errorf("%w at %s; please create one first", ErrNoConfiguration, path)
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return configFile, nil
|
2023-08-14 12:45:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 14:50:20 +00:00
|
|
|
func LoadProfiles(ctx context.Context, fn ProfileMatchFunction) (file string, profiles Profiles, err error) {
|
|
|
|
f, err := Get(ctx)
|
2023-08-14 12:45:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, fmt.Errorf("cannot load Databricks config file: %w", err)
|
2023-06-09 11:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace homedir with ~ if applicable.
|
|
|
|
// This is to make the output more readable.
|
2023-11-08 14:50:20 +00:00
|
|
|
file = filepath.Clean(f.Path())
|
2023-11-29 19:08:27 +00:00
|
|
|
home, err := env.UserHomeDir(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
homedir := filepath.Clean(home)
|
2023-06-09 11:56:35 +00:00
|
|
|
if strings.HasPrefix(file, homedir) {
|
|
|
|
file = "~" + file[len(homedir):]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over sections and collect matching profiles.
|
|
|
|
for _, v := range f.Sections() {
|
|
|
|
all := v.KeysHash()
|
|
|
|
host, ok := all["host"]
|
|
|
|
if !ok {
|
|
|
|
// invalid profile
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
profile := Profile{
|
|
|
|
Name: v.Name(),
|
|
|
|
Host: host,
|
|
|
|
AccountID: all["account_id"],
|
|
|
|
}
|
|
|
|
if fn(profile) {
|
|
|
|
profiles = append(profiles, profile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-07-12 10:05:51 +00:00
|
|
|
|
|
|
|
func ProfileCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2023-11-08 14:50:20 +00:00
|
|
|
_, profiles, err := LoadProfiles(cmd.Context(), MatchAllProfiles)
|
2023-07-12 10:05:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
return profiles.Names(), cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|