diff --git a/cmd/bundle/init.go b/cmd/bundle/init.go index 28a4346e3..c69e374ed 100644 --- a/cmd/bundle/init.go +++ b/cmd/bundle/init.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "path/filepath" - "slices" "strings" "github.com/databricks/cli/cmd/root" @@ -19,119 +18,6 @@ import ( "github.com/spf13/cobra" ) -var gitUrlPrefixes = []string{ - "https://", - "git@", -} - -type nativeTemplate struct { - name string - gitUrl string - description string - aliases []string - hidden bool -} - -const customTemplate = "custom..." - -var nativeTemplates = []nativeTemplate{ - { - name: "default-python", - description: "The default Python template for Notebooks / Delta Live Tables / Workflows", - }, - { - name: "default-sql", - description: "The default SQL template for .sql files that run with Databricks SQL", - }, - { - name: "dbt-sql", - description: "The dbt SQL template (databricks.com/blog/delivering-cost-effective-data-real-time-dbt-and-databricks)", - }, - { - name: "mlops-stacks", - gitUrl: "https://github.com/databricks/mlops-stacks", - description: "The Databricks MLOps Stacks template (github.com/databricks/mlops-stacks)", - aliases: []string{"mlops-stack"}, - }, - { - name: "default-pydabs", - gitUrl: "https://databricks.github.io/workflows-authoring-toolkit/pydabs-template.git", - hidden: true, - description: "The default PyDABs template", - }, - { - name: customTemplate, - description: "Bring your own template", - }, -} - -// Return template descriptions for command-line help -func nativeTemplateHelpDescriptions() string { - var lines []string - for _, template := range nativeTemplates { - if template.name != customTemplate && !template.hidden { - lines = append(lines, fmt.Sprintf("- %s: %s", template.name, template.description)) - } - } - return strings.Join(lines, "\n") -} - -// Return template options for an interactive prompt -func nativeTemplateOptions() []cmdio.Tuple { - names := make([]cmdio.Tuple, 0, len(nativeTemplates)) - for _, template := range nativeTemplates { - if template.hidden { - continue - } - tuple := cmdio.Tuple{ - Name: template.name, - Id: template.description, - } - names = append(names, tuple) - } - return names -} - -func getNativeTemplateByDescription(description string) string { - for _, template := range nativeTemplates { - if template.description == description { - return template.name - } - } - return "" -} - -func getNativeTemplateByName(name string) *nativeTemplate { - for _, template := range nativeTemplates { - if template.name == name { - return &template - } - if slices.Contains(template.aliases, name) { - return &template - } - } - return nil -} - -func isRepoUrl(url string) bool { - result := false - for _, prefix := range gitUrlPrefixes { - if strings.HasPrefix(url, prefix) { - result = true - break - } - } - return result -} - -// Computes the repo name from the repo URL. Treats the last non empty word -// when splitting at '/' as the repo name. For example: for url git@github.com:databricks/cli.git -// the name would be "cli.git" -func repoName(url string) string { - parts := strings.Split(strings.TrimRight(url, "/"), "/") - return parts[len(parts)-1] -} - func constructOutputFiler(ctx context.Context, outputDir string) (filer.Filer, error) { outputDir, err := filepath.Abs(outputDir) if err != nil { diff --git a/cmd/bundle/init_test.go b/cmd/bundle/init_test.go deleted file mode 100644 index 8b10fe131..000000000 --- a/cmd/bundle/init_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package bundle - -import ( - "testing" - - "github.com/databricks/cli/libs/cmdio" - "github.com/stretchr/testify/assert" -) - -func TestBundleInitIsRepoUrl(t *testing.T) { - assert.True(t, isRepoUrl("git@github.com:databricks/cli.git")) - assert.True(t, isRepoUrl("https://github.com/databricks/cli.git")) - - assert.False(t, isRepoUrl("./local")) - assert.False(t, isRepoUrl("foo")) -} - -func TestBundleInitRepoName(t *testing.T) { - // Test valid URLs - assert.Equal(t, "cli.git", repoName("git@github.com:databricks/cli.git")) - assert.Equal(t, "cli", repoName("https://github.com/databricks/cli/")) - - // test invalid URLs. In these cases the error would be floated when the - // git clone operation fails. - assert.Equal(t, "git@github.com:databricks", repoName("git@github.com:databricks")) - assert.Equal(t, "invalid-url", repoName("invalid-url")) - assert.Equal(t, "www.github.com", repoName("https://www.github.com")) -} - -func TestNativeTemplateOptions(t *testing.T) { - expected := []cmdio.Tuple{ - {Name: "default-python", Id: "The default Python template for Notebooks / Delta Live Tables / Workflows"}, - {Name: "default-sql", Id: "The default SQL template for .sql files that run with Databricks SQL"}, - {Name: "dbt-sql", Id: "The dbt SQL template (databricks.com/blog/delivering-cost-effective-data-real-time-dbt-and-databricks)"}, - {Name: "mlops-stacks", Id: "The Databricks MLOps Stacks template (github.com/databricks/mlops-stacks)"}, - {Name: "custom...", Id: "Bring your own template"}, - } - assert.Equal(t, expected, nativeTemplateOptions()) -} - -func TestGetNativeTemplateByName(t *testing.T) { - assert.Equal(t, "https://github.com/databricks/mlops-stacks", getNativeTemplateByName("mlops-stacks").gitUrl) - assert.Equal(t, "https://github.com/databricks/mlops-stacks", getNativeTemplateByName("mlops-stack").gitUrl) - - assert.Equal(t, "default-python", getNativeTemplateByName("default-python").name) - assert.Equal(t, "The default Python template for Notebooks / Delta Live Tables / Workflows", getNativeTemplateByName("default-python").description) - assert.Equal(t, "", getNativeTemplateByName("default-python").gitUrl) - - assert.Nil(t, getNativeTemplateByName("invalid")) -} diff --git a/libs/template/template_test.go b/libs/template/template_test.go index bdd4391d3..6b6ca0d0e 100644 --- a/libs/template/template_test.go +++ b/libs/template/template_test.go @@ -3,6 +3,7 @@ package template import ( "testing" + "github.com/databricks/cli/libs/cmdio" "github.com/stretchr/testify/assert" ) @@ -13,3 +14,34 @@ func TestTemplateHelpDescriptions(t *testing.T) { - mlops-stacks: The Databricks MLOps Stacks template (github.com/databricks/mlops-stacks)` assert.Equal(t, expected, HelpDescriptions()) } + +func TestTemplateOptions(t *testing.T) { + expected := []cmdio.Tuple{ + {Name: "default-python", Id: "The default Python template for Notebooks / Delta Live Tables / Workflows"}, + {Name: "default-sql", Id: "The default SQL template for .sql files that run with Databricks SQL"}, + {Name: "dbt-sql", Id: "The dbt SQL template (databricks.com/blog/delivering-cost-effective-data-real-time-dbt-and-databricks)"}, + {Name: "mlops-stacks", Id: "The Databricks MLOps Stacks template (github.com/databricks/mlops-stacks)"}, + {Name: "custom", Id: "Bring your own template"}, + } + assert.Equal(t, expected, options()) +} + +func TestBundleInitIsRepoUrl(t *testing.T) { + assert.True(t, IsGitRepoUrl("git@github.com:databricks/cli.git")) + assert.True(t, IsGitRepoUrl("https://github.com/databricks/cli.git")) + + assert.False(t, IsGitRepoUrl("./local")) + assert.False(t, IsGitRepoUrl("foo")) +} + +func TestBundleInitRepoName(t *testing.T) { + // Test valid URLs + assert.Equal(t, "cli.git", repoName("git@github.com:databricks/cli.git")) + assert.Equal(t, "cli", repoName("https://github.com/databricks/cli/")) + + // test invalid URLs. In these cases the error would be floated when the + // git clone operation fails. + assert.Equal(t, "git@github.com:databricks", repoName("git@github.com:databricks")) + assert.Equal(t, "invalid-url", repoName("invalid-url")) + assert.Equal(t, "www.github.com", repoName("https://www.github.com")) +}