mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2024-11-16 17:07:36 +00:00
Ability to delete prober by ID from CLI #2
This commit is contained in:
parent
d2f927e3db
commit
b5e2787420
3 changed files with 42 additions and 3 deletions
|
@ -44,6 +44,7 @@ func init() {
|
|||
cmd.Root.AddCommand(probersCmd)
|
||||
probersCmd.AddCommand(listProbersCmd)
|
||||
probersCmd.AddCommand(addProbersCmd)
|
||||
probersCmd.AddCommand(deleteProbersCmd)
|
||||
listProbersCmd.Flags().StringP("sort-by", "s", "last_submit_ts", "Sort by column name, can be id or last_submit_ts")
|
||||
listProbersCmd.Flags().StringP("sort-dir", "d", "desc", "Sort direction, can be asc or desc")
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package server
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
@ -76,9 +77,10 @@ var addProbersCmd = &cobra.Command{
|
|||
Long: `Create new prober identified by [name] (if provided) along with an API key.
|
||||
|
||||
This command will display the prober name and API key when successfully executed.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
Run: func(_ *cobra.Command, args []string) {
|
||||
if err := database.ConnectDB(); err != nil {
|
||||
panic(err)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
proberName := ""
|
||||
|
@ -98,3 +100,29 @@ This command will display the prober name and API key when successfully executed
|
|||
fmt.Printf("Name: %s\nAPI Key: %s\n", prober.Name, prober.ApiKey)
|
||||
},
|
||||
}
|
||||
|
||||
var deleteProbersCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "Delete prober",
|
||||
Long: `Delete prober identified by id.`,
|
||||
Run: func(_ *cobra.Command, _ []string) {
|
||||
if err := database.ConnectDB(); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
proberId, err := strconv.Atoi(stringPrompt("Prober ID:"))
|
||||
if err != nil {
|
||||
fmt.Println("Invalid ID:", err)
|
||||
return
|
||||
}
|
||||
|
||||
proberRepo := repo.NewProberRepo(database.GetDB())
|
||||
err = proberRepo.Delete(proberId)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to delete prober:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Prober ID %d deleted\n", proberId)
|
||||
},
|
||||
}
|
||||
|
|
|
@ -50,7 +50,17 @@ func (repo *ProberRepo) Update(id int, name string) error {
|
|||
|
||||
func (repo *ProberRepo) Delete(id int) error {
|
||||
query := `DELETE FROM tbl_prober WHERE id = ?`
|
||||
_, err := repo.db.Exec(query, id)
|
||||
res, err := repo.db.Exec(query, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
row, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if row == 0 {
|
||||
return fmt.Errorf("no rows affected")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue