2025-01-03 10:58:35 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2025-01-03 12:59:50 +00:00
|
|
|
"github.com/databricks/cli/libs/git"
|
2025-01-03 10:58:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Template struct {
|
|
|
|
Reader Reader
|
|
|
|
Writer Writer
|
|
|
|
|
2025-01-03 12:59:50 +00:00
|
|
|
name TemplateName
|
|
|
|
description string
|
|
|
|
aliases []string
|
|
|
|
hidden bool
|
2025-01-03 10:58:35 +00:00
|
|
|
}
|
|
|
|
|
2025-01-03 11:58:22 +00:00
|
|
|
type TemplateName string
|
2025-01-03 10:58:35 +00:00
|
|
|
|
|
|
|
const (
|
2025-01-03 11:58:22 +00:00
|
|
|
DefaultPython TemplateName = "default-python"
|
|
|
|
DefaultSql TemplateName = "default-sql"
|
|
|
|
DbtSql TemplateName = "dbt-sql"
|
|
|
|
MlopsStacks TemplateName = "mlops-stacks"
|
|
|
|
DefaultPydabs TemplateName = "default-pydabs"
|
|
|
|
Custom TemplateName = "custom"
|
2025-01-03 10:58:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var allTemplates = []Template{
|
|
|
|
{
|
2025-01-03 12:59:50 +00:00
|
|
|
name: DefaultPython,
|
|
|
|
description: "The default Python template for Notebooks / Delta Live Tables / Workflows",
|
2025-01-03 10:58:35 +00:00
|
|
|
Reader: &builtinReader{name: "default-python"},
|
|
|
|
Writer: &writerWithTelemetry{},
|
|
|
|
},
|
|
|
|
{
|
2025-01-03 12:59:50 +00:00
|
|
|
name: DefaultSql,
|
|
|
|
description: "The default SQL template for .sql files that run with Databricks SQL",
|
2025-01-03 10:58:35 +00:00
|
|
|
Reader: &builtinReader{name: "default-sql"},
|
|
|
|
Writer: &writerWithTelemetry{},
|
|
|
|
},
|
|
|
|
{
|
2025-01-03 12:59:50 +00:00
|
|
|
name: DbtSql,
|
|
|
|
description: "The dbt SQL template (databricks.com/blog/delivering-cost-effective-data-real-time-dbt-and-databricks)",
|
2025-01-03 10:58:35 +00:00
|
|
|
Reader: &builtinReader{name: "dbt-sql"},
|
|
|
|
Writer: &writerWithTelemetry{},
|
|
|
|
},
|
|
|
|
{
|
2025-01-03 12:59:50 +00:00
|
|
|
name: MlopsStacks,
|
|
|
|
description: "The Databricks MLOps Stacks template (github.com/databricks/mlops-stacks)",
|
|
|
|
aliases: []string{"mlops-stack"},
|
|
|
|
Reader: &gitReader{gitUrl: "https://github.com/databricks/mlops-stacks", cloneFunc: git.Clone},
|
2025-01-03 10:58:35 +00:00
|
|
|
Writer: &writerWithTelemetry{},
|
|
|
|
},
|
|
|
|
{
|
2025-01-03 12:59:50 +00:00
|
|
|
name: DefaultPydabs,
|
|
|
|
hidden: true,
|
|
|
|
description: "The default PyDABs template",
|
|
|
|
Reader: &gitReader{gitUrl: "https://databricks.github.io/workflows-authoring-toolkit/pydabs-template.git", cloneFunc: git.Clone},
|
2025-01-03 10:58:35 +00:00
|
|
|
Writer: &writerWithTelemetry{},
|
|
|
|
},
|
|
|
|
{
|
2025-01-03 12:59:50 +00:00
|
|
|
name: Custom,
|
|
|
|
description: "Bring your own template",
|
2025-01-03 10:58:35 +00:00
|
|
|
Reader: &failReader{},
|
|
|
|
Writer: &defaultWriter{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func HelpDescriptions() string {
|
|
|
|
var lines []string
|
|
|
|
for _, template := range allTemplates {
|
2025-01-03 12:59:50 +00:00
|
|
|
if template.name != Custom && !template.hidden {
|
|
|
|
lines = append(lines, fmt.Sprintf("- %s: %s", template.name, template.description))
|
2025-01-03 10:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func options() []cmdio.Tuple {
|
|
|
|
names := make([]cmdio.Tuple, 0, len(allTemplates))
|
|
|
|
for _, template := range allTemplates {
|
2025-01-03 12:59:50 +00:00
|
|
|
if template.hidden {
|
2025-01-03 10:58:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
tuple := cmdio.Tuple{
|
2025-01-03 12:59:50 +00:00
|
|
|
Name: string(template.name),
|
|
|
|
Id: template.description,
|
2025-01-03 10:58:35 +00:00
|
|
|
}
|
|
|
|
names = append(names, tuple)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2025-01-03 11:58:22 +00:00
|
|
|
func SelectTemplate(ctx context.Context) (TemplateName, error) {
|
2025-01-03 10:58:35 +00:00
|
|
|
if !cmdio.IsPromptSupported(ctx) {
|
|
|
|
return "", fmt.Errorf("please specify a template")
|
|
|
|
}
|
|
|
|
description, err := cmdio.SelectOrdered(ctx, options(), "Template to use")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, template := range allTemplates {
|
2025-01-03 12:59:50 +00:00
|
|
|
if template.description == description {
|
|
|
|
return template.name, nil
|
2025-01-03 10:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("this should never happen - template not found")
|
|
|
|
}
|
|
|
|
|
2025-01-03 11:58:22 +00:00
|
|
|
func Get(name TemplateName) *Template {
|
2025-01-03 10:58:35 +00:00
|
|
|
for _, template := range allTemplates {
|
2025-01-03 12:59:50 +00:00
|
|
|
if template.name == name {
|
2025-01-03 10:58:35 +00:00
|
|
|
return &template
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|