package cmd import ( "bytes" "encoding/json" "fmt" "net/http" "pixelridgesoftworks.com/BackGo/pkg/backup" ) // InterfaceCLI manages commands related to node interface operations type InterfaceCLI struct { NodeAddress string DBManager *backup.DBManager // Assuming DBManager is part of the backup package } // NewInterfaceCLI creates a new InterfaceCLI instance func NewInterfaceCLI(nodeAddress string, dbManager *backup.DBManager) *InterfaceCLI { return &InterfaceCLI{ NodeAddress: nodeAddress, DBManager: dbManager, } } // TriggerCommand sends a trigger command to a node func (cli *InterfaceCLI) TriggerCommand(action, value string) error { return cli.sendCommandToNode("trigger", action, value) } // GetInfo sends a get_info command to a node func (cli *InterfaceCLI) GetInfo(value string) error { return cli.sendCommandToNode("get_info", cli.NodeAddress, value) } // AddNode handles adding a new node on the master node func (cli *InterfaceCLI) AddNode(hostname, token string) error { // Verify the hostname resolves to an expected IP before adding // 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) { 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 { 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 } // GenCert generates a certificate for a node func (cli *InterfaceCLI) GenCert(nodeName string) error { return cli.sendCommandToNode("gencert", nodeName, "") } // sendCommandToNode sends a command to the specified node func (cli *InterfaceCLI) sendCommandToNode(action, node, value string) error { payload := backup.CommandPayload{ Action: action, Node: node, 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 { return fmt.Errorf("error sending command to node %s: %v", node, err) } defer resp.Body.Close() fmt.Printf("Command '%s' sent to node %s successfully\n", action, node) return nil }