move %tmp% dereferencing to stage 6 of initialization, using a simpler and more efficient way of checking for contents

This commit is contained in:
2025-07-30 19:17:36 +03:00
parent b601962354
commit 299fd59e19

View File

@@ -10,10 +10,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings"
"github.com/akyaiy/GoSally-mvp/internal/core/run_manager"
"github.com/akyaiy/GoSally-mvp/internal/engine/config" "github.com/akyaiy/GoSally-mvp/internal/engine/config"
"gopkg.in/natefinch/lumberjack.v2" "gopkg.in/natefinch/lumberjack.v2"
) )
@@ -32,11 +29,11 @@ func (w *SlogWriter) Write(p []byte) (n int, err error) {
} }
// SetupLogger initializes and returns a logger based on the provided environment. // SetupLogger initializes and returns a logger based on the provided environment.
func SetupLogger(o config.Log) (*slog.Logger, error) { func SetupLogger(o *config.Log) (*slog.Logger, error) {
var handlerOpts = slog.HandlerOptions{} var handlerOpts = slog.HandlerOptions{}
var writer io.Writer = os.Stdout var writer io.Writer = os.Stdout
switch o.Level { switch *o.Level {
case "debug": case "debug":
GlobalLevel = slog.LevelDebug GlobalLevel = slog.LevelDebug
handlerOpts.Level = slog.LevelDebug handlerOpts.Level = slog.LevelDebug
@@ -48,32 +45,9 @@ func SetupLogger(o config.Log) (*slog.Logger, error) {
handlerOpts.Level = slog.LevelInfo handlerOpts.Level = slog.LevelInfo
} }
if o.OutPath != "" { if *o.OutPath != "" {
repl := map[string]string{
"tmp": filepath.Clean(run_manager.RuntimeDir()),
}
re := regexp.MustCompile(`%(\w+)%`)
result := re.ReplaceAllStringFunc(o.OutPath, func(match string) string {
sub := re.FindStringSubmatch(match)
if len(sub) < 2 {
return match
}
key := sub[1]
if val, ok := repl[key]; ok {
return val
}
return match
})
if strings.Contains(o.OutPath, "%tmp%") {
relPath := strings.TrimPrefix(result, filepath.Clean(run_manager.RuntimeDir()))
if err := run_manager.SetDir(relPath); err != nil {
return nil, err
}
}
logFile := &lumberjack.Logger{ logFile := &lumberjack.Logger{
Filename: filepath.Join(result, "event.log"), Filename: filepath.Join(*o.OutPath, "event.log"),
MaxSize: 10, MaxSize: 10,
MaxBackups: 5, MaxBackups: 5,
MaxAge: 28, MaxAge: 28,