135 lines
4.1 KiB
Go
135 lines
4.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"pixelridgesoftworks.com/BackGo/pkg/backup"
|
|
"pixelridgesoftworks.com/BackGo/config"
|
|
)
|
|
|
|
type Command struct {
|
|
Name string
|
|
Description string
|
|
FlagSet *flag.FlagSet
|
|
SubCommands map[string]*Command
|
|
Execute func(cmd *Command, args []string)
|
|
}
|
|
|
|
var appConfig = config.LoadConfig()
|
|
var dbManager *backup.DBManager
|
|
|
|
var RootCmd = NewCommand("backup", "Main entry point for backup utility", nil)
|
|
|
|
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)
|
|
|
|
triggerCmd := NewCommand("trigger", "Trigger an action on a node", handleTrigger)
|
|
getInfoCmd := NewCommand("getinfo", "Get information from a node", handleGetInfo)
|
|
addNodeCmd := NewCommand("addnode", "Add a new node to the cluster", handleAddNode)
|
|
genCertCmd := NewCommand("gencert", "Generate a certificate for a node", handleGenCert)
|
|
|
|
remoteCmd.AddSubCommand(triggerCmd)
|
|
remoteCmd.AddSubCommand(getInfoCmd)
|
|
remoteCmd.AddSubCommand(addNodeCmd)
|
|
remoteCmd.AddSubCommand(genCertCmd)
|
|
|
|
RootCmd.AddSubCommand(remoteCmd)
|
|
}
|
|
|
|
func handleTrigger(cmd *Command, args []string) {
|
|
nodeFlag := cmd.FlagSet.String("node", "", "Specify the node hostname")
|
|
actionFlag := cmd.FlagSet.String("action", "", "Specify the action to trigger")
|
|
cmd.FlagSet.Parse(args)
|
|
|
|
if *nodeFlag == "" || *actionFlag == "" {
|
|
fmt.Println("Both node hostname and action are required")
|
|
return
|
|
}
|
|
|
|
// Since backup.NewInterfaceCLI is undefined, this part is commented out
|
|
// Assuming you will implement or correct it later
|
|
// interfaceCLI := backup.NewInterfaceCLI(*nodeFlag, dbManager)
|
|
// if err := interfaceCLI.TriggerCommand(*actionFlag, ""); err != nil {
|
|
// fmt.Printf("Error triggering command on node %s: %v\n", *nodeFlag, err)
|
|
// return
|
|
// }
|
|
// fmt.Printf("Command '%s' triggered successfully on node %s\n", *actionFlag, *nodeFlag)
|
|
}
|
|
|
|
func handleGetInfo(cmd *Command, args []string) {
|
|
// Implementation similar to handleTrigger, tailored for get_info
|
|
}
|
|
|
|
func handleAddNode(cmd *Command, args []string) {
|
|
nodeName := cmd.FlagSet.String("node", "", "Specify the node name")
|
|
cmd.FlagSet.Parse(args)
|
|
|
|
if *nodeName == "" {
|
|
fmt.Println("Node name is required")
|
|
return
|
|
}
|
|
|
|
token := "some_generated_token" // Replace with actual token generation logic
|
|
if err := dbManager.CreateToken(*nodeName, token); err != nil {
|
|
fmt.Printf("Error adding new node %s: %v\n", *nodeName, err)
|
|
return
|
|
}
|
|
fmt.Printf("New node %s added successfully with token %s\n", *nodeName, token)
|
|
}
|
|
|
|
func handleGenCert(cmd *Command, args []string) {
|
|
// Implementation for generating a certificate for a node
|
|
}
|
|
|
|
func NewCommand(name, description string, execute func(cmd *Command, args []string)) *Command {
|
|
return &Command{
|
|
Name: name,
|
|
Description: description,
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
|
SubCommands: make(map[string]*Command),
|
|
Execute: execute,
|
|
}
|
|
}
|
|
|
|
func (c *Command) AddSubCommand(subCmd *Command) {
|
|
c.SubCommands[subCmd.Name] = subCmd
|
|
}
|
|
|
|
func (c *Command) FindSubCommand(name string) (*Command, bool) {
|
|
subCmd, found := c.SubCommands[name]
|
|
return subCmd, found
|
|
}
|
|
|
|
func ExecuteCommand(rootCmd *Command, args []string) error {
|
|
if len(args) < 1 {
|
|
fmt.Println("No command provided")
|
|
return nil
|
|
}
|
|
|
|
cmdName := args[0]
|
|
subArgs := args[1:]
|
|
|
|
cmd, found := rootCmd.FindSubCommand(cmdName)
|
|
if !found {
|
|
fmt.Printf("Unknown command: %s\n", cmdName)
|
|
return nil
|
|
}
|
|
|
|
if len(subArgs) > 0 {
|
|
subCmdName := subArgs[0]
|
|
subCmd, found := cmd.FindSubCommand(subCmdName)
|
|
if found {
|
|
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
|
|
}
|