Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deleting all measurements #158

Merged
merged 4 commits into from
Nov 13, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions internal/cli/rm/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,46 @@ import (
db "upper.io/db.v3"
)

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(ctx.DB, result.Result.ID)
if err == db.ErrNoMoreRows {
log.WithError(err).Errorf("failed to delete resul #%d", result.Result.ID)
bassosimone marked this conversation as resolved.
Show resolved Hide resolved
}
cnt += 1
}
for _, result := range doneResults {
err = database.DeleteResult(ctx.DB, result.Result.ID)
if err == db.ErrNoMoreRows {
log.WithError(err).Errorf("failed to delete resul #%d", result.Result.ID)
bassosimone marked this conversation as resolved.
Show resolved Hide resolved
}
cnt += 1
}
log.Infof("Deleted #%d measurements", cnt)
}

func init() {
cmd := root.Command("rm", "Delete a result")
yes := cmd.Flag("yes", "Skip interactive prompt").Bool()
yes := cmd.Flag("all", "Delete all measurements").Bool()
bassosimone marked this conversation as resolved.
Show resolved Hide resolved

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

Expand All @@ -25,6 +62,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