Starting refactor to improve how ghetto the object handling was
This commit is contained in:
parent
a63d615ad2
commit
b6ad826204
|
@ -1,19 +1,18 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"strconv"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"pixelridgesoftworks.com/BackGo/config"
|
"git.pixelridgesoftworks.com/BackGo/config"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const configFilePath = "/etc/PixelRidge/BackGo/config.json"
|
|
||||||
|
|
||||||
// OpenConfigInNano opens the configuration file in the nano editor.
|
|
||||||
func OpenConfigInNano() error {
|
func OpenConfigInNano() error {
|
||||||
|
configFilePath := config.GetConfigFilePath()
|
||||||
cmd := exec.Command("nano", configFilePath)
|
cmd := exec.Command("nano", configFilePath)
|
||||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
|
@ -23,15 +22,24 @@ func OpenConfigInNano() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EditConfigOption allows editing a specific configuration option from the CLI
|
func EditConfigOption(ctx context.Context, optionName, newValue string) error {
|
||||||
func EditConfigOption(optionName, newValue string) error {
|
|
||||||
// Load the current configuration from file
|
|
||||||
cfg, err := config.LoadConfigFromFile()
|
cfg, err := config.LoadConfigFromFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("loading config: %v", err)
|
return fmt.Errorf("loading config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use reflection to find and set the field in the Config struct
|
if err := setConfigOption(cfg, optionName, newValue); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := config.SaveConfigToFile(cfg); err != nil {
|
||||||
|
return fmt.Errorf("saving config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setConfigOption(cfg *config.Config, optionName, newValue string) error {
|
||||||
rConfig := reflect.ValueOf(cfg).Elem()
|
rConfig := reflect.ValueOf(cfg).Elem()
|
||||||
field := rConfig.FieldByNameFunc(func(s string) bool {
|
field := rConfig.FieldByNameFunc(func(s string) bool {
|
||||||
return strings.EqualFold(s, optionName)
|
return strings.EqualFold(s, optionName)
|
||||||
|
@ -41,12 +49,10 @@ func EditConfigOption(optionName, newValue string) error {
|
||||||
return fmt.Errorf("unknown configuration option: %s", optionName)
|
return fmt.Errorf("unknown configuration option: %s", optionName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the field can be set
|
|
||||||
if !field.CanSet() {
|
if !field.CanSet() {
|
||||||
return fmt.Errorf("cannot set configuration option: %s", optionName)
|
return fmt.Errorf("cannot set configuration option: %s", optionName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert and set the field value based on its kind
|
|
||||||
switch field.Kind() {
|
switch field.Kind() {
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
field.SetString(newValue)
|
field.SetString(newValue)
|
||||||
|
@ -56,21 +62,15 @@ func EditConfigOption(optionName, newValue string) error {
|
||||||
return fmt.Errorf("invalid value for %s: %v", optionName, err)
|
return fmt.Errorf("invalid value for %s: %v", optionName, err)
|
||||||
}
|
}
|
||||||
field.SetBool(boolVal)
|
field.SetBool(boolVal)
|
||||||
case reflect.Int:
|
case reflect.Int, reflect.Int64:
|
||||||
intVal, err := strconv.Atoi(newValue)
|
intVal, err := strconv.ParseInt(newValue, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid value for %s: %v", optionName, err)
|
return fmt.Errorf("invalid value for %s: %v", optionName, err)
|
||||||
}
|
}
|
||||||
// Additional checks can be implemented here for specific int fields like LogLevel
|
field.SetInt(intVal)
|
||||||
field.SetInt(int64(intVal))
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported configuration type for: %s", optionName)
|
return fmt.Errorf("unsupported configuration type for: %s", optionName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the updated configuration back to the file
|
|
||||||
if err := config.SaveConfigToFile(cfg); err != nil {
|
|
||||||
return fmt.Errorf("saving config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,43 +5,38 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"pixelridgesoftworks.com/BackGo/pkg/backup"
|
"git.pixelridgesoftworks.com/BackGo/pkg/backup"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InterfaceCLI manages commands related to node interface operations
|
|
||||||
type InterfaceCLI struct {
|
type InterfaceCLI struct {
|
||||||
NodeAddress string
|
NodeAddress string
|
||||||
DBManager *backup.DBManager // Assuming DBManager is part of the backup package
|
DBManager *backup.DBManager
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewInterfaceCLI creates a new InterfaceCLI instance
|
|
||||||
func NewInterfaceCLI(nodeAddress string, dbManager *backup.DBManager) *InterfaceCLI {
|
func NewInterfaceCLI(nodeAddress string, dbManager *backup.DBManager) *InterfaceCLI {
|
||||||
|
if dbManager == nil {
|
||||||
|
panic("DBManager cannot be nil")
|
||||||
|
}
|
||||||
return &InterfaceCLI{
|
return &InterfaceCLI{
|
||||||
NodeAddress: nodeAddress,
|
NodeAddress: nodeAddress,
|
||||||
DBManager: dbManager,
|
DBManager: dbManager,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TriggerCommand sends a trigger command to a node
|
|
||||||
func (cli *InterfaceCLI) TriggerCommand(action, value string) error {
|
func (cli *InterfaceCLI) TriggerCommand(action, value string) error {
|
||||||
return cli.sendCommandToNode("trigger", action, value)
|
return cli.sendCommandToNode("trigger", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetInfo sends a get_info command to a node
|
|
||||||
func (cli *InterfaceCLI) GetInfo(value string) error {
|
func (cli *InterfaceCLI) GetInfo(value string) error {
|
||||||
return cli.sendCommandToNode("get_info", cli.NodeAddress, value)
|
return cli.sendCommandToNode("get_info", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddNode handles adding a new node on the master node
|
|
||||||
func (cli *InterfaceCLI) AddNode(hostname, token string) error {
|
func (cli *InterfaceCLI) AddNode(hostname, token string) error {
|
||||||
// Verify the hostname resolves to an expected IP before adding
|
expectedIP := "" // Implement IP determination logic here
|
||||||
// This might involve additional logic to determine what the expected IP is
|
|
||||||
expectedIP := "" // Placeholder for expected IP determination logic
|
|
||||||
if !cli.DBManager.VerifyHostname(hostname, expectedIP) {
|
if !cli.DBManager.VerifyHostname(hostname, expectedIP) {
|
||||||
return fmt.Errorf("hostname %s does not resolve to the expected IP %s", hostname, expectedIP)
|
return fmt.Errorf("hostname %s does not resolve to the expected IP %s", hostname, expectedIP)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a token for the new node
|
|
||||||
if err := cli.DBManager.CreateToken(hostname, token); err != nil {
|
if err := cli.DBManager.CreateToken(hostname, token); err != nil {
|
||||||
return fmt.Errorf("failed to create token for hostname %s: %v", hostname, err)
|
return fmt.Errorf("failed to create token for hostname %s: %v", hostname, err)
|
||||||
}
|
}
|
||||||
|
@ -50,16 +45,14 @@ func (cli *InterfaceCLI) AddNode(hostname, token string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenCert generates a certificate for a node
|
|
||||||
func (cli *InterfaceCLI) GenCert(nodeName string) error {
|
func (cli *InterfaceCLI) GenCert(nodeName string) error {
|
||||||
return cli.sendCommandToNode("gencert", nodeName, "")
|
return cli.sendCommandToNode("gencert", nodeName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// sendCommandToNode sends a command to the specified node
|
func (cli *InterfaceCLI) sendCommandToNode(action, value string) error {
|
||||||
func (cli *InterfaceCLI) sendCommandToNode(action, node, value string) error {
|
|
||||||
payload := backup.CommandPayload{
|
payload := backup.CommandPayload{
|
||||||
Action: action,
|
Action: action,
|
||||||
Node: node,
|
Node: cli.NodeAddress,
|
||||||
Value: value,
|
Value: value,
|
||||||
}
|
}
|
||||||
payloadBytes, err := json.Marshal(payload)
|
payloadBytes, err := json.Marshal(payload)
|
||||||
|
@ -69,10 +62,10 @@ func (cli *InterfaceCLI) sendCommandToNode(action, node, value string) error {
|
||||||
|
|
||||||
resp, err := http.Post(fmt.Sprintf("http://%s/api/command", cli.NodeAddress), "application/json", bytes.NewReader(payloadBytes))
|
resp, err := http.Post(fmt.Sprintf("http://%s/api/command", cli.NodeAddress), "application/json", bytes.NewReader(payloadBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error sending command to node %s: %v", node, err)
|
return fmt.Errorf("error sending command to node %s: %v", cli.NodeAddress, err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
fmt.Printf("Command '%s' sent to node %s successfully\n", action, node)
|
fmt.Printf("Command '%s' sent to node %s successfully\n", action, cli.NodeAddress)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
101
cmd/root.go
101
cmd/root.go
|
@ -1,10 +1,11 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"pixelridgesoftworks.com/BackGo/pkg/backup"
|
"git.pixelridgesoftworks.com/BackGo/pkg/backup"
|
||||||
"pixelridgesoftworks.com/BackGo/config"
|
"git.pixelridgesoftworks.com/BackGo/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
|
@ -12,18 +13,12 @@ type Command struct {
|
||||||
Description string
|
Description string
|
||||||
FlagSet *flag.FlagSet
|
FlagSet *flag.FlagSet
|
||||||
SubCommands map[string]*Command
|
SubCommands map[string]*Command
|
||||||
Execute func(cmd *Command, args []string)
|
Execute func(ctx context.Context, cmd *Command, args []string)
|
||||||
}
|
}
|
||||||
|
|
||||||
var appConfig = config.LoadConfig()
|
|
||||||
var dbManager *backup.DBManager
|
|
||||||
|
|
||||||
var RootCmd = NewCommand("backup", "Main entry point for backup utility", nil)
|
var RootCmd = NewCommand("backup", "Main entry point for backup utility", nil)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Assuming NewDBManager is correctly implemented in the backup package and accepts *config.Config
|
|
||||||
dbManager = backup.NewDBManager(appConfig)
|
|
||||||
|
|
||||||
remoteCmd := NewCommand("interface", "Remote node management", nil)
|
remoteCmd := NewCommand("interface", "Remote node management", nil)
|
||||||
|
|
||||||
triggerCmd := NewCommand("trigger", "Trigger an action on a node", handleTrigger)
|
triggerCmd := NewCommand("trigger", "Trigger an action on a node", handleTrigger)
|
||||||
|
@ -39,31 +34,48 @@ func init() {
|
||||||
RootCmd.AddSubCommand(remoteCmd)
|
RootCmd.AddSubCommand(remoteCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleTrigger(cmd *Command, args []string) {
|
func handleTrigger(ctx context.Context, cmd *Command, args []string) {
|
||||||
|
log := ctx.Value("logger").(*logger.Logger)
|
||||||
|
dbManager := ctx.Value("dbManager").(*backup.DBManager)
|
||||||
nodeFlag := cmd.FlagSet.String("node", "", "Specify the node hostname")
|
nodeFlag := cmd.FlagSet.String("node", "", "Specify the node hostname")
|
||||||
actionFlag := cmd.FlagSet.String("action", "", "Specify the action to trigger")
|
actionFlag := cmd.FlagSet.String("action", "", "Specify the action to trigger")
|
||||||
cmd.FlagSet.Parse(args)
|
cmd.FlagSet.Parse(args)
|
||||||
|
|
||||||
if *nodeFlag == "" || *actionFlag == "" {
|
if *nodeFlag == "" || *actionFlag == "" {
|
||||||
fmt.Println("Both node hostname and action are required")
|
log.Println("Both node hostname and action are required")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since backup.NewInterfaceCLI is undefined, this part is commented out
|
cli := NewInterfaceCLI(*nodeFlag, dbManager)
|
||||||
// Assuming you will implement or correct it later
|
if err := cli.TriggerCommand(*actionFlag, ""); err != nil {
|
||||||
// interfaceCLI := backup.NewInterfaceCLI(*nodeFlag, dbManager)
|
log.Printf("Error triggering command on node %s: %v\n", *nodeFlag, err)
|
||||||
// if err := interfaceCLI.TriggerCommand(*actionFlag, ""); err != nil {
|
} else {
|
||||||
// fmt.Printf("Error triggering command on node %s: %v\n", *nodeFlag, err)
|
log.Printf("Command '%s' triggered successfully on node %s\n", *actionFlag, *nodeFlag)
|
||||||
// return
|
}
|
||||||
// }
|
|
||||||
// fmt.Printf("Command '%s' triggered successfully on node %s\n", *actionFlag, *nodeFlag)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGetInfo(cmd *Command, args []string) {
|
func handleGetInfo(ctx context.Context, cmd *Command, args []string) {
|
||||||
// Implementation similar to handleTrigger, tailored for get_info
|
log := ctx.Value("logger").(*logger.Logger)
|
||||||
|
nodeFlag := cmd.FlagSet.String("node", "", "Specify the node address")
|
||||||
|
valueFlag := cmd.FlagSet.String("value", "", "Specify the value for get_info")
|
||||||
|
cmd.FlagSet.Parse(args)
|
||||||
|
|
||||||
|
if *nodeFlag == "" {
|
||||||
|
log.Println("Node address is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dbManager := ctx.Value("dbManager").(*backup.DBManager)
|
||||||
|
cli := NewInterfaceCLI(*nodeFlag, dbManager)
|
||||||
|
if err := cli.GetInfo(*valueFlag); err != nil {
|
||||||
|
log.Printf("Error getting info from node %s: %v\n", *nodeFlag, err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Successfully got info from node %s\n", *nodeFlag)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAddNode(cmd *Command, args []string) {
|
func handleAddNode(ctx context.Context, cmd *Command, args []string) {
|
||||||
|
dbManager := ctx.Value("dbManager").(*backup.DBManager)
|
||||||
nodeName := cmd.FlagSet.String("node", "", "Specify the node name")
|
nodeName := cmd.FlagSet.String("node", "", "Specify the node name")
|
||||||
cmd.FlagSet.Parse(args)
|
cmd.FlagSet.Parse(args)
|
||||||
|
|
||||||
|
@ -72,19 +84,34 @@ func handleAddNode(cmd *Command, args []string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := "some_generated_token" // Replace with actual token generation logic
|
token := "some_generated_token"
|
||||||
if err := dbManager.CreateToken(*nodeName, token); err != nil {
|
if err := dbManager.CreateToken(*nodeName, token); err != nil {
|
||||||
fmt.Printf("Error adding new node %s: %v\n", *nodeName, err)
|
fmt.Printf("Error adding new node %s: %v\n", *nodeName, err)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("New node %s added successfully with token %s\n", *nodeName, token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleGenCert(ctx context.Context, cmd *Command, args []string) {
|
||||||
|
log := ctx.Value("logger").(*logger.Logger)
|
||||||
|
dbManager := ctx.Value("dbManager").(*backup.DBManager)
|
||||||
|
nodeNameFlag := cmd.FlagSet.String("node", "", "Specify the node name for which to generate a certificate")
|
||||||
|
cmd.FlagSet.Parse(args)
|
||||||
|
|
||||||
|
if *nodeNameFlag == "" {
|
||||||
|
log.Println("Node name is required")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("New node %s added successfully with token %s\n", *nodeName, token)
|
|
||||||
|
cli := NewInterfaceCLI(*nodeNameFlag, dbManager)
|
||||||
|
if err := cli.GenCert(*nodeNameFlag); err != nil {
|
||||||
|
log.Printf("Error generating certificate for node %s: %v\n", *nodeNameFlag, err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Certificate generated successfully for node %s\n", *nodeNameFlag)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGenCert(cmd *Command, args []string) {
|
func NewCommand(name, description string, execute func(ctx context.Context, cmd *Command, args []string)) *Command {
|
||||||
// Implementation for generating a certificate for a node
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCommand(name, description string, execute func(cmd *Command, args []string)) *Command {
|
|
||||||
return &Command{
|
return &Command{
|
||||||
Name: name,
|
Name: name,
|
||||||
Description: description,
|
Description: description,
|
||||||
|
@ -103,7 +130,7 @@ func (c *Command) FindSubCommand(name string) (*Command, bool) {
|
||||||
return subCmd, found
|
return subCmd, found
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExecuteCommand(rootCmd *Command, args []string) error {
|
func ExecuteCommandWithContext(ctx context.Context, rootCmd *Command, args []string) error {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
fmt.Println("No command provided")
|
fmt.Println("No command provided")
|
||||||
return nil
|
return nil
|
||||||
|
@ -118,17 +145,11 @@ func ExecuteCommand(rootCmd *Command, args []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(subArgs) > 0 {
|
if cmd.Execute != nil {
|
||||||
subCmdName := subArgs[0]
|
cmd.Execute(ctx, cmd, subArgs)
|
||||||
subCmd, found := cmd.FindSubCommand(subCmdName)
|
} else {
|
||||||
if found {
|
fmt.Println("No execute function provided for command:", cmdName)
|
||||||
subCmd.FlagSet.Parse(subArgs[1:])
|
|
||||||
subCmd.Execute(subCmd, subCmd.FlagSet.Args())
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.FlagSet.Parse(subArgs)
|
|
||||||
cmd.Execute(cmd, cmd.FlagSet.Args())
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -185,3 +185,8 @@ func SaveConfigToFile(config *Config) error {
|
||||||
func ReloadConfig() {
|
func ReloadConfig() {
|
||||||
fmt.Println("Configuration reloaded")
|
fmt.Println("Configuration reloaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetConfigFilePath returns the path to the configuration file.
|
||||||
|
func GetConfigFilePath() string {
|
||||||
|
return configFilePath
|
||||||
|
}
|
||||||
|
|
4
go.mod
4
go.mod
|
@ -1,9 +1,10 @@
|
||||||
module pixelridgesoftworks.com/BackGo
|
module git.pixelridgesoftworks.com/BackGo
|
||||||
|
|
||||||
go 1.22.0
|
go 1.22.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/aws/aws-sdk-go-v2/config v1.27.9
|
github.com/aws/aws-sdk-go-v2/config v1.27.9
|
||||||
|
github.com/aws/aws-sdk-go-v2/credentials v1.17.9
|
||||||
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.13
|
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.13
|
||||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.0
|
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.0
|
||||||
github.com/go-sql-driver/mysql v1.8.1
|
github.com/go-sql-driver/mysql v1.8.1
|
||||||
|
@ -15,7 +16,6 @@ require (
|
||||||
filippo.io/edwards25519 v1.1.0 // indirect
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2 v1.26.0 // indirect
|
github.com/aws/aws-sdk-go-v2 v1.26.0 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.1 // indirect
|
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.1 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/credentials v1.17.9 // indirect
|
|
||||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0 // indirect
|
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.0 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.4 // indirect
|
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.4 // indirect
|
||||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.4 // indirect
|
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.4 // indirect
|
||||||
|
|
54
main.go
54
main.go
|
@ -3,13 +3,59 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"pixelridgesoftworks.com/BackGo/cmd"
|
"context"
|
||||||
|
"git.pixelridgesoftworks.com/BackGo/cmd"
|
||||||
|
backGoConfig "git.pixelridgesoftworks.com/BackGo/config"
|
||||||
|
"git.pixelridgesoftworks.com/BackGo/pkg/backup"
|
||||||
|
"git.pixelridgesoftworks.com/BackGo/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Correctly pass RootCmd as it's already a *Command
|
// Load configuration
|
||||||
if err := cmd.ExecuteCommand(cmd.RootCmd, os.Args[1:]); err != nil {
|
cfg := backGoConfig.LoadConfig()
|
||||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
||||||
|
// Initialize Logger
|
||||||
|
log := logger.NewLogger()
|
||||||
|
|
||||||
|
// Set the logger level based on the configuration
|
||||||
|
log.SetLevel(mapConfigLogLevelToLoggerLogLevel(cfg.LogLevel))
|
||||||
|
|
||||||
|
// Adjust logger output based on configuration or environment variables
|
||||||
|
logFileEnv := os.Getenv("LOG_FILE")
|
||||||
|
if logFileEnv != "" {
|
||||||
|
if err := log.LogToFile(logFileEnv); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Failed to set logger output to file: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the database manager
|
||||||
|
dbManager := backup.NewDBManager(cfg)
|
||||||
|
|
||||||
|
// Create a context
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Attach the logger and DB manager to the context
|
||||||
|
ctx = context.WithValue(ctx, "logger", log)
|
||||||
|
ctx = context.WithValue(ctx, "dbManager", dbManager)
|
||||||
|
|
||||||
|
// Now, pass only the context (and any other necessary parameters) to your command execution
|
||||||
|
// Note: You'll need to adjust your application's command handling to work with context
|
||||||
|
if err := cmd.ExecuteCommandWithContext(ctx, cmd.RootCmd, os.Args[1:]); err != nil {
|
||||||
|
log.Error(fmt.Sprintf("Command execution failed: %v", err))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mapConfigLogLevelToLoggerLogLevel(lvl backGoConfig.LogLevel) logger.LogLevel {
|
||||||
|
switch lvl {
|
||||||
|
case backGoConfig.LogLevelInfo:
|
||||||
|
return logger.LogLevelInfo
|
||||||
|
case backGoConfig.LogLevelWarning:
|
||||||
|
return logger.LogLevelWarning
|
||||||
|
case backGoConfig.LogLevelError:
|
||||||
|
return logger.LogLevelError
|
||||||
|
default:
|
||||||
|
return logger.LogLevelInfo // Default to Info if unsure
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"pixelridgesoftworks.com/BackGo/config"
|
"git.pixelridgesoftworks.com/BackGo/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DBManager struct {
|
type DBManager struct {
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
awsConfig "github.com/aws/aws-sdk-go-v2/config" // Aliased AWS config package
|
awsConfig "github.com/aws/aws-sdk-go-v2/config" // Aliased AWS config package
|
||||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
||||||
backGoConfig "pixelridgesoftworks.com/BackGo/config" // Aliased your config package
|
backGoConfig "git.pixelridgesoftworks.com/BackGo/config" // Aliased your config package
|
||||||
)
|
)
|
||||||
|
|
||||||
// S3Client wraps the AWS S3 client
|
// S3Client wraps the AWS S3 client
|
||||||
|
|
|
@ -6,18 +6,36 @@ import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Logger holds the log package's Logger instance.
|
// LogLevel type for defining different logging levels.
|
||||||
|
type LogLevel int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Define different log levels.
|
||||||
|
LogLevelInfo LogLevel = iota
|
||||||
|
LogLevelWarning
|
||||||
|
LogLevelError
|
||||||
|
)
|
||||||
|
|
||||||
|
// Logger wraps the standard log.Logger from the Go standard library.
|
||||||
|
// It adds additional functionality like setting log levels.
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
*log.Logger
|
*log.Logger
|
||||||
|
level LogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLogger creates a new Logger instance. By default, it logs to stdout.
|
// NewLogger creates a new Logger instance. By default, it logs to stdout with LogLevelInfo.
|
||||||
func NewLogger() *Logger {
|
func NewLogger() *Logger {
|
||||||
return &Logger{
|
return &Logger{
|
||||||
Logger: log.New(os.Stdout, "", log.LstdFlags),
|
Logger: log.New(os.Stdout, "", log.LstdFlags),
|
||||||
|
level: LogLevelInfo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetLevel changes the logging level of the Logger.
|
||||||
|
func (l *Logger) SetLevel(level LogLevel) {
|
||||||
|
l.level = level
|
||||||
|
}
|
||||||
|
|
||||||
// LogToFile configures the logger to log to a specified file.
|
// LogToFile configures the logger to log to a specified file.
|
||||||
func (l *Logger) LogToFile(filePath string) error {
|
func (l *Logger) LogToFile(filePath string) error {
|
||||||
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||||
|
@ -43,3 +61,24 @@ func (l *Logger) LogToBoth(filePath string) error {
|
||||||
l.SetOutput(multi)
|
l.SetOutput(multi)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Info logs a message at level Info, respecting the set log level.
|
||||||
|
func (l *Logger) Info(msg string) {
|
||||||
|
if l.level <= LogLevelInfo {
|
||||||
|
l.Println("INFO: " + msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning logs a message at level Warning, respecting the set log level.
|
||||||
|
func (l *Logger) Warning(msg string) {
|
||||||
|
if l.level <= LogLevelWarning {
|
||||||
|
l.Println("WARNING: " + msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error logs a message at level Error, respecting the set log level.
|
||||||
|
func (l *Logger) Error(msg string) {
|
||||||
|
if l.level <= LogLevelError {
|
||||||
|
l.Println("ERROR: " + msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user