diff --git a/commands/init.go b/commands/init.go index eb5319dbff7168453f9c38d695afbd5c7aea3ad4..9a15786e051bdaa74041a356dfb41cd106bd8215 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 0000000000000000000000000000000000000000..36da776045a7e551f39b26b0cc63dff2fadc31bb --- /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