From 688a1f12b5af7e0feedd691c7cc7b61fb972110d Mon Sep 17 00:00:00 2001 From: "Dominik.Sigmund" <dominik.sigmund@br.de> Date: Mon, 8 Jul 2024 16:02:04 +0200 Subject: [PATCH] Started creating init --- commands/init.go | 31 +++++++++++++++++++++++++++++++ templates/datasmith.yaml.tmpl | 6 ++++++ 2 files changed, 37 insertions(+) create mode 100644 templates/datasmith.yaml.tmpl diff --git a/commands/init.go b/commands/init.go index eb5319d..9a15786 100644 --- a/commands/init.go +++ b/commands/init.go @@ -4,14 +4,27 @@ import ( "fmt" "os/exec" "os" + "time" "path/filepath" "datasmith/utils" + "datasmith/templates" ) func InitProject(name string, dbType string) { utils.PromptIfEmpty(&name, "Enter the name of the project: ") slug := utils.Slugify(name) + // Check if datasmith.yaml already exists in the directory + configFilePath := filepath.Join(slug, "datasmith.yaml") + if _, err := os.Stat(configFilePath); err == nil { + fmt.Printf("Error: A datasmith.yaml file already exists in the directory '%s'. Initialization aborted.\n", slug) + return + } else if !os.IsNotExist(err) { + fmt.Printf("Error checking datasmith.yaml file: %v\n", err) + return + } + + if dbType == "" { dbType = promptForDbType() } @@ -21,6 +34,8 @@ func InitProject(name string, dbType string) { // Create project directories createProjectDirs(slug) + // Create datasmith.yaml file from template + createConfigFile(slug, name, dbType) // TODO: copy base files @@ -61,6 +76,22 @@ func createProjectDirs(projectName string) { } } +// createConfigFile creates the datasmith.yaml file from template +func createConfigFile(projectDir, projectName, dbType string) { + data := map[string]interface{}{ + "Name": projectName, + "CreatedAt": time.Now().Format(time.RFC3339), + "DbType": dbType, + } + + err := templates.CreateFileFromTemplate(projectDir, "", "datasmith.yaml", "datasmith.yaml", data) + if err != nil { + fmt.Printf("Error creating datasmith.yaml file: %v\n", err) + } else { + fmt.Printf("Created file: %s/datasmith.yaml\n", projectDir) + } +} + // Help returns the help information for the init command func InitHelp() string { return "init [name] [--type mysql|postgres] : Create a new project with the specified name and database type." diff --git a/templates/datasmith.yaml.tmpl b/templates/datasmith.yaml.tmpl new file mode 100644 index 0000000..36da776 --- /dev/null +++ b/templates/datasmith.yaml.tmpl @@ -0,0 +1,6 @@ +# datasmith.yaml + +name: {{ .Name }} +version: 0.0.0 +created_at: {{ .CreatedAt }} +database_type: {{ .DbType }} \ No newline at end of file -- GitLab