Ability to delete prober by ID from CLI #2

This commit is contained in:
ditatompel 2024-05-18 19:39:27 +07:00
parent d2f927e3db
commit b5e2787420
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
3 changed files with 42 additions and 3 deletions

View file

@ -44,6 +44,7 @@ func init() {
cmd.Root.AddCommand(probersCmd) cmd.Root.AddCommand(probersCmd)
probersCmd.AddCommand(listProbersCmd) probersCmd.AddCommand(listProbersCmd)
probersCmd.AddCommand(addProbersCmd) 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-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") listProbersCmd.Flags().StringP("sort-dir", "d", "desc", "Sort direction, can be asc or desc")
} }

View file

@ -3,6 +3,7 @@ package server
import ( import (
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"time" "time"
@ -76,9 +77,10 @@ var addProbersCmd = &cobra.Command{
Long: `Create new prober identified by [name] (if provided) along with an API key. 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.`, 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 { if err := database.ConnectDB(); err != nil {
panic(err) fmt.Println(err)
return
} }
proberName := "" 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) 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)
},
}

View file

@ -50,7 +50,17 @@ func (repo *ProberRepo) Update(id int, name string) error {
func (repo *ProberRepo) Delete(id int) error { func (repo *ProberRepo) Delete(id int) error {
query := `DELETE FROM tbl_prober WHERE id = ?` 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 return err
} }