mirror of https://github.com/databricks/cli.git
[DRAFT] Add bundle init command to initialize templates
This commit is contained in:
parent
8ffff241fe
commit
37255215be
|
@ -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)
|
||||||
|
}
|
|
@ -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()
|
||||||
|
}
|
Loading…
Reference in New Issue