Skip to content

Commit 5f2bcdf

Browse files
committed
feat: Added node delete command
1 parent ef953b3 commit 5f2bcdf

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

cmd/server/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ func init() {
1212
probersCmd.AddCommand(deleteProbersCmd)
1313
listProbersCmd.Flags().StringP("sort-by", "s", "last_submit_ts", "Sort by column name, can be id or last_submit_ts")
1414
listProbersCmd.Flags().StringP("sort-dir", "d", "desc", "Sort direction, can be asc or desc")
15+
cmd.Root.AddCommand(nodeCmd)
16+
nodeCmd.AddCommand(deleteNodeCmd)
1517
}

cmd/server/node.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package server
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
"os"
7+
"strconv"
8+
9+
"github.com/ditatompel/xmr-remote-nodes/internal/database"
10+
"github.com/ditatompel/xmr-remote-nodes/internal/monero"
11+
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var nodeCmd = &cobra.Command{
16+
Use: "node",
17+
Short: "[Server] Administer monitored nodes",
18+
Long: `Command to administer monitored nodes.
19+
20+
This command should only be run on the server which directly connect to the MySQL database.
21+
`,
22+
Run: func(cmd *cobra.Command, _ []string) {
23+
if err := cmd.Help(); err != nil {
24+
slog.Error(err.Error())
25+
os.Exit(1)
26+
}
27+
},
28+
}
29+
30+
var deleteNodeCmd = &cobra.Command{
31+
Use: "delete",
32+
Short: "Delete node",
33+
Long: `Delete node identified by ID.
34+
35+
This command delete node and it's associated probe logs (if exists).
36+
37+
To find out the node ID, visit frontend UI or from "/api/v1/nodes" endpoint.
38+
`,
39+
Run: func(_ *cobra.Command, _ []string) {
40+
if err := database.ConnectDB(); err != nil {
41+
fmt.Println(err)
42+
return
43+
}
44+
nodeID, err := strconv.Atoi(stringPrompt("Node ID:"))
45+
if err != nil {
46+
fmt.Println("Invalid ID:", err)
47+
return
48+
}
49+
50+
moneroRepo := monero.New()
51+
err = moneroRepo.Delete(uint(nodeID))
52+
if err != nil {
53+
fmt.Println("Failed to delete node:", err)
54+
return
55+
}
56+
57+
fmt.Printf("Node ID %d deleted\n", nodeID)
58+
},
59+
}

0 commit comments

Comments
 (0)