2018-09-18 09:54:27 +02:00
|
|
|
package rm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-01-19 17:58:37 +01:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2018-09-18 09:54:27 +02:00
|
|
|
"github.com/alecthomas/kingpin"
|
|
|
|
"github.com/apex/log"
|
2021-02-02 10:32:46 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root"
|
2022-11-15 10:35:30 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/database"
|
2022-05-06 13:05:24 +02:00
|
|
|
"github.com/upper/db/v4"
|
2018-09-18 09:54:27 +02:00
|
|
|
)
|
|
|
|
|
2022-05-06 13:05:24 +02:00
|
|
|
func deleteAll(sess db.Session, skipInteractive bool) error {
|
2020-11-13 07:43:00 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-18 09:54:27 +02:00
|
|
|
func init() {
|
|
|
|
cmd := root.Command("rm", "Delete a result")
|
|
|
|
yes := cmd.Flag("yes", "Skip interactive prompt").Bool()
|
2020-11-13 07:43:00 +01:00
|
|
|
all := cmd.Flag("all", "Delete all measurements").Bool()
|
2018-09-18 09:54:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-13 07:43:00 +01:00
|
|
|
if *all == true {
|
2020-11-13 20:07:30 +01:00
|
|
|
return deleteAll(ctx.DB(), *yes)
|
2020-11-13 07:43:00 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 09:54:27 +02:00
|
|
|
if *yes == true {
|
2020-11-13 20:07:30 +01:00
|
|
|
err = database.DeleteResult(ctx.DB(), *resultID)
|
2018-09-18 09:54:27 +02:00
|
|
|
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")
|
|
|
|
}
|
2020-11-13 20:07:30 +01:00
|
|
|
err = database.DeleteResult(ctx.DB(), *resultID)
|
2018-09-18 09:54:27 +02:00
|
|
|
if err == db.ErrNoMoreRows {
|
|
|
|
return errors.New("result not found")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|