From bfcba9a4640bfdf9f734be281c369fca10c8ea18 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Tue, 13 Jun 2023 19:29:54 +0200 Subject: [PATCH] matching env secrets with config file --- main.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/main.go b/main.go index 4131400..0136b75 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "os/signal" @@ -18,6 +19,35 @@ func main() { logger.Error(err.Error()) os.Exit(1) } + type Credentials struct { + Password string + AccessKey string + } + credentials := make(map[string]Credentials) + for i := 0; i < len(config.Bots); i++ { + user := getParam(fmt.Sprintf("MATRIX_BOT%d_ID", i), "") + if user == "" { + logger.Error("missing user id", slog.Int("user", i)) + os.Exit(1) + } + credentials[user] = Credentials{ + Password: getParam(fmt.Sprintf("MATRIX_BOT%d_PASSWORD", i), ""), + AccessKey: getParam(fmt.Sprintf("MATRIX_BOT%d_ACCESSKEY", i), ""), + } + } + for i, bc := range config.Bots { + creds, ok := credentials[bc.UserID] + if !ok { + logger.Error("missing credentials", slog.Int("user", i)) + os.Exit(1) + } + config.Bots[i].UserPassword = creds.Password + config.Bots[i].UserAccessKey = creds.AccessKey + } + + config.OpenAI = bot.ConfigOpenAI{ + APIKey: getParam("OPENAI_API_KEY", ""), + } logger.Info("loaded config", slog.Int("bots", len(config.Bots))) for _, bc := range config.Bots {