Files
pg-bkup/utils/utils.go

144 lines
2.9 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 (
"fmt"
"github.com/spf13/cobra"
2024-07-29 07:33:26 +02:00
"io"
2024-01-18 16:08:11 +01:00
"io/fs"
"os"
)
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-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() {
Info("Testing database connection...")
// Test database connection
}
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 ShowHistory() {
}