2023-08-07 13:14:25 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
2023-08-25 09:03:42 +00:00
|
|
|
"errors"
|
2023-08-07 13:14:25 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
2023-09-05 13:57:01 +00:00
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2023-08-07 13:14:25 +00:00
|
|
|
"github.com/databricks/cli/libs/git"
|
|
|
|
"github.com/databricks/cli/libs/template"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var gitUrlPrefixes = []string{
|
|
|
|
"https://",
|
|
|
|
"git@",
|
|
|
|
}
|
|
|
|
|
2023-10-16 08:36:01 +00:00
|
|
|
var aliasedTemplates = map[string]string{
|
2023-10-18 08:53:01 +00:00
|
|
|
"mlops-stack": "https://github.com/databricks/mlops-stacks",
|
|
|
|
"mlops-stacks": "https://github.com/databricks/mlops-stacks",
|
2023-10-16 08:36:01 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
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 newInitCommand() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2023-08-25 09:03:42 +00:00
|
|
|
Use: "init [TEMPLATE_PATH]",
|
2023-10-19 07:08:36 +00:00
|
|
|
Short: "Initialize using a bundle template",
|
2023-08-25 09:03:42 +00:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
2023-10-19 07:08:36 +00:00
|
|
|
Long: `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
|
|
|
|
- a local file system path with a template directory
|
|
|
|
- a Git repository URL, e.g. https://github.com/my/repository
|
|
|
|
|
2023-10-23 12:31:31 +00:00
|
|
|
See https://docs.databricks.com/en/dev-tools/bundles/templates.html for more information on templates.`,
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var configFile string
|
2023-08-17 20:32:30 +00:00
|
|
|
var outputDir string
|
2023-08-18 09:29:48 +00:00
|
|
|
var templateDir string
|
2023-08-07 13:14:25 +00:00
|
|
|
cmd.Flags().StringVar(&configFile, "config-file", "", "File containing input parameters for template initialization.")
|
2023-10-19 07:08:36 +00:00
|
|
|
cmd.Flags().StringVar(&templateDir, "template-dir", "", "Directory path within a Git repository containing the template.")
|
2023-08-17 20:32:30 +00:00
|
|
|
cmd.Flags().StringVar(&outputDir, "output-dir", "", "Directory to write the initialized template to.")
|
2023-08-07 13:14:25 +00:00
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
cmd.PreRunE = root.MustWorkspaceClient
|
2023-08-07 13:14:25 +00:00
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := cmd.Context()
|
2023-10-19 07:08:36 +00:00
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
var templatePath string
|
|
|
|
if len(args) > 0 {
|
|
|
|
templatePath = args[0]
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
if !cmdio.IsOutTTY(ctx) || !cmdio.IsInTTY(ctx) {
|
|
|
|
return errors.New("please specify a template")
|
|
|
|
}
|
|
|
|
templatePath, err = cmdio.Ask(ctx, "Template to use", "default-python")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-08-07 13:14:25 +00:00
|
|
|
|
2023-10-16 08:36:01 +00:00
|
|
|
// Expand templatePath if it's an alias for a known template
|
|
|
|
if _, ok := aliasedTemplates[templatePath]; ok {
|
|
|
|
templatePath = aliasedTemplates[templatePath]
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:14:25 +00:00
|
|
|
if !isRepoUrl(templatePath) {
|
2023-10-19 07:08:36 +00:00
|
|
|
if templateDir != "" {
|
|
|
|
return errors.New("--template-dir can only be used with a Git repository URL")
|
|
|
|
}
|
2023-08-07 13:14:25 +00:00
|
|
|
// skip downloading the repo because input arg is not a URL. We assume
|
|
|
|
// it's a path on the local file system in that case
|
2023-08-17 20:32:30 +00:00
|
|
|
return template.Materialize(ctx, configFile, templatePath, outputDir)
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 10:22:05 +00:00
|
|
|
// Create a temporary directory with the name of the repository. The '*'
|
|
|
|
// character is replaced by a random string in the generated temporary directory.
|
|
|
|
repoDir, err := os.MkdirTemp("", repoName(templatePath)+"-*")
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: Add automated test that the downloaded git repo is cleaned up.
|
2023-09-11 10:22:05 +00:00
|
|
|
// Clone the repository in the temporary directory
|
|
|
|
err = git.Clone(ctx, templatePath, "", repoDir)
|
2023-08-07 13:14:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-11 10:22:05 +00:00
|
|
|
// Clean up downloaded repository once the template is materialized.
|
|
|
|
defer os.RemoveAll(repoDir)
|
2023-08-18 09:29:48 +00:00
|
|
|
return template.Materialize(ctx, configFile, filepath.Join(repoDir, templateDir), outputDir)
|
2023-08-07 13:14:25 +00:00
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|