Ability to add prober from CLI #2

This commit is contained in:
Cristian Ditaputratama 2024-05-18 19:03:56 +07:00
parent 7b6cfee31d
commit d2f927e3db
Signed by: ditatompel
GPG key ID: 31D3D06D77950979
2 changed files with 28 additions and 7 deletions

View file

@ -73,8 +73,28 @@ xmr-nodes probers list -s last_submit_ts -d asc sin1`,
var addProbersCmd = &cobra.Command{ var addProbersCmd = &cobra.Command{
Use: "add [name]", Use: "add [name]",
Short: "Add new prober", Short: "Add new prober",
Long: `Add new prober machine.`, 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(cmd *cobra.Command, args []string) {
fmt.Println("TODO: Add new prober") if err := database.ConnectDB(); err != nil {
panic(err)
}
proberName := ""
if len(args) > 0 {
proberName = strings.Join(args, " ")
} else {
proberName = stringPrompt("Prober Name:")
}
proberRepo := repo.NewProberRepo(database.GetDB())
prober, err := proberRepo.Add(proberName)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Name: %s\nAPI Key: %s\n", prober.Name, prober.ApiKey)
}, },
} }

View file

@ -10,7 +10,7 @@ import (
) )
type ProberRepository interface { type ProberRepository interface {
AddProber(name string) error Add(name string) (Prober, error)
Update(id int, name string) error Update(id int, name string) error
Probers(q ProbersQueryParams) ([]Prober, error) Probers(q ProbersQueryParams) ([]Prober, error)
CheckApi(key string) (Prober, error) CheckApi(key string) (Prober, error)
@ -32,13 +32,14 @@ func NewProberRepo(db *database.DB) ProberRepository {
return &ProberRepo{db} return &ProberRepo{db}
} }
func (repo *ProberRepo) AddProber(name string) error { func (repo *ProberRepo) Add(name string) (Prober, error) {
apiKey := uuid.New()
query := `INSERT INTO tbl_prober (name, api_key, last_submit_ts) VALUES (?, ?, ?)` query := `INSERT INTO tbl_prober (name, api_key, last_submit_ts) VALUES (?, ?, ?)`
_, err := repo.db.Exec(query, name, uuid.New(), 0) _, err := repo.db.Exec(query, name, apiKey, 0)
if err != nil { if err != nil {
return err return Prober{}, err
} }
return nil return Prober{Name: name, ApiKey: apiKey}, nil
} }
func (repo *ProberRepo) Update(id int, name string) error { func (repo *ProberRepo) Update(id int, name string) error {