2024-01-18 14:19:27 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
/*****
|
|
|
|
|
* MySQL Backup & Restore
|
|
|
|
|
* @author Jonas Kaninda
|
|
|
|
|
* @license MIT License <https://opensource.org/licenses/MIT>
|
|
|
|
|
* @link https://github.com/jkaninda/mysql-bkup
|
|
|
|
|
**/
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2024-01-20 13:04:39 +01:00
|
|
|
"github.com/spf13/cobra"
|
2024-08-03 16:03:17 +02:00
|
|
|
"io"
|
2024-01-18 14:19:27 +01:00
|
|
|
"io/fs"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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-08-03 16:03:17 +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 14:19:27 +01:00
|
|
|
func ChangePermission(filePath string, mod int) {
|
|
|
|
|
if err := os.Chmod(filePath, fs.FileMode(mod)); err != nil {
|
2024-08-03 16:03:17 +02:00
|
|
|
Fatal("Error changing permissions of %s: %v\n", filePath, err)
|
2024-01-18 14:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
2024-01-19 05:31:30 +01:00
|
|
|
|
2024-01-20 13:04:39 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-03 16:03:17 +02:00
|
|
|
func GetEnvVariable(envName, oldEnvName string) string {
|
|
|
|
|
value := os.Getenv(envName)
|
|
|
|
|
if value == "" {
|
|
|
|
|
value = os.Getenv(oldEnvName)
|
|
|
|
|
if value != "" {
|
|
|
|
|
err := os.Setenv(envName, value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return value
|
|
|
|
|
}
|
2024-08-04 01:42:51 +02:00
|
|
|
Warn("%s is deprecated, please use %s instead!", oldEnvName, envName)
|
2024-01-20 13:04:39 +01:00
|
|
|
|
2024-08-03 16:03:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
}
|
2024-01-20 13:04:39 +01:00
|
|
|
func ShowHistory() {
|
|
|
|
|
}
|
2024-08-03 16:03:17 +02:00
|
|
|
|
|
|
|
|
// CheckEnvVars checks if all the specified environment variables are set
|
|
|
|
|
func CheckEnvVars(vars []string) error {
|
|
|
|
|
missingVars := []string{}
|
|
|
|
|
|
|
|
|
|
for _, v := range vars {
|
|
|
|
|
if os.Getenv(v) == "" {
|
|
|
|
|
missingVars = append(missingVars, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(missingVars) > 0 {
|
|
|
|
|
return fmt.Errorf("missing environment variables: %v", missingVars)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-08-30 19:58:12 +02:00
|
|
|
|
|
|
|
|
// MakeDir create directory
|
|
|
|
|
func MakeDir(dirPath string) error {
|
|
|
|
|
err := os.Mkdir(dirPath, 0700)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MakeDirAll create directory
|
|
|
|
|
func MakeDirAll(dirPath string) error {
|
|
|
|
|
err := os.MkdirAll(dirPath, 0700)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|