62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"context"
|
|
"git.pixelridgesoftworks.com/BackGo/cmd"
|
|
backGoConfig "git.pixelridgesoftworks.com/BackGo/config"
|
|
"git.pixelridgesoftworks.com/BackGo/pkg/backup"
|
|
"git.pixelridgesoftworks.com/BackGo/pkg/logger"
|
|
)
|
|
|
|
func main() {
|
|
// Load configuration
|
|
cfg := backGoConfig.LoadConfig()
|
|
|
|
// Initialize Logger
|
|
log := logger.NewLogger()
|
|
|
|
// Set the logger level based on the configuration
|
|
log.SetLevel(mapConfigLogLevelToLoggerLogLevel(cfg.LogLevel))
|
|
|
|
// Adjust logger output based on configuration or environment variables
|
|
logFileEnv := os.Getenv("LOG_FILE")
|
|
if logFileEnv != "" {
|
|
if err := log.LogToFile(logFileEnv); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Failed to set logger output to file: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// Initialize the database manager
|
|
dbManager := backup.NewDBManager(cfg)
|
|
|
|
// Create a context
|
|
ctx := context.Background()
|
|
|
|
// Attach the logger and DB manager to the context
|
|
ctx = context.WithValue(ctx, "logger", log)
|
|
ctx = context.WithValue(ctx, "dbManager", dbManager)
|
|
|
|
// Now, pass only the context (and any other necessary parameters) to your command execution
|
|
// Note: You'll need to adjust your application's command handling to work with context
|
|
if err := cmd.ExecuteCommandWithContext(ctx, cmd.RootCmd, os.Args[1:]); err != nil {
|
|
log.Error(fmt.Sprintf("Command execution failed: %v", err))
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func mapConfigLogLevelToLoggerLogLevel(lvl backGoConfig.LogLevel) logger.LogLevel {
|
|
switch lvl {
|
|
case backGoConfig.LogLevelInfo:
|
|
return logger.LogLevelInfo
|
|
case backGoConfig.LogLevelWarning:
|
|
return logger.LogLevelWarning
|
|
case backGoConfig.LogLevelError:
|
|
return logger.LogLevelError
|
|
default:
|
|
return logger.LogLevelInfo // Default to Info if unsure
|
|
}
|
|
}
|