From d2f927e3dbd501d8f916052bb6b9757ca1b46d3d Mon Sep 17 00:00:00 2001 From: ditatompel Date: Sat, 18 May 2024 19:03:56 +0700 Subject: [PATCH] Ability to add prober from CLI #2 --- cmd/server/probers.go | 24 ++++++++++++++++++++++-- internal/repo/prober.go | 11 ++++++----- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/cmd/server/probers.go b/cmd/server/probers.go index 5df4d61..35fe16a 100644 --- a/cmd/server/probers.go +++ b/cmd/server/probers.go @@ -73,8 +73,28 @@ xmr-nodes probers list -s last_submit_ts -d asc sin1`, var addProbersCmd = &cobra.Command{ Use: "add [name]", 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) { - 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) }, } diff --git a/internal/repo/prober.go b/internal/repo/prober.go index 1e80427..65992d5 100644 --- a/internal/repo/prober.go +++ b/internal/repo/prober.go @@ -10,7 +10,7 @@ import ( ) type ProberRepository interface { - AddProber(name string) error + Add(name string) (Prober, error) Update(id int, name string) error Probers(q ProbersQueryParams) ([]Prober, error) CheckApi(key string) (Prober, error) @@ -32,13 +32,14 @@ func NewProberRepo(db *database.DB) ProberRepository { 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 (?, ?, ?)` - _, err := repo.db.Exec(query, name, uuid.New(), 0) + _, err := repo.db.Exec(query, name, apiKey, 0) 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 {