Merge pull request #26 from jkaninda/develop
refractor: refactoring of code
This commit is contained in:
@@ -50,5 +50,6 @@ func init() {
|
||||
rootCmd.PersistentFlags().IntP("port", "p", 5432, "Set database port")
|
||||
rootCmd.PersistentFlags().BoolP("help", "h", false, "Print this help message")
|
||||
rootCmd.PersistentFlags().BoolP("version", "v", false, "shows version information")
|
||||
rootCmd.AddCommand(VersionCmd)
|
||||
|
||||
}
|
||||
|
||||
23
cmd/version.go
Normal file
23
cmd/version.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
)
|
||||
|
||||
var appVersion = os.Getenv("VERSION")
|
||||
|
||||
var VersionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Show version",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
Version()
|
||||
},
|
||||
}
|
||||
|
||||
// Version display application version
|
||||
func Version() {
|
||||
fmt.Printf("Version: %s \n", appVersion)
|
||||
fmt.Print()
|
||||
}
|
||||
3
main.go
3
main.go
@@ -130,6 +130,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
storage = os.Getenv("STORAGE")
|
||||
|
||||
err := os.Setenv("STORAGE_PATH", storagePath)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -142,8 +143,6 @@ func version() {
|
||||
fmt.Print()
|
||||
}
|
||||
func main() {
|
||||
//cmd.Execute()
|
||||
|
||||
err := os.Setenv("STORAGE_PATH", storagePath)
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
@@ -46,6 +46,7 @@ func BackupDatabase(disableCompression bool) {
|
||||
|
||||
if disableCompression {
|
||||
bkFileName = fmt.Sprintf("%s_%s.sql", dbName, time.Now().Format("20060102_150405"))
|
||||
// Execute pg_dump
|
||||
cmd := exec.Command("pg_dump",
|
||||
"-h", dbHost,
|
||||
"-p", dbPort,
|
||||
@@ -56,7 +57,7 @@ func BackupDatabase(disableCompression bool) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// save output
|
||||
file, err := os.Create(fmt.Sprintf("%s/%s", storagePath, bkFileName))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -67,9 +68,10 @@ func BackupDatabase(disableCompression bool) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Info("Database has been backed up")
|
||||
utils.Done("Database has been backed up")
|
||||
|
||||
} else {
|
||||
// Execute pg_dump
|
||||
cmd := exec.Command("pg_dump",
|
||||
"-h", dbHost,
|
||||
"-p", dbPort,
|
||||
@@ -82,6 +84,7 @@ func BackupDatabase(disableCompression bool) {
|
||||
}
|
||||
gzipCmd := exec.Command("gzip")
|
||||
gzipCmd.Stdin = stdout
|
||||
// save output
|
||||
gzipCmd.Stdout, err = os.Create(fmt.Sprintf("%s/%s", storagePath, bkFileName))
|
||||
gzipCmd.Start()
|
||||
if err != nil {
|
||||
@@ -93,7 +96,7 @@ func BackupDatabase(disableCompression bool) {
|
||||
if err := gzipCmd.Wait(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Info("Database has been backed up")
|
||||
utils.Done("Database has been backed up")
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -30,23 +30,23 @@ func RestoreDatabase(file string) {
|
||||
utils.TestDatabaseConnection()
|
||||
|
||||
extension := filepath.Ext(fmt.Sprintf("%s/%s", storagePath, file))
|
||||
// GZ compressed file
|
||||
// Restore from compressed file / .sql.gz
|
||||
if extension == ".gz" {
|
||||
str := "zcat " + fmt.Sprintf("%s/%s", storagePath, file) + " | psql -h " + os.Getenv("DB_HOST") + " -p " + os.Getenv("DB_PORT") + " -U " + os.Getenv("DB_USERNAME") + " -v -d " + os.Getenv("DB_NAME")
|
||||
_, err := exec.Command("bash", "-c", str).Output()
|
||||
if err != nil {
|
||||
utils.Fatal("Error, in restoring the database")
|
||||
}
|
||||
utils.Info("Database has been restored")
|
||||
utils.Done("Database has been restored")
|
||||
|
||||
} else if extension == ".sql" {
|
||||
//SQL file
|
||||
//Restore from sql file
|
||||
str := "cat " + fmt.Sprintf("%s/%s", storagePath, file) + " | psql -h " + os.Getenv("DB_HOST") + " -p " + os.Getenv("DB_PORT") + " -U " + os.Getenv("DB_USERNAME") + " -v -d " + os.Getenv("DB_NAME")
|
||||
_, err := exec.Command("bash", "-c", str).Output()
|
||||
if err != nil {
|
||||
utils.Fatal("Error in restoring the database", err)
|
||||
}
|
||||
utils.Info("Database has been restored")
|
||||
utils.Done("Database has been restored")
|
||||
} else {
|
||||
utils.Fatal("Unknown file extension ", extension)
|
||||
}
|
||||
|
||||
@@ -13,23 +13,17 @@ import (
|
||||
)
|
||||
|
||||
func Info(v ...any) {
|
||||
fmt.Println("[INFO] ", fmt.Sprint(v...))
|
||||
fmt.Println("⒤ ", fmt.Sprint(v...))
|
||||
}
|
||||
func Infof(msg string, v ...any) {
|
||||
fmt.Printf("[INFO] "+msg, v...)
|
||||
}
|
||||
func Warning(message string) {
|
||||
fmt.Println("[WARNING]", message)
|
||||
}
|
||||
func Warningf(msg string, v ...any) {
|
||||
fmt.Printf("[WARNING] "+msg, v...)
|
||||
func Done(v ...any) {
|
||||
fmt.Println("✔ ", fmt.Sprint(v...))
|
||||
}
|
||||
func Fatal(v ...any) {
|
||||
fmt.Println("[ERROR] ", fmt.Sprint(v...))
|
||||
fmt.Println("✘ ", fmt.Sprint(v...))
|
||||
os.Exit(1)
|
||||
}
|
||||
func Fatalf(msg string, v ...any) {
|
||||
fmt.Printf("[ERROR] "+msg, v...)
|
||||
fmt.Printf("✘ "+msg, v...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user