From 4c10d32bb685223e62755a70d15bb5fc0bc4c99d Mon Sep 17 00:00:00 2001
From: "Dominik.Sigmund" <dominik.sigmund@br.de>
Date: Tue, 9 Jul 2024 13:48:15 +0200
Subject: [PATCH] Added more templates for init

---
 commands/init.go                              | 66 +++++++++++++++++--
 templates/CHANGELOG.md.tmpl                   | 12 ++++
 templates/Containerfile.mysql.tmpl            | 23 +++++++
 templates/Containerfile.postgres.tmpl         | 22 +++++++
 templates/datasmith.yaml.tmpl                 |  2 +-
 .../k8s/deployment.mysql.template.yaml.tmpl   |  2 +-
 templates/sqlfluff.tmpl                       |  1 -
 7 files changed, 121 insertions(+), 7 deletions(-)
 create mode 100644 templates/CHANGELOG.md.tmpl
 create mode 100644 templates/Containerfile.mysql.tmpl
 create mode 100644 templates/Containerfile.postgres.tmpl

diff --git a/commands/init.go b/commands/init.go
index d455731..ea3c305 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 0000000..61484c9
--- /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 0000000..180efe1
--- /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 0000000..0d3c5fb
--- /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 36da776..904961c 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 b0cf605..2cdef77 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 259f86d..5a16aed 100644
--- a/templates/sqlfluff.tmpl
+++ b/templates/sqlfluff.tmpl
@@ -1,2 +1 @@
 [sqlfluff]
-#exclude_rules= LT05
-- 
GitLab