refactor: Restructure project files for better organization, readability, and maintainability

This commit is contained in:
2024-11-04 09:00:14 +01:00
parent f781b69b64
commit d97a0aafea
11 changed files with 52 additions and 32 deletions

View File

@@ -7,7 +7,7 @@
package cmd package cmd
import ( import (
"github.com/jkaninda/pg-bkup/pkg" "github.com/jkaninda/pg-bkup/internal"
"github.com/jkaninda/pg-bkup/utils" "github.com/jkaninda/pg-bkup/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -18,7 +18,7 @@ var BackupCmd = &cobra.Command{
Example: utils.BackupExample, Example: utils.BackupExample,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 { if len(args) == 0 {
pkg.StartBackup(cmd) internal.StartBackup(cmd)
} else { } else {
utils.Fatal(`"backup" accepts no argument %q`, args) utils.Fatal(`"backup" accepts no argument %q`, args)

View File

@@ -7,7 +7,7 @@
package cmd package cmd
import ( import (
"github.com/jkaninda/pg-bkup/pkg" "github.com/jkaninda/pg-bkup/internal"
"github.com/jkaninda/pg-bkup/utils" "github.com/jkaninda/pg-bkup/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -17,7 +17,7 @@ var MigrateCmd = &cobra.Command{
Short: "Migrate database from a source database to a target database", Short: "Migrate database from a source database to a target database",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 { if len(args) == 0 {
pkg.StartMigration(cmd) internal.StartMigration(cmd)
} else { } else {
utils.Fatal(`"migrate" accepts no argument %q`, args) utils.Fatal(`"migrate" accepts no argument %q`, args)

View File

@@ -7,7 +7,7 @@
package cmd package cmd
import ( import (
"github.com/jkaninda/pg-bkup/pkg" "github.com/jkaninda/pg-bkup/internal"
"github.com/jkaninda/pg-bkup/utils" "github.com/jkaninda/pg-bkup/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -18,7 +18,7 @@ var RestoreCmd = &cobra.Command{
Example: utils.RestoreExample, Example: utils.RestoreExample,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 { if len(args) == 0 {
pkg.StartRestore(cmd) internal.StartRestore(cmd)
} else { } else {
utils.Fatal(`"restore" accepts no argument %q`, args) utils.Fatal(`"restore" accepts no argument %q`, args)

View File

@@ -1,10 +1,10 @@
// Package pkg / // Package internal /
/***** /*****
@author Jonas Kaninda @author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT> @license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda @Copyright © 2024 Jonas Kaninda
**/ **/
package pkg package internal
import ( import (
"fmt" "fmt"

View File

@@ -1,10 +1,10 @@
// Package pkg / // Package internal /
/***** /*****
@author Jonas Kaninda @author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT> @license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda @Copyright © 2024 Jonas Kaninda
**/ **/
package pkg package internal
import ( import (
"fmt" "fmt"

View File

@@ -1,10 +1,10 @@
// Package pkg / // Package internal /
/***** /*****
@author Jonas Kaninda @author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT> @license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda @Copyright © 2024 Jonas Kaninda
**/ **/
package pkg package internal
import ( import (
"bytes" "bytes"

View File

@@ -1,10 +1,10 @@
// Package pkg / // Package internal /
/***** /*****
@author Jonas Kaninda @author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT> @license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda @Copyright © 2024 Jonas Kaninda
**/ **/
package pkg package internal
import ( import (
"fmt" "fmt"

View File

@@ -1,10 +1,10 @@
// Package pkg / // Package internal /
/***** /*****
@author Jonas Kaninda @author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT> @license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda @Copyright © 2024 Jonas Kaninda
**/ **/
package pkg package internal
import ( import (
"os" "os"

View File

@@ -1,10 +1,10 @@
// Package pkg / // Package internal /
/***** /*****
@author Jonas Kaninda @author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT> @license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda @Copyright © 2024 Jonas Kaninda
**/ **/
package pkg package internal
const tmpPath = "/tmp/backup" const tmpPath = "/tmp/backup"
const gpgHome = "/config/gnupg" const gpgHome = "/config/gnupg"

View File

@@ -6,6 +6,8 @@
**/ **/
package utils package utils
import "os"
const RestoreExample = "restore --dbname database --file db_20231219_022941.sql.gz\n" + const RestoreExample = "restore --dbname database --file db_20231219_022941.sql.gz\n" +
"restore --dbname database --storage s3 --path /custom-path --file db_20231219_022941.sql.gz" "restore --dbname database --storage s3 --path /custom-path --file db_20231219_022941.sql.gz"
const BackupExample = "backup --dbname database --disable-compression\n" + const BackupExample = "backup --dbname database --disable-compression\n" +
@@ -14,3 +16,20 @@ const BackupExample = "backup --dbname database --disable-compression\n" +
const MainExample = "backup --dbname database --disable-compression\n" + const MainExample = "backup --dbname database --disable-compression\n" +
"backup --dbname database --storage s3 --path /custom-path\n" + "backup --dbname database --storage s3 --path /custom-path\n" +
"restore --dbname database --file db_20231219_022941.sql.gz" "restore --dbname database --file db_20231219_022941.sql.gz"
var Version string
func VERSION(def string) string {
build := os.Getenv("VERSION")
if build == "" {
return def
}
return build
}
func FullVersion() string {
ver := Version
if b := VERSION(""); b != "" {
return b
}
return ver
}

View File

@@ -8,51 +8,52 @@ package utils
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"time"
) )
// Info message // Info message
func Info(msg string, args ...any) { func Info(msg string, args ...any) {
var currentTime = time.Now().Format("2006/01/02 15:04:05") log.SetOutput(os.Stdout)
formattedMessage := fmt.Sprintf(msg, args...) formattedMessage := fmt.Sprintf(msg, args...)
if len(args) == 0 { if len(args) == 0 {
fmt.Printf("%s INFO: %s\n", currentTime, msg) log.Printf("INFO: %s\n", msg)
} else { } else {
fmt.Printf("%s INFO: %s\n", currentTime, formattedMessage) log.Printf("INFO: %s\n", formattedMessage)
} }
} }
// Warn Warning message // Warn a Warning message
func Warn(msg string, args ...any) { func Warn(msg string, args ...any) {
var currentTime = time.Now().Format("2006/01/02 15:04:05") log.SetOutput(os.Stdout)
formattedMessage := fmt.Sprintf(msg, args...) formattedMessage := fmt.Sprintf(msg, args...)
if len(args) == 0 { if len(args) == 0 {
fmt.Printf("%s WARN: %s\n", currentTime, msg) log.Printf("WARN: %s\n", msg)
} else { } else {
fmt.Printf("%s WARN: %s\n", currentTime, formattedMessage) log.Printf("WARN: %s\n", formattedMessage)
} }
} }
// Error error message // Error error message
func Error(msg string, args ...any) { func Error(msg string, args ...any) {
var currentTime = time.Now().Format("2006/01/02 15:04:05") log.SetOutput(os.Stdout)
formattedMessage := fmt.Sprintf(msg, args...) formattedMessage := fmt.Sprintf(msg, args...)
if len(args) == 0 { if len(args) == 0 {
fmt.Printf("%s ERROR: %s\n", currentTime, msg) log.Printf("ERROR: %s\n", msg)
} else { } else {
fmt.Printf("%s ERROR: %s\n", currentTime, formattedMessage) log.Printf("ERROR: %s\n", formattedMessage)
} }
} }
func Fatal(msg string, args ...any) { func Fatal(msg string, args ...any) {
var currentTime = time.Now().Format("2006/01/02 15:04:05") log.SetOutput(os.Stdout)
// Fatal logs an error message and exits the program. // Fatal logs an error message and exits the program.
formattedMessage := fmt.Sprintf(msg, args...) formattedMessage := fmt.Sprintf(msg, args...)
if len(args) == 0 { if len(args) == 0 {
fmt.Printf("%s ERROR: %s\n", currentTime, msg) log.Printf("ERROR: %s\n", msg)
NotifyError(msg) NotifyError(msg)
} else { } else {
fmt.Printf("%s ERROR: %s\n", currentTime, formattedMessage) log.Printf("ERROR: %s\n", formattedMessage)
NotifyError(formattedMessage) NotifyError(formattedMessage)
} }