diff --git a/cmd/bundle/init.go b/cmd/bundle/init.go new file mode 100644 index 00000000..ad9f3419 --- /dev/null +++ b/cmd/bundle/init.go @@ -0,0 +1,60 @@ +package bundle + +import ( + "encoding/json" + "os" + "path/filepath" + + "github.com/databricks/cli/libs/git" + "github.com/databricks/cli/libs/template" + "github.com/spf13/cobra" +) + +var initCmd = &cobra.Command{ + Use: "init TEMPLATE_PATH", + Short: "Initialize Template", + Long: `Initialize template`, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + templateURL := args[0] + tmpDir := os.TempDir() + templateDir := filepath.Join(tmpDir, templateURL) + ctx := cmd.Context() + + err := os.MkdirAll(templateDir, 0755) + if err != nil { + return err + } + + // TODO: should we delete this directory once we are done with it? + // It's a destructive action that can be risky + err = git.Clone(ctx, templateURL, "", templateDir) + if err != nil { + return err + } + + // TODO: substitute to a read config method that respects the schema + // and prompts for input variables + b, err := os.ReadFile(configFile) + if err != nil { + return err + } + config := make(map[string]any) + err = json.Unmarshal(b, &config) + if err != nil { + return err + } + return template.Materialize(ctx, config, templateDir, projectDir) + }, +} + +var configFile string +var projectDir string + +func init() { + initCmd.Flags().StringVar(&configFile, "config-file", "", "Input parameters for template initialization.") + initCmd.Flags().StringVar(&projectDir, "project-dir", "", "The project will be initialized in this directory.") + initCmd.MarkFlagRequired("config-file") + initCmd.MarkFlagRequired("output-dir") + AddCommand(initCmd) +} diff --git a/libs/template/materialize.go b/libs/template/materialize.go new file mode 100644 index 00000000..ca67e89f --- /dev/null +++ b/libs/template/materialize.go @@ -0,0 +1,24 @@ +package template + +import ( + "context" + "path/filepath" +) + +const libraryDirName = "library" +const templateDirName = "template" + +func Materialize(ctx context.Context, config map[string]any, templateRoot, instanceRoot string) error { + templatePath := filepath.Join(templateRoot, templateDirName) + libraryPath := filepath.Join(templateRoot, libraryDirName) + + r, err := newRenderer(ctx, config, templatePath, libraryPath, instanceRoot) + if err != nil { + return err + } + err = r.walk() + if err != nil { + return err + } + return r.persistToDisk() +}