Merge branch 'piper/absolute-paths' into 'main'

Use absolute paths where possible

See merge request whom/bingobot!15
This commit is contained in:
piper pentagram 2024-12-05 20:46:15 +00:00
commit 706fc0d769
3 changed files with 23 additions and 5 deletions

View file

@ -2,6 +2,8 @@ package config
import ( import (
"fmt" "fmt"
"os"
"path/filepath"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -36,16 +38,29 @@ type AppConfig struct {
} }
var config *AppConfig var config *AppConfig
var workingDir string
func init() { func init() {
setDefaults() setDefaults()
viper.Unmarshal(&config) 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 { func Get() *AppConfig {
return config return config
} }
func GetWorkingDir() string {
return workingDir
}
func GetDefaultConfig() *AppConfig { func GetDefaultConfig() *AppConfig {
var config *AppConfig var config *AppConfig
setDefaults() setDefaults()
@ -62,7 +77,7 @@ func Init() error {
viper.SetConfigName("config") viper.SetConfigName("config")
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.AddConfigPath(".") viper.AddConfigPath(workingDir)
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { if err != nil {

View file

@ -20,7 +20,7 @@ func Init() {
cfg := config.Get() cfg := config.Get()
lj := &lumberjack.Logger{ lj := &lumberjack.Logger{
Filename: filepath.Join(cfg.LogDir, cfg.LogFile), Filename: filepath.Join(config.GetWorkingDir(), cfg.LogDir, cfg.LogFile),
MaxSize: cfg.LogMaxSizeMB, MaxSize: cfg.LogMaxSizeMB,
MaxBackups: cfg.LogMaxBackups, MaxBackups: cfg.LogMaxBackups,
MaxAge: cfg.LogMaxAgeDays, MaxAge: cfg.LogMaxAgeDays,

View file

@ -1,10 +1,13 @@
PROJECT_ROOT=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
TOKEN_FILE="${PROJECT_ROOT}/bot.token"
APP_ID=1303795224857804851 APP_ID=1303795224857804851
if [ ! -f "bot.token" ]; then if [ ! -f "${TOKEN_FILE}" ]; then
echo "can't startup: bot.token file missing" echo "can't startup: bot.token file missing"
exit 2 exit 2
fi fi
BOT_TOKEN=$(cat bot.token) BOT_TOKEN=$(cat ${TOKEN_FILE})
./bingobot --token ${BOT_TOKEN} ${PROJECT_ROOT}/bingobot --token ${BOT_TOKEN}