diff --git a/commands/init.go b/commands/init.go index d455731dfedafb9b993bbcf38893f79eff2bddf8..ea3c305dd5da5084b4dc45665532099d4b35dfb2 100644 --- a/commands/init.go +++ b/commands/init.go @@ -34,12 +34,18 @@ func InitProject(name string, dbType string) { // Create project directories createProjectDirs(slug) + // Create CHANGELOG.md file from template + createChangelogFile(slug) + // Create datasmith.yaml file from template createConfigFile(slug, name, dbType) // Create an empty database.dbml file createDBMLFile(slug) + // Create an empty sql/database.sql file + createDatabaseSQLFile(slug) + // Create LICENSE.md file from template createLicenseFile(slug, name) @@ -49,7 +55,11 @@ func InitProject(name string, dbType string) { // Create .dockerignore file from template createDockerignoreFile(slug) + // Create Containerfile from template based on dbType + createContainerfile(slug, dbType) + // Create sqlfluff file from template + createSqlfluffFile(slug) // Create k8s/persistentvolumeclaim.template.yaml file from template @@ -66,14 +76,10 @@ func InitProject(name string, dbType string) { // TODO: copy base files from https://gitlab.ard.de/br/buzzboard/database/-/tree/develop?ref_type=heads /* - - .sqlfluff (by database type) - - Containerfile (by database type) - README.md - CONTRIBUTING.md - - CHANGELOG.md - import-sql.sh (by database type) - .gilab-ci.yml (by database type) (also import tests from blessing of the day) - - sql/database.sql (by database type) */ fmt.Printf("Initialized new project structure in '%s'\n", slug) @@ -113,6 +119,20 @@ func createProjectDirs(projectName string) { } } +// createChangelogFile creates the CHANGELOG.md file from template +func createChangelogFile(projectDir string) { + data := map[string]interface{}{ + "Date": time.Now().Format("2006-01-02"), + } + + err := templates.CreateFileFromTemplate(projectDir, "", "CHANGELOG.md", "CHANGELOG.md", data) + if err != nil { + fmt.Printf("Error creating CHANGELOG.md file: %v\n", err) + } else { + fmt.Printf("Created file: %s/CHANGELOG.md\n", projectDir) + } +} + // createConfigFile creates the datasmith.yaml file from template func createConfigFile(projectDir, projectName, dbType string) { data := map[string]interface{}{ @@ -142,6 +162,19 @@ func createDBMLFile(projectDir string) { fmt.Printf("Created file: %s/database.dbml\n", projectDir) } +// createDatabaseSQLFile creates an empty sql/database.sql file in the project directory +func createDatabaseSQLFile(projectDir string) { + sqlFilePath := filepath.Join(projectDir, "sql", "database.sql") + file, err := os.Create(sqlFilePath) + if err != nil { + fmt.Printf("Error creating sql/database.sql file: %v\n", err) + return + } + defer file.Close() + + fmt.Printf("Created file: %s/sql/database.sql\n", projectDir) +} + // createLicenseFile creates the LICENSE.md file from template func createLicenseFile(projectDir, projectName string) { data := map[string]interface{}{ @@ -177,6 +210,31 @@ func createDockerignoreFile(projectDir string) { } } +// createContainerfile creates the Containerfile from the appropriate template +func createContainerfile(projectDir, dbType string) { + templateFile := "Containerfile.mysql" + if dbType == "postgres" { + templateFile = "Containerfile.postgres" + } + + err := templates.CreateFileFromTemplate(projectDir, "", "Containerfile", templateFile, nil) + if err != nil { + fmt.Printf("Error creating Containerfile: %v\n", err) + } else { + fmt.Printf("Created file: %s/Containerfile\n", projectDir) + } +} + +// createSqlfluffFile creates the sqlfluff configuration file from template +func createSqlfluffFile(projectDir string) { + err := templates.CreateFileFromTemplate(projectDir, "", "sqlfluff", "sqlfluff", nil) + if err != nil { + fmt.Printf("Error creating sqlfluff file: %v\n", err) + } else { + fmt.Printf("Created file: %s/sqlfluff\n", projectDir) + } +} + // createK8sPersistentVolumeClaimFile creates the k8s/persistentVolume.template.yaml file from template func createK8sPersistentVolumeFile(projectDir string) { diff --git a/templates/CHANGELOG.md.tmpl b/templates/CHANGELOG.md.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..61484c9aa316aaefb142d8f8ccc191c59d928889 --- /dev/null +++ b/templates/CHANGELOG.md.tmpl @@ -0,0 +1,12 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [0.0.1] - {{ .Date }} +### Added +- Initial release \ No newline at end of file diff --git a/templates/Containerfile.mysql.tmpl b/templates/Containerfile.mysql.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..180efe1dc7f6d4ac05692abfc89020eb344e7ed9 --- /dev/null +++ b/templates/Containerfile.mysql.tmpl @@ -0,0 +1,23 @@ +# Use the official MariaDB image based on Alpine +FROM mariadb:latest + +# Set build-time variables +ARG DB_USER +ARG DB_PASSWORD +ARG DB_DATABASE +ARG MARIADB_ROOT_PASSWORD + +# Set environment variables +ENV MYSQL_USER=$DB_USER +ENV MYSQL_PASSWORD=$DB_PASSWORD +ENV MYSQL_DATABASE=$DB_DATABASE +ENV MYSQL_ROOT_PASSWORD=$MARIADB_ROOT_PASSWORD + +# Copy your SQL scripts from the root directory +COPY ./sql/ /tmp/ + +# Add a script to create the database in the correct order +COPY ./import-sql.sh /docker-entrypoint-initdb.d/ + +# Grant execution permissions to the script +RUN chmod +x /docker-entrypoint-initdb.d/import-sql.sh \ No newline at end of file diff --git a/templates/Containerfile.postgres.tmpl b/templates/Containerfile.postgres.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..0d3c5fb5d5911e959bfee9dc8247ac94d0417e66 --- /dev/null +++ b/templates/Containerfile.postgres.tmpl @@ -0,0 +1,22 @@ +# Use the official PostgreSQL image +FROM postgres:latest + +# Set build-time variables +ARG DB_USER +ARG DB_PASSWORD +ARG DB_DATABASE +ARG POSTGRES_PASSWORD + +# Set environment variables +ENV POSTGRES_USER=$DB_USER +ENV POSTGRES_PASSWORD=$DB_PASSWORD +ENV POSTGRES_DB=$DB_DATABASE + +# Copy your SQL scripts from the root directory +COPY ./sql/ /docker-entrypoint-initdb.d/ + +# Add a script to create the database in the correct order +COPY ./import-sql.sh /docker-entrypoint-initdb.d/ + +# Grant execution permissions to the script +RUN chmod +x /docker-entrypoint-initdb.d/import-sql.sh \ No newline at end of file diff --git a/templates/datasmith.yaml.tmpl b/templates/datasmith.yaml.tmpl index 36da776045a7e551f39b26b0cc63dff2fadc31bb..904961c16163c782569cab10c113a74a4556cc63 100644 --- a/templates/datasmith.yaml.tmpl +++ b/templates/datasmith.yaml.tmpl @@ -1,6 +1,6 @@ # datasmith.yaml name: {{ .Name }} -version: 0.0.0 +version: 0.0.1 created_at: {{ .CreatedAt }} database_type: {{ .DbType }} \ No newline at end of file diff --git a/templates/k8s/deployment.mysql.template.yaml.tmpl b/templates/k8s/deployment.mysql.template.yaml.tmpl index b0cf605f9715e8264e890e4af8c8525fb8b524f5..2cdef77c5527ac2f257ba83f03d0aa6f5202f056 100644 --- a/templates/k8s/deployment.mysql.template.yaml.tmpl +++ b/templates/k8s/deployment.mysql.template.yaml.tmpl @@ -19,7 +19,7 @@ spec: app.kubernetes.io/created-by: gitlab template: metadata: - name: ${CI_PROJECT_NAME}_ + name: ${CI_PROJECT_NAME} namespace: ${K8S_NAMESPACE} labels: app.kubernetes.io/name: ${CI_PROJECT_NAME} diff --git a/templates/sqlfluff.tmpl b/templates/sqlfluff.tmpl index 259f86de59602b5029d12e442c311949787e223d..5a16aed806822a47e5509a1cbb77afcf8bf7bc7c 100644 --- a/templates/sqlfluff.tmpl +++ b/templates/sqlfluff.tmpl @@ -1,2 +1 @@ [sqlfluff] -#exclude_rules= LT05