-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from gowizzard/development
feat: Add entrypoint for project.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} | ||
|
||
} |