From 47a2fbb88c5f1b0498cd82b1223029a00481bc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Tue, 18 Sep 2018 09:54:27 +0200 Subject: [PATCH] Add rm command to delete results --- cmd/ooni/main.go | 1 + internal/cli/rm/rm.go | 51 +++++++++++++++++++++++++++++++ internal/database/actions.go | 3 ++ internal/database/actions_test.go | 6 ++++ 4 files changed, 61 insertions(+) create mode 100644 internal/cli/rm/rm.go diff --git a/cmd/ooni/main.go b/cmd/ooni/main.go index 017382b..5b6a6ce 100644 --- a/cmd/ooni/main.go +++ b/cmd/ooni/main.go @@ -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" diff --git a/internal/cli/rm/rm.go b/internal/cli/rm/rm.go new file mode 100644 index 0000000..284a2a9 --- /dev/null +++ b/internal/cli/rm/rm.go @@ -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 + }) +} diff --git a/internal/database/actions.go b/internal/database/actions.go index 27583f7..205fed1 100644 --- a/internal/database/actions.go +++ b/internal/database/actions.go @@ -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 } diff --git a/internal/database/actions_test.go b/internal/database/actions_test.go index 6068ddf..5f03f69 100644 --- a/internal/database/actions_test.go +++ b/internal/database/actions_test.go @@ -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) {