2022-05-14 17:55:55 +00:00
|
|
|
package init
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2022-09-05 18:25:54 +00:00
|
|
|
"github.com/databricks/bricks/cmd/prompt"
|
2022-05-14 17:55:55 +00:00
|
|
|
"github.com/databricks/bricks/cmd/root"
|
|
|
|
"github.com/databricks/bricks/project"
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed templates
|
|
|
|
var templates embed.FS
|
|
|
|
|
|
|
|
// initCmd represents the init command
|
|
|
|
var initCmd = &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "Project starter templates",
|
|
|
|
Long: `Generate project templates`,
|
|
|
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if project.IsDatabricksProject() {
|
|
|
|
return fmt.Errorf("this path is already a Databricks project")
|
|
|
|
}
|
|
|
|
profileChoice, err := getConnectionProfile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wd, _ := os.Getwd()
|
2022-09-05 18:25:54 +00:00
|
|
|
q := prompt.Questions{
|
|
|
|
prompt.Text{
|
|
|
|
Key: "name",
|
|
|
|
Label: "Project name",
|
|
|
|
Default: func(res prompt.Results) string {
|
|
|
|
return path.Base(wd)
|
|
|
|
},
|
2022-09-16 09:06:58 +00:00
|
|
|
Callback: func(ans prompt.Answer, config *project.Config, res prompt.Results) {
|
|
|
|
config.Name = ans.Value
|
2022-09-05 18:25:54 +00:00
|
|
|
},
|
|
|
|
},
|
2022-05-14 17:55:55 +00:00
|
|
|
*profileChoice,
|
2022-09-05 18:25:54 +00:00
|
|
|
prompt.Choice{Key: "language", Label: "Project language", Answers: prompt.Answers{
|
|
|
|
{
|
|
|
|
Value: "Python",
|
|
|
|
Details: "Machine learning and data engineering focused projects",
|
|
|
|
Callback: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "Scala",
|
|
|
|
Details: "Data engineering focused projects with strong typing",
|
|
|
|
Callback: nil,
|
|
|
|
},
|
2022-05-14 17:55:55 +00:00
|
|
|
}},
|
2022-09-05 18:25:54 +00:00
|
|
|
prompt.Choice{Key: "isolation", Label: "Deployment isolation", Answers: prompt.Answers{
|
|
|
|
{
|
|
|
|
Value: "None",
|
|
|
|
Details: "Use shared Databricks workspace resources for all project team members",
|
|
|
|
Callback: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "Soft",
|
|
|
|
Details: "Prepend prefixes to each team member's deployment",
|
|
|
|
Callback: func(
|
2022-09-16 09:06:58 +00:00
|
|
|
ans prompt.Answer, config *project.Config, res prompt.Results) {
|
|
|
|
config.Isolation = project.Soft
|
2022-09-05 18:25:54 +00:00
|
|
|
},
|
|
|
|
},
|
2022-05-14 17:55:55 +00:00
|
|
|
}},
|
2022-05-20 18:43:29 +00:00
|
|
|
// DBR selection
|
2022-05-14 17:55:55 +00:00
|
|
|
// Choice{"cloud", "Cloud", Answers{
|
|
|
|
// {"AWS", "Amazon Web Services", nil},
|
|
|
|
// {"Azure", "Microsoft Azure Cloud", nil},
|
|
|
|
// {"GCP", "Google Cloud Platform", nil},
|
|
|
|
// }},
|
|
|
|
// Choice{"ci", "Continuous Integration", Answers{
|
|
|
|
// {"None", "Do not create continuous integration configuration", nil},
|
|
|
|
// {"GitHub Actions", "Create .github/workflows/push.yml configuration", nil},
|
|
|
|
// {"Azure DevOps", "Create basic build and test pipelines", nil},
|
|
|
|
// }},
|
|
|
|
// Choice{"ide", "Integrated Development Environment", Answers{
|
|
|
|
// {"None", "Do not create templates for IDE", nil},
|
|
|
|
// {"VSCode", "Create .devcontainer and other useful things", nil},
|
|
|
|
// {"PyCharm", "Create project conf and other things", nil},
|
|
|
|
// }},
|
|
|
|
}
|
2022-09-05 18:25:54 +00:00
|
|
|
res := prompt.Results{}
|
2022-05-14 17:55:55 +00:00
|
|
|
err = q.Ask(res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
var config project.Config
|
2022-05-14 17:55:55 +00:00
|
|
|
for _, ans := range res {
|
|
|
|
if ans.Callback == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
ans.Callback(ans, &config, res)
|
2022-05-14 17:55:55 +00:00
|
|
|
}
|
2022-09-16 09:06:58 +00:00
|
|
|
raw, err := yaml.Marshal(config)
|
2022-05-14 17:55:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newConfig, err := os.Create(fmt.Sprintf("%s/%s", wd, project.ConfigFile))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = newConfig.Write(raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d, err := templates.ReadDir(".")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, v := range d {
|
|
|
|
cmd.Printf("template found: %v", v.Name())
|
|
|
|
}
|
|
|
|
cmd.Print("Config initialized!")
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
root.RootCmd.AddCommand(initCmd)
|
|
|
|
}
|