Skip to content

Commit

Permalink
Add support for deleting all measurements (#158)
Browse files Browse the repository at this point in the history
Co-authored-by: Simone Basso <bassosimone@gmail.com>
  • Loading branch information
hellais and bassosimone authored Nov 13, 2020
1 parent c4da81e commit c0a95c0
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions internal/cli/rm/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,50 @@ import (
"github.com/ooni/probe-cli/internal/database"
survey "gopkg.in/AlecAivazis/survey.v1"
db "upper.io/db.v3"
"upper.io/db.v3/lib/sqlbuilder"
)

func deleteAll(sess sqlbuilder.Database, skipInteractive bool) error {
if skipInteractive == false {
answer := ""
confirm := &survey.Select{
Message: fmt.Sprintf("Are you sure you wish to delete ALL results"),
Options: []string{"true", "false"},
Default: "false",
}
survey.AskOne(confirm, &answer, nil)
if answer == "false" {
return errors.New("canceled by user")
}
}
doneResults, incompleteResults, err := database.ListResults(sess)
if err != nil {
log.WithError(err).Error("failed to list results")
return err
}
cnt := 0
for _, result := range incompleteResults {
err = database.DeleteResult(sess, result.Result.ID)
if err == db.ErrNoMoreRows {
log.WithError(err).Errorf("failed to delete result #%d", result.Result.ID)
}
cnt++
}
for _, result := range doneResults {
err = database.DeleteResult(sess, result.Result.ID)
if err == db.ErrNoMoreRows {
log.WithError(err).Errorf("failed to delete result #%d", result.Result.ID)
}
cnt++
}
log.Infof("Deleted #%d measurements", cnt)
return nil
}

func init() {
cmd := root.Command("rm", "Delete a result")
yes := cmd.Flag("yes", "Skip interactive prompt").Bool()
all := cmd.Flag("all", "Delete all measurements").Bool()

resultID := cmd.Arg("id", "the id of the result to delete").Int64()

Expand All @@ -25,6 +64,10 @@ func init() {
return err
}

if *all == true {
return deleteAll(ctx.DB, *yes)
}

if *yes == true {
err = database.DeleteResult(ctx.DB, *resultID)
if err == db.ErrNoMoreRows {
Expand Down

0 comments on commit c0a95c0

Please sign in to comment.