Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 56 additions & 10 deletions cmd/bundle/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package bundle

import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"

"github.com/databricks/cli/cmd/root"
Expand All @@ -18,9 +20,52 @@ var gitUrlPrefixes = []string{
"git@",
}

var aliasedTemplates = map[string]string{
"mlops-stack": "https://github.com/databricks/mlops-stacks",
"mlops-stacks": "https://github.com/databricks/mlops-stacks",
type nativeTemplate struct {
gitUrl string
description string
aliases []string
}

var nativeTemplates = map[string]nativeTemplate{
"default-python": {
description: "The default Python template",
},
"mlops-stacks": {
gitUrl: "https://github.com/databricks/mlops-stacks",
description: "The Databricks MLOps Stacks template. More information can be found at: https://github.com/databricks/mlops-stacks",
Comment thread
shreyas-goenka marked this conversation as resolved.
Outdated
aliases: []string{"mlops-stack"},
},
}

func nativeTemplateDescriptions() string {
var lines []string
for name, template := range nativeTemplates {
lines = append(lines, fmt.Sprintf("- %s: %s", name, template.description))
}
return strings.Join(lines, "\n")
}

func nativeTemplateOptions() []string {
keys := make([]string, 0, len(nativeTemplates))
for k := range nativeTemplates {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}

func getURLForNativeTemplate(name string) string {
Comment thread
shreyas-goenka marked this conversation as resolved.
Outdated
for templateName, template := range nativeTemplates {
if templateName == name {
return template.gitUrl
}
for _, alias := range template.aliases {
Comment thread
shreyas-goenka marked this conversation as resolved.
Outdated
if alias == name {
return template.gitUrl
}
}
}
return ""
}

func isRepoUrl(url string) bool {
Expand All @@ -47,14 +92,14 @@ func newInitCommand() *cobra.Command {
Use: "init [TEMPLATE_PATH]",
Short: "Initialize using a bundle template",
Args: cobra.MaximumNArgs(1),
Long: `Initialize using a bundle template.
Long: fmt.Sprintf(`Initialize using a bundle template.

TEMPLATE_PATH optionally specifies which template to use. It can be one of the following:
- 'default-python' for the default Python template
%s
- a local file system path with a template directory
- a Git repository URL, e.g. https://github.com/my/repository

See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more information on templates.`,
See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more information on templates.`, nativeTemplateDescriptions()),
}

var configFile string
Expand Down Expand Up @@ -89,15 +134,16 @@ See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more inf
if !cmdio.IsOutTTY(ctx) || !cmdio.IsInTTY(ctx) {
return errors.New("please specify a template")
}
templatePath, err = cmdio.Ask(ctx, "Template to use", "default-python")
templatePath, err = cmdio.AskSelect(ctx, "Template to use", nativeTemplateOptions())
if err != nil {
return err
}
}

// Expand templatePath if it's an alias for a known template
if _, ok := aliasedTemplates[templatePath]; ok {
templatePath = aliasedTemplates[templatePath]
// Expand templatePath to a git URL if it's an alias for a known native template
// and we know it's git URL.
if gitUrl := getURLForNativeTemplate(templatePath); gitUrl != "" {
templatePath = gitUrl
}

if !isRepoUrl(templatePath) {
Expand Down