Skip to content

Commit

Permalink
fix template
Browse files Browse the repository at this point in the history
  • Loading branch information
damien-jacinto committed Nov 13, 2023
1 parent af4086c commit 5316648
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ vendor/

# ignore siqtch conf
sqitch.conf
secret.conf

# build
**/bin
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ deps:
@go mod tidy

# build
build:
build: fmt
@go build -o $(OUTPUT_FOLDER) $(CMD_FOLDER)

# Run tests
Expand All @@ -24,3 +24,7 @@ fmt:
# Clean
clean:
@rm -rf $(OUTPUT_FOLDER)

# Run
run: build
@go run ./...
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# FinalcadOne-sqitch

Create configuration file for sqitch containing postgres connection credentials using rds IAM authentification.
Create configuration file and a secret file for sqitch containing postgres connection credentials using rds IAM authentification.
By default configuration file will be named `sqitch.conf` in current directory.
Password to load in environment variable will be located by default in `secret.conf`

### Environment variables

Expand All @@ -12,6 +13,7 @@ By default configuration file will be named `sqitch.conf` in current directory.
| POSTGRES_URI | Postgres URI endpoint | empty |
| POSTGRES_DB | Database name | empty |
| CONFIG_SQITCH_PATH | config filepath + name | sqitch.conf |
| SECRET_SQITCH_PATH | config filepath + name | secret.conf |

## User environment variable

Expand All @@ -33,5 +35,9 @@ IAM database authentication
## Usage in other container

```yaml
# add binary from image
COPY --from XXX /sqitch-config .
# in entrypoint.sh
# load secret in env vars
source ./secret.conf
```
2 changes: 1 addition & 1 deletion cmd/sqitch-config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
if err != nil {
panic(err.Error())
}
err = configsqitch.WriteConfig(configSqitch)
err = configSqitch.WriteConfig()
if err != nil {
panic(err.Error())
}
Expand Down
41 changes: 28 additions & 13 deletions internal/configsqitch/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ type ConfigSqitch struct {
Region string `json:"region"`
Profile string `json:"profile"`
Timeout int `json:"timeout"`
Filepath string `json:"filepath"`
ConfigFilepath string `json:"configfilepath"`
SecretFilepath string `json:"secretfilepath"`
}

const (
DEFAULT_REGION = "us-east-1"
DEFAULT_TIMEOUT = 5000
DEFAULT_PORT = "5432"
DEFAULT_CONFIG_SQITCH_PATH = "sqitch.conf"
DEFAULT_SECRET_SQITCH_PATH = "secret.conf"
)

//go:embed sqitch.tmpl
Expand All @@ -45,17 +47,6 @@ func (c ConfigSqitch) String() string {
c.PostgresUser, c.PostgresURI, c.PostgresPort, c.PostgresDB, c.Region, c.Profile, c.Timeout)
}

func WriteConfig(configSqitch *ConfigSqitch) error {
outputFile, err := utils.CreateFile(configSqitch.Filepath)
if err != nil {
return err
}
defer outputFile.Close()

tmpl := template.Must(template.New("configTemplate").Parse(templateConfig))
return tmpl.Execute(outputFile, configSqitch)
}

func GetConfig() (*ConfigSqitch, error) {
var err error
configSqitch := ConfigSqitch{Timeout: DEFAULT_TIMEOUT}
Expand All @@ -65,7 +56,8 @@ func GetConfig() (*ConfigSqitch, error) {
configSqitch.PostgresDB = utils.Getenv("POSTGRES_DB", "notset")
configSqitch.Region = utils.Getenv("AWS_REGION", DEFAULT_REGION)
configSqitch.Profile = utils.Getenv("AWS_PROFILE", "")
configSqitch.Filepath = utils.Getenv("CONFIG_SQITCH_PATH", DEFAULT_CONFIG_SQITCH_PATH)
configSqitch.ConfigFilepath = utils.Getenv("CONFIG_SQITCH_PATH", DEFAULT_CONFIG_SQITCH_PATH)
configSqitch.SecretFilepath = utils.Getenv("SECRET_SQITCH_PATH", DEFAULT_SECRET_SQITCH_PATH)

configSqitch.PostgresUser = utils.Getenv("POSTGRES_IAM_USER", "")
if configSqitch.PostgresUser == "" {
Expand All @@ -83,6 +75,29 @@ func GetConfig() (*ConfigSqitch, error) {
return &configSqitch, nil
}

func (c *ConfigSqitch) WriteConfig() error {
outputFile, err := utils.CreateFile(c.ConfigFilepath)
if err != nil {
return err
}
defer outputFile.Close()

tmpl := template.Must(template.New("configTemplate").Parse(templateConfig))
err = tmpl.Execute(outputFile, &c)
if err != nil {
return err
}

secretFile, err := utils.CreateFile(c.SecretFilepath)
if err != nil {
return err
}
defer secretFile.Close()
_, err = secretFile.WriteString(fmt.Sprintf("SQITCH_PASSWORD=%s", c.PostgresPassword))

return err
}

func (c *ConfigSqitch) Connect() (string, error) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Duration(c.Timeout)*time.Millisecond)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion internal/configsqitch/sqitch.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[core]
engine = pg
[engine "pg"]
target = db:pg://{{ .PostgresUser }}:{{ .PostgresPassword }}@{{ .PostgresURI }}:{{ .PostgresPort }}/{{ .PostgresDB }}
target = db:pg://{{ .PostgresUser }}@{{ .PostgresURI }}:{{ .PostgresPort }}/{{ .PostgresDB }}

0 comments on commit 5316648

Please sign in to comment.