mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-07 05:59:43 +01:00
Fix log, add verification of required environment
This commit is contained in:
55
utils/logger.go
Normal file
55
utils/logger.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var currentTime = time.Now().Format("2006/01/02 15:04:05")
|
||||
|
||||
func Info(msg string, args ...any) {
|
||||
formattedMessage := fmt.Sprintf(msg, args...)
|
||||
if len(args) == 0 {
|
||||
fmt.Printf("%s INFO: %s\n", currentTime, msg)
|
||||
} else {
|
||||
fmt.Printf("%s INFO: %s\n", currentTime, formattedMessage)
|
||||
}
|
||||
}
|
||||
|
||||
// Warn warning message
|
||||
func Warn(msg string, args ...any) {
|
||||
formattedMessage := fmt.Sprintf(msg, args...)
|
||||
if len(args) == 0 {
|
||||
fmt.Printf("%s WARN: %s\n", currentTime, msg)
|
||||
} else {
|
||||
fmt.Printf("%s WARN: %s\n", currentTime, formattedMessage)
|
||||
}
|
||||
}
|
||||
func Error(msg string, args ...any) {
|
||||
formattedMessage := fmt.Sprintf(msg, args...)
|
||||
if len(args) == 0 {
|
||||
fmt.Printf("%s ERROR: %s\n", currentTime, msg)
|
||||
} else {
|
||||
fmt.Printf("%s ERROR: %s\n", currentTime, formattedMessage)
|
||||
}
|
||||
}
|
||||
func Done(msg string, args ...any) {
|
||||
formattedMessage := fmt.Sprintf(msg, args...)
|
||||
if len(args) == 0 {
|
||||
fmt.Printf("%s INFO: %s\n", currentTime, msg)
|
||||
} else {
|
||||
fmt.Printf("%s INFO: %s\n", currentTime, formattedMessage)
|
||||
}
|
||||
}
|
||||
|
||||
func Fatal(msg string, args ...any) {
|
||||
// Fatal logs an error message and exits the program.
|
||||
formattedMessage := fmt.Sprintf(msg, args...)
|
||||
if len(args) == 0 {
|
||||
fmt.Printf("%s ERROR: %s\n", currentTime, msg)
|
||||
} else {
|
||||
fmt.Printf("%s ERROR: %s\n", currentTime, formattedMessage)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||
"golang.org/x/exp/slog"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -43,7 +42,7 @@ func CreateSession() (*session.Session, error) {
|
||||
|
||||
err = CheckEnvVars(awsVars)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintf("Error checking environment variables\n: %s", err))
|
||||
Error(fmt.Sprintf("Error checking environment variables\n: %s", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
// S3 Config
|
||||
|
||||
@@ -10,51 +10,12 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/exp/slog"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func Info(msg string, args ...any) {
|
||||
if len(args) == 0 {
|
||||
slog.Info(msg)
|
||||
} else {
|
||||
slog.Info(fmt.Sprintf(msg, args...))
|
||||
}
|
||||
}
|
||||
func Worn(msg string, args ...any) {
|
||||
if len(args) == 0 {
|
||||
slog.Warn(msg)
|
||||
} else {
|
||||
slog.Warn(fmt.Sprintf(msg, args...))
|
||||
}
|
||||
}
|
||||
func Error(msg string, args ...any) {
|
||||
if len(args) == 0 {
|
||||
slog.Error(msg)
|
||||
} else {
|
||||
slog.Error(fmt.Sprintf(msg, args...))
|
||||
}
|
||||
}
|
||||
func Done(msg string, args ...any) {
|
||||
if len(args) == 0 {
|
||||
slog.Info(msg)
|
||||
} else {
|
||||
slog.Info(fmt.Sprintf(msg, args...))
|
||||
}
|
||||
}
|
||||
func Fatal(msg string, args ...any) {
|
||||
// Fatal logs an error message and exits the program.
|
||||
if len(args) == 0 {
|
||||
slog.Error(msg)
|
||||
} else {
|
||||
slog.Error(fmt.Sprintf(msg, args...))
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func FileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
@@ -150,7 +111,7 @@ func TestDatabaseConnection() {
|
||||
cmd.Stderr = &out
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintf("Error testing database connection: %v\nOutput: %s\n", err, out.String()))
|
||||
Error(fmt.Sprintf("Error testing database connection: %v\nOutput: %s\n", err, out.String()))
|
||||
os.Exit(1)
|
||||
|
||||
}
|
||||
@@ -196,7 +157,7 @@ func GetEnvVariable(envName, oldEnvName string) string {
|
||||
if err != nil {
|
||||
return value
|
||||
}
|
||||
Worn("%s is deprecated, please use %s instead!\n", oldEnvName, envName)
|
||||
Warn("%s is deprecated, please use %s instead!\n", oldEnvName, envName)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user