Use absolute paths where possible

bingobot blindly uses the shell's working dir to write logs and read
configs. This change introduces the following changes:

1. start.sh now determines the project root's absolute path before
   reading the token file and starting the app.
2. the config package now determine's the executable's working directory
   (rather than assuming it's the same as the shell's) before loading
   configs. It also now exposes this for use in other packages.
3. the logging package now uses config.GetWorkingDir() to determine
   where to write logs.
This commit is contained in:
Piper Pentagram 2024-11-14 11:16:30 -08:00
parent ad873ef836
commit 0c64670c33
3 changed files with 23 additions and 5 deletions

View file

@ -2,6 +2,8 @@ package config
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/viper"
)
@ -36,16 +38,29 @@ type AppConfig struct {
}
var config *AppConfig
var workingDir string
func init() {
setDefaults()
viper.Unmarshal(&config)
ex, err := os.Executable()
if err != nil {
panic(fmt.Errorf("failed to get path of executable (self): %v", err))
}
workingDir = filepath.Dir(ex)
}
func Get() *AppConfig {
return config
}
func GetWorkingDir() string {
return workingDir
}
func GetDefaultConfig() *AppConfig {
var config *AppConfig
setDefaults()
@ -62,7 +77,7 @@ func Init() error {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath(workingDir)
err := viper.ReadInConfig()
if err != nil {