Add rm command to delete results

This commit is contained in:
Arturo Filastò 2018-09-18 09:54:27 +02:00
parent e6a67ca5aa
commit 47a2fbb88c
4 changed files with 61 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
_ "github.com/ooni/probe-cli/internal/cli/list"
_ "github.com/ooni/probe-cli/internal/cli/onboard"
_ "github.com/ooni/probe-cli/internal/cli/reset"
_ "github.com/ooni/probe-cli/internal/cli/rm"
_ "github.com/ooni/probe-cli/internal/cli/run"
_ "github.com/ooni/probe-cli/internal/cli/show"
_ "github.com/ooni/probe-cli/internal/cli/upload"

51
internal/cli/rm/rm.go Normal file
View File

@ -0,0 +1,51 @@
package rm
import (
"errors"
"fmt"
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/database"
survey "gopkg.in/AlecAivazis/survey.v1"
db "upper.io/db.v3"
)
func init() {
cmd := root.Command("rm", "Delete a result")
yes := cmd.Flag("yes", "Skip interactive prompt").Bool()
resultID := cmd.Arg("id", "the id of the result to delete").Int64()
cmd.Action(func(_ *kingpin.ParseContext) error {
ctx, err := root.Init()
if err != nil {
log.Errorf("%s", err)
return err
}
if *yes == true {
err = database.DeleteResult(ctx.DB, *resultID)
if err == db.ErrNoMoreRows {
return errors.New("result not found")
}
return err
}
answer := ""
confirm := &survey.Select{
Message: fmt.Sprintf("Are you sure you wish to delete the result #%d", *resultID),
Options: []string{"true", "false"},
Default: "false",
}
survey.AskOne(confirm, &answer, nil)
if answer == "false" {
return errors.New("canceled by user")
}
err = database.DeleteResult(ctx.DB, *resultID)
if err == db.ErrNoMoreRows {
return errors.New("result not found")
}
return err
})
}

View File

@ -122,6 +122,9 @@ func DeleteResult(sess sqlbuilder.Database, resultID int64) error {
var result Result
res := sess.Collection("results").Find("result_id", resultID)
if err := res.One(&result); err != nil {
if err == db.ErrNoMoreRows {
return err
}
log.WithError(err).Error("error in obtaining the result")
return err
}

View File

@ -8,6 +8,7 @@ import (
"testing"
"github.com/ooni/probe-cli/utils"
db "upper.io/db.v3"
)
func TestMeasurementWorkflow(t *testing.T) {
@ -155,6 +156,11 @@ func TestDeleteResult(t *testing.T) {
if totalMeasurements != 0 {
t.Fatal("measurements should be zero")
}
err = DeleteResult(sess, 20)
if err != db.ErrNoMoreRows {
t.Fatal(err)
}
}
func TestNetworkCreate(t *testing.T) {