8010e9783a
* Upgrade to github.com/upper/db/v4 * fix(oonitest): repair imports after merge Oops, okay, it seems the merge did not preserve all the import changes, so let's ensure we use the right imports here! * cleanup(go.mod): don't refer to upper.io/db/v3 These lines didn't disappear previously because the merge commit failed to remove all references to upper.io/db/v3. Co-authored-by: stergem <sgemelas@protonmail.com> Co-authored-by: Simone Basso <bassosimone@gmail.com>
94 lines
2.3 KiB
Go
94 lines
2.3 KiB
Go
package rm
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
"github.com/alecthomas/kingpin"
|
|
"github.com/apex/log"
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root"
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
|
|
"github.com/upper/db/v4"
|
|
)
|
|
|
|
func deleteAll(sess db.Session, 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()
|
|
|
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
|
ctx, err := root.Init()
|
|
if err != nil {
|
|
log.Errorf("%s", err)
|
|
return err
|
|
}
|
|
|
|
if *all == true {
|
|
return deleteAll(ctx.DB(), *yes)
|
|
}
|
|
|
|
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
|
|
})
|
|
}
|