Skip to content

Commit

Permalink
Merge pull request #22 from gowizzard/development
Browse files Browse the repository at this point in the history
feat: Add entrypoint for project.
  • Loading branch information
gowizzard authored Nov 8, 2023
2 parents d1b08b4 + ca0649a commit cc70c04
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cmd/trigsnfuncs/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
// Package main is the entrypoint for the hole project.
package main

import (
"context"
"github.com/gowizzard/trigsnfuncs/internal/database"
"github.com/gowizzard/trigsnfuncs/internal/escape"
"github.com/gowizzard/trigsnfuncs/internal/read"
"github.com/gowizzard/trigsnfuncs/internal/write"
"os"
)

// path is to set the path to the directory where the redis triggers or functions are stored.
const (
path = "/scripts"
)

// address, username and password are to set the environment variables.
var (
address = os.Getenv("REDIS_ADDRESS")
username = os.Getenv("REDIS_USERNAME")
password = os.Getenv("REDIS_PASSWORD")
)

// init is to check if the environment variables are set.
func init() {

if address == "" {
write.Logger.Error("Environment variable REDIS_ADDRESS is not set.")
os.Exit(1)
}

}

// main is to load the redis triggers or functions to the database.
func main() {

c, err := database.NewClient(context.Background(), database.Options{
Address: address,
Username: username,
Password: password,
})
if err != nil {
write.Logger.Error("Create redis database client.", err)
os.Exit(1)
}

defer func(c *database.Client) {
err := c.Close()
if err != nil {
write.Logger.Error("Close redis database connection.", err)
os.Exit(1)
}
}(c)

scripts, err := read.Directory(path)
if err != nil {
write.Logger.Error("Read redis triggers or functions from directory.", err)
os.Exit(1)
}

for _, value := range scripts {

_, err = c.TFunctionLoad(escape.Bytes(value))
if err != nil {
write.Logger.Error("Load redis trigger or function to database.", err)
}

}

}

0 comments on commit cc70c04

Please sign in to comment.