Skip to content

Commit

Permalink
fix(config): loading env file
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Nov 28, 2023
1 parent 731a945 commit b0f4f56
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ cd TG-FileStreamBot
go get -u
go build .
chmod +x fsb
nano fsb.env
# (add your environment variables, see the next section for more info)
./fsb
```

Expand All @@ -71,7 +73,7 @@ and to stop the program,
## Setting up things

If you're locally hosting, create a file named `.env` in the root directory and add all the variables there.
An example of `.env` file:
An example of `fsb.env` file:

```sh
API_ID=452525
Expand Down
24 changes: 12 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package config

import (
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"

Expand All @@ -32,11 +30,19 @@ type config struct {

var botTokenRegex = regexp.MustCompile(`MULTI\_TOKEN[\d+]=(.*)`)

func (c *config) setupEnvVars() {
envPath := filepath.Join(callerDir(), "fsb.env")
func (c *config) setupEnvVars(log *zap.Logger) {
envPath := filepath.Clean("fsb.env")
log.Sugar().Infof("Trying to load ENV vars from %s", envPath)
err := godotenv.Load(envPath)
if err != nil {
panic(err)
if os.IsNotExist(err) {
log.WithOptions(zap.AddStacktrace(zap.DPanicLevel)).Sugar().Errorf("ENV file not found: %s", envPath)
log.Sugar().Info("Please create fsb.env file")
log.Sugar().Info("For more info, refer: https://github.com/EverythingSuckz/TG-FileStreamBot/tree/golang#setting-up-things")
os.Exit(1)
} else {
panic(err)
}
}
err = envconfig.Process("", c)
if err != nil {
Expand All @@ -54,7 +60,7 @@ func (c *config) setupEnvVars() {
func Load(log *zap.Logger) {
log = log.Named("Config")
defer log.Info("Loaded config")
ValueOf.setupEnvVars()
ValueOf.setupEnvVars(log)
ValueOf.LogChannelID = int64(stripInt(log, int(ValueOf.LogChannelID)))
if ValueOf.HashLength == 0 {
log.Sugar().Info("HASH_LENGTH can't be 0, defaulting to 6")
Expand Down Expand Up @@ -87,9 +93,3 @@ func abs(x int) int {
}
return x
}

func callerDir() string {
_, b, _, _ := runtime.Caller(0)
d := path.Join(path.Dir(b))
return filepath.Dir(d)
}

0 comments on commit b0f4f56

Please sign in to comment.