From 334958f1660ac01dd64928f6683db7e34090b080 Mon Sep 17 00:00:00 2001 From: Andrii Klymiuk Date: Tue, 4 Oct 2022 16:00:45 +0300 Subject: [PATCH] Extract templates to separate file --- cmd/init.go | 37 +++---------------------------------- templates/postgres.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 templates/postgres.go diff --git a/cmd/init.go b/cmd/init.go index 78b13aa..3ae5b03 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -6,6 +6,7 @@ import ( "strings" "text/template" + "andriiklymiuk/corgi/templates" "andriiklymiuk/corgi/utils" "github.com/spf13/cobra" @@ -52,8 +53,8 @@ Please provided them in corgi-compose.yml file`) } filesToCreate := []FilenameForService{ - {"docker-compose.yml", dockerComposeTemplate}, - {"Makefile", makefileTemplate}, + {"docker-compose.yml", templates.DockerComposePostgres}, + {"Makefile", templates.MakefilePostgres}, } for _, service := range databaseServices { @@ -151,38 +152,6 @@ func createFileFromTemplate( return nil } -var dockerComposeTemplate = `version: "3.8" - -services: - postgres: - image: postgres:11.5-alpine - container_name: postgres-{{.ServiceName}} - logging: - driver: none - environment: - - POSTGRES_USER={{.User}} - - POSTGRES_PASSWORD={{.Password}} - - POSTGRES_DB={{.DatabaseName}} - ports: - - "{{.Port}}:5432" -` - -var makefileTemplate = `up: - docker compose up -d -down: - docker compose down -stop: - docker stop postgres-{{.ServiceName}} -id: - docker ps -aqf "name=postgres-{{.ServiceName}}" | awk '{print $1}' -seed: - cat dump.sql | docker exec -i $(c) psql -U {{.User}} -d {{.DatabaseName}} -help: - make -qpRr | egrep -e '^[a-z].*:$$' | sed -e 's~:~~g' | sort - -.PHONY: up down stop id seed help -` - func init() { rootCmd.AddCommand(initCmd) } diff --git a/templates/postgres.go b/templates/postgres.go new file mode 100644 index 0000000..cf99768 --- /dev/null +++ b/templates/postgres.go @@ -0,0 +1,33 @@ +package templates + +var DockerComposePostgres = `version: "3.8" + +services: + postgres: + image: postgres:11.5-alpine + container_name: postgres-{{.ServiceName}} + logging: + driver: none + environment: + - POSTGRES_USER={{.User}} + - POSTGRES_PASSWORD={{.Password}} + - POSTGRES_DB={{.DatabaseName}} + ports: + - "{{.Port}}:5432" +` + +var MakefilePostgres = `up: + docker compose up -d +down: + docker compose down +stop: + docker stop postgres-{{.ServiceName}} +id: + docker ps -aqf "name=postgres-{{.ServiceName}}" | awk '{print $1}' +seed: + cat dump.sql | docker exec -i $(c) psql -U {{.User}} -d {{.DatabaseName}} +help: + make -qpRr | egrep -e '^[a-z].*:$$' | sed -e 's~:~~g' | sort + +.PHONY: up down stop id seed help +`