Files
pg-bkup/utils/utils.go

198 lines
4.4 KiB
Go
Raw Normal View History

2024-01-18 16:08:11 +01:00
package utils
/*****
* MySQL Backup & Restore
2024-01-18 16:08:11 +01:00
* @author Jonas Kaninda
* @license MIT License <https://opensource.org/licenses/MIT>
* @link https://github.com/jkaninda/mysql-bkup
2024-01-18 16:08:11 +01:00
**/
import (
2024-07-30 07:02:18 +02:00
"bytes"
2024-01-18 16:08:11 +01:00
"fmt"
"github.com/spf13/cobra"
2024-08-03 00:49:14 +02:00
"golang.org/x/exp/slog"
2024-07-29 07:33:26 +02:00
"io"
2024-01-18 16:08:11 +01:00
"io/fs"
"os"
2024-07-30 07:02:18 +02:00
"os/exec"
2024-01-18 16:08:11 +01:00
)
func Info(v ...any) {
2024-01-19 14:41:25 +01:00
fmt.Println("⒤ ", fmt.Sprint(v...))
2024-01-18 16:08:11 +01:00
}
2024-08-03 00:49:14 +02:00
func Worn(msg string, v ...any) {
slog.Warn(fmt.Sprintf(msg, v))
}
2024-01-19 14:41:25 +01:00
func Done(v ...any) {
fmt.Println("✔ ", fmt.Sprint(v...))
2024-01-18 16:08:11 +01:00
}
func Fatal(v ...any) {
2024-01-19 14:41:25 +01:00
fmt.Println("✘ ", fmt.Sprint(v...))
2024-01-18 16:08:11 +01:00
os.Exit(1)
}
func Fatalf(msg string, v ...any) {
2024-01-19 14:41:25 +01:00
fmt.Printf("✘ "+msg, v...)
2024-01-18 16:08:11 +01:00
os.Exit(1)
}
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func WriteToFile(filePath, content string) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(content)
return err
}
2024-07-29 07:33:26 +02:00
func DeleteFile(filePath string) error {
err := os.Remove(filePath)
if err != nil {
return fmt.Errorf("failed to delete file: %v", err)
}
return nil
}
func CopyFile(src, dst string) error {
// Open the source file for reading
sourceFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open source file: %v", err)
}
defer sourceFile.Close()
// Create the destination file
destinationFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to create destination file: %v", err)
}
defer destinationFile.Close()
// Copy the content from source to destination
_, err = io.Copy(destinationFile, sourceFile)
if err != nil {
return fmt.Errorf("failed to copy file: %v", err)
}
// Flush the buffer to ensure all data is written
err = destinationFile.Sync()
if err != nil {
return fmt.Errorf("failed to sync destination file: %v", err)
}
return nil
}
2024-01-18 16:08:11 +01:00
func ChangePermission(filePath string, mod int) {
if err := os.Chmod(filePath, fs.FileMode(mod)); err != nil {
Fatalf("Error changing permissions of %s: %v\n", filePath, err)
}
}
func IsDirEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == nil {
return false, nil
}
return true, nil
}
// TestDatabaseConnection tests the database connection
func TestDatabaseConnection() {
2024-07-30 07:02:18 +02:00
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)
if value != "" {
err := os.Setenv(envName, value)
if err != nil {
return value
}
}
return os.Getenv(envName)
}
func FlagGetString(cmd *cobra.Command, flagName string) string {
value, _ := cmd.Flags().GetString(flagName)
if value != "" {
return value
}
return ""
}
func FlagGetBool(cmd *cobra.Command, flagName string) bool {
value, _ := cmd.Flags().GetBool(flagName)
return value
}
func SetEnv(key, value string) {
err := os.Setenv(key, value)
if err != nil {
return
}
}
func GetEnvVariable(envName, oldEnvName string) string {
value := os.Getenv(envName)
if value == "" {
value = os.Getenv(oldEnvName)
if value != "" {
2024-08-03 00:49:14 +02:00
slog.Warn(fmt.Sprintf("%s is deprecated, please use %s instead!\n", oldEnvName, envName))
}
}
return value
}
func ShowHistory() {
}