2024-03-28 00:48:22 -06:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-03-28 15:45:20 -06:00
|
|
|
"git.pixelridgesoftworks.com/BackGo/pkg/backup"
|
2024-03-28 00:48:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type InterfaceCLI struct {
|
|
|
|
NodeAddress string
|
2024-03-28 15:45:20 -06:00
|
|
|
DBManager *backup.DBManager
|
2024-03-28 00:48:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewInterfaceCLI(nodeAddress string, dbManager *backup.DBManager) *InterfaceCLI {
|
2024-03-28 15:45:20 -06:00
|
|
|
if dbManager == nil {
|
|
|
|
panic("DBManager cannot be nil")
|
|
|
|
}
|
2024-03-28 00:48:22 -06:00
|
|
|
return &InterfaceCLI{
|
|
|
|
NodeAddress: nodeAddress,
|
|
|
|
DBManager: dbManager,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *InterfaceCLI) TriggerCommand(action, value string) error {
|
2024-03-28 15:45:20 -06:00
|
|
|
return cli.sendCommandToNode("trigger", value)
|
2024-03-28 00:48:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *InterfaceCLI) GetInfo(value string) error {
|
2024-03-28 15:45:20 -06:00
|
|
|
return cli.sendCommandToNode("get_info", value)
|
2024-03-28 00:48:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *InterfaceCLI) AddNode(hostname, token string) error {
|
2024-03-28 15:45:20 -06:00
|
|
|
expectedIP := "" // Implement IP determination logic here
|
2024-03-28 00:48:22 -06:00
|
|
|
if !cli.DBManager.VerifyHostname(hostname, expectedIP) {
|
|
|
|
return fmt.Errorf("hostname %s does not resolve to the expected IP %s", hostname, expectedIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cli.DBManager.CreateToken(hostname, token); err != nil {
|
|
|
|
return fmt.Errorf("failed to create token for hostname %s: %v", hostname, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Node %s added successfully with token %s\n", hostname, token)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *InterfaceCLI) GenCert(nodeName string) error {
|
2024-03-28 15:45:20 -06:00
|
|
|
return cli.sendCommandToNode("gencert", nodeName)
|
2024-03-28 00:48:22 -06:00
|
|
|
}
|
|
|
|
|
2024-03-28 15:45:20 -06:00
|
|
|
func (cli *InterfaceCLI) sendCommandToNode(action, value string) error {
|
2024-03-28 00:48:22 -06:00
|
|
|
payload := backup.CommandPayload{
|
|
|
|
Action: action,
|
2024-03-28 15:45:20 -06:00
|
|
|
Node: cli.NodeAddress,
|
2024-03-28 00:48:22 -06:00
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
payloadBytes, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error marshaling command payload: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Post(fmt.Sprintf("http://%s/api/command", cli.NodeAddress), "application/json", bytes.NewReader(payloadBytes))
|
|
|
|
if err != nil {
|
2024-03-28 15:45:20 -06:00
|
|
|
return fmt.Errorf("error sending command to node %s: %v", cli.NodeAddress, err)
|
2024-03-28 00:48:22 -06:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2024-03-28 15:45:20 -06:00
|
|
|
fmt.Printf("Command '%s' sent to node %s successfully\n", action, cli.NodeAddress)
|
2024-03-28 00:48:22 -06:00
|
|
|
return nil
|
|
|
|
}
|