Refactoring of code

This commit is contained in:
2024-07-30 07:02:18 +02:00
parent c277228ab3
commit 05a195e1ba
8 changed files with 77 additions and 32 deletions

View File

@@ -12,25 +12,27 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
// CreateSession creates a new AWS session
func CreateSession() (*session.Session, error) {
//key := aws.String("testobject")
endPoint := os.Getenv("S3_ENDPOINT")
//bucket := os.Getenv("BUCKET_NAME")
region := os.Getenv("REGION")
accessKey := os.Getenv("ACCESS_KEY")
secretKey := os.Getenv("SECRET_KEY")
region := os.Getenv("AWS_REGION")
awsDisableSsl, err := strconv.ParseBool(os.Getenv("AWS_DISABLE_SSL"))
if err != nil {
Fatalf("Unable to parse AWS_DISABLE_SSL env var: %s", err)
}
// Configure to use MinIO Server
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""),
Endpoint: aws.String(endPoint),
Region: aws.String(region),
DisableSSL: aws.Bool(false),
DisableSSL: aws.Bool(awsDisableSsl),
S3ForcePathStyle: aws.Bool(true),
}
return session.NewSession(s3Config)

View File

@@ -7,11 +7,13 @@ package utils
* @link https://github.com/jkaninda/mysql-bkup
**/
import (
"bytes"
"fmt"
"github.com/spf13/cobra"
"io"
"io/fs"
"os"
"os/exec"
)
func Info(v ...any) {
@@ -105,8 +107,46 @@ func IsDirEmpty(name string) (bool, error) {
// TestDatabaseConnection tests the database connection
func TestDatabaseConnection() {
Info("Testing database connection...")
// Test database connection
dbHost := os.Getenv("DB_HOST")
dbPassword := os.Getenv("DB_PASSWORD")
dbUserName := os.Getenv("DB_USERNAME")
dbName := os.Getenv("DB_NAME")
dbPort := os.Getenv("DB_PORT")
if os.Getenv("DB_HOST") == "" || os.Getenv("DB_NAME") == "" || os.Getenv("DB_USERNAME") == "" || os.Getenv("DB_PASSWORD") == "" {
Fatal("Please make sure all required database environment variables are set")
} else {
Info("Connecting to database ...")
// Test database connection
query := "SELECT version();"
// Set the environment variable for the database password
err := os.Setenv("PGPASSWORD", dbPassword)
if err != nil {
return
}
// Prepare the psql command
cmd := exec.Command("psql",
"-U", dbUserName, // database user
"-d", dbName, // database name
"-h", dbHost, // host
"-p", dbPort, // port
"-c", query, // SQL command to execute
)
// Capture the output
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
// Run the command and capture any errors
err = cmd.Run()
if err != nil {
fmt.Printf("Error running psql command: %v\nOutput: %s\n", err, out.String())
return
}
Info("Successfully connected to database")
}
}
func GetEnv(cmd *cobra.Command, flagName, envName string) string {
value, _ := cmd.Flags().GetString(flagName)