Skip to content

Commit

Permalink
feat: env fix (#151)
Browse files Browse the repository at this point in the history
Removing the need to have duplicate .env files - generate a single file
  • Loading branch information
SudoSurya authored Dec 29, 2023
1 parent 9471913 commit 19ba389
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 74 deletions.
30 changes: 14 additions & 16 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ type Templater interface {
type DBDriverTemplater interface {
Service() []byte
Env() []byte
EnvExample() []byte
}

type DockerTemplater interface {
Expand Down Expand Up @@ -273,13 +272,6 @@ func (p *Project) CreateMainFile() error {
// Create correct docker compose for the selected driver
if p.DBDriver != "none" {

err = p.CreateFileWithInjection(root, projectPath, ".env.example", "env-example")
if err != nil {
log.Printf("Error injecting .env.example file: %v", err)
cobra.CheckErr(err)
return err
}

if p.DBDriver != "sqlite" {
p.createDockerMap()
p.Docker = p.DBDriver
Expand Down Expand Up @@ -336,11 +328,20 @@ func (p *Project) CreateMainFile() error {

defer makeFile.Close()

// inject makefile template
makeFileTemplate := template.Must(template.New("makefile").Parse(string(framework.MakeTemplate())))
err = makeFileTemplate.Execute(makeFile, p)
if err != nil {
return err
if p.DBDriver == "sqlite" || p.DBDriver == "none" {
// inject makefile template
makeFileTemplate := template.Must(template.New("makefile").Parse(string(framework.NonDbMakeFileTemplate())))
err = makeFileTemplate.Execute(makeFile, p)
if err != nil {
return err
}
} else {
// inject makefile template for database excluding sqlite
makeFileTemplate := template.Must(template.New("makefile").Parse(string(framework.MakeTemplate())))
err = makeFileTemplate.Execute(makeFile, p)
if err != nil {
return err
}
}

readmeFile, err := os.Create(filepath.Join(projectPath, "README.md"))
Expand Down Expand Up @@ -654,9 +655,6 @@ func (p *Project) CreateFileWithInjection(pathToCreate string, projectPath strin
case "tests":
createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType].templater.TestHandler())))
err = createdTemplate.Execute(createdFile, p)
case "env-example":
createdTemplate := template.Must(template.New(fileName).Parse(string(p.DBDriverMap[p.DBDriver].templater.EnvExample())))
err = createdTemplate.Execute(createdFile, p)
case "env":
if p.DBDriver != "none" {

Expand Down
4 changes: 0 additions & 4 deletions cmd/template/dbdriver/files/env/mongo.example.tmpl

This file was deleted.

8 changes: 4 additions & 4 deletions cmd/template/dbdriver/files/env/mongo.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DB_HOST=
DB_PORT=
DB_USERNAME=
DB_ROOT_PASSWORD=
DB_HOST=localhost
DB_PORT=27017
DB_USERNAME=melkey
DB_ROOT_PASSWORD=password1234
6 changes: 0 additions & 6 deletions cmd/template/dbdriver/files/env/mysql.example.tmpl

This file was deleted.

12 changes: 6 additions & 6 deletions cmd/template/dbdriver/files/env/mysql.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
DB_ROOT_PASSWORD=
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=blueprint
DB_USERNAME=melkey
DB_PASSWORD=password1234
DB_ROOT_PASSWORD=password4321
5 changes: 0 additions & 5 deletions cmd/template/dbdriver/files/env/postgres.example.tmpl

This file was deleted.

10 changes: 5 additions & 5 deletions cmd/template/dbdriver/files/env/postgres.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=blueprint
DB_USERNAME=melkey
DB_PASSWORD=password1234
1 change: 0 additions & 1 deletion cmd/template/dbdriver/files/env/sqlite.example.tmpl

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/template/dbdriver/files/env/sqlite.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DB_URL=
DB_URL=./test.db
5 changes: 0 additions & 5 deletions cmd/template/dbdriver/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ var mongoServiceTemplate []byte
//go:embed files/env/mongo.tmpl
var mongoEnvTemplate []byte

//go:embed files/env/mongo.example.tmpl
var mongoEnvExampleTemplate []byte

func (m MongoTemplate) Service() []byte {
return mongoServiceTemplate
Expand All @@ -23,6 +21,3 @@ func (m MongoTemplate) Env() []byte {
return mongoEnvTemplate
}

func (m MongoTemplate) EnvExample() []byte {
return mongoEnvExampleTemplate
}
6 changes: 0 additions & 6 deletions cmd/template/dbdriver/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ var mysqlServiceTemplate []byte
//go:embed files/env/mysql.tmpl
var mysqlEnvTemplate []byte

//go:embed files/env/mysql.example.tmpl
var mysqlEnvExampleTemplate []byte

func (m MysqlTemplate) Service() []byte {
return mysqlServiceTemplate
}
Expand All @@ -23,6 +20,3 @@ func (m MysqlTemplate) Env() []byte {
return mysqlEnvTemplate
}

func (m MysqlTemplate) EnvExample() []byte {
return mysqlEnvExampleTemplate
}
7 changes: 0 additions & 7 deletions cmd/template/dbdriver/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ var postgresServiceTemplate []byte
//go:embed files/env/postgres.tmpl
var postgresEnvTemplate []byte

//go:embed files/env/postgres.example.tmpl
var postgresEnvExampleTemplate []byte

func (m PostgresTemplate) Service() []byte {
return postgresServiceTemplate
}

func (m PostgresTemplate) Env() []byte {
return postgresEnvTemplate
}

func (m PostgresTemplate) EnvExample() []byte {
return postgresEnvExampleTemplate
}
7 changes: 0 additions & 7 deletions cmd/template/dbdriver/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ var sqliteServiceTemplate []byte
//go:embed files/env/sqlite.tmpl
var sqliteEnvTemplate []byte

//go:embed files/env/sqlite.example.tmpl
var sqliteEnvExampleTemplate []byte

func (m SqliteTemplate) Service() []byte {
return sqliteServiceTemplate
}

func (m SqliteTemplate) Env() []byte {
return sqliteEnvTemplate
}

func (m SqliteTemplate) EnvExample() []byte {
return sqliteEnvExampleTemplate
}
41 changes: 41 additions & 0 deletions cmd/template/framework/files/nondbMakeFile.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Simple Makefile for a Go project

# Build the application
all: build

build:
@echo "Building..."
@go build -o main cmd/api/main.go

# Run the application
run:
@go run cmd/api/main.go

# Test the application
test:
@echo "Testing..."
@go test ./tests -v

# Clean the binary
clean:
@echo "Cleaning..."
@rm -f main

# Live Reload
watch:
@if command -v air > /dev/null; then \
air; \
echo "Watching...";\
else \
read -p "Go's 'air' is not installed on your machine. Do you want to install it? [Y/n] " choice; \
if [ "$$choice" != "n" ] && [ "$$choice" != "N" ]; then \
go install github.com/cosmtrek/air@latest; \
air; \
echo "Watching...";\
else \
echo "You chose not to install air. Exiting..."; \
exit 1; \
fi; \
fi

.PHONY: all build run test clean
7 changes: 7 additions & 0 deletions cmd/template/framework/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
//go:embed files/main/main.go.tmpl
var mainTemplate []byte

//go:embed files/nondbMakeFile.tmpl
var nondbMakeFileTemplate []byte

//go:embed files/air.toml.tmpl
var airTomlTemplate []byte

Expand Down Expand Up @@ -40,3 +43,7 @@ func AirTomlTemplate() []byte {
func ReadmeTemplate() []byte {
return readmeTemplate
}

func NonDbMakeFileTemplate() []byte {
return nondbMakeFileTemplate
}
2 changes: 1 addition & 1 deletion contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- nhlmg93
- MitchellBerend
- itschip
- 20pa5a1210
- SudoSurya
- alexjoedt
- sakelig
- joshjms
Expand Down

0 comments on commit 19ba389

Please sign in to comment.