Skip to content

Commit

Permalink
cli: log batches of eval deletes, don't continue when cancelled.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Jun 29, 2022
1 parent 4322260 commit 3ec5232
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions command/eval_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,8 @@ func (e *EvalDeleteCommand) Run(args []string) int {
// Depending on whether we deleted evaluations or not, output a message so
// this is clear.
if e.numDeleted > 0 {
grammar := "evaluation"
if e.numDeleted > 1 {
grammar = grammar + "s"
}
e.Ui.Output(fmt.Sprintf("Successfully deleted %v %s", e.numDeleted, grammar))
e.Ui.Output(fmt.Sprintf("Successfully deleted %v %s",
e.numDeleted, correctGrammar("evaluation", e.numDeleted)))
} else if err == nil {
e.Ui.Output("No evaluations were deleted")
}
Expand Down Expand Up @@ -199,7 +196,11 @@ func (e *EvalDeleteCommand) handleEvalArgDelete(evalID string) (int, error) {
if err != nil {
return 1, err
}
return e.batchDelete([]*api.Evaluation{evalInfo})

// Supplying an eval to delete by its ID will always skip verification, so
// we don't need to understand the boolean response.
code, _, err := e.batchDelete([]*api.Evaluation{evalInfo})
return code, err
}

// handleFlagFilterDelete handles deletion of evaluations discovered using
Expand Down Expand Up @@ -251,21 +252,28 @@ func (e *EvalDeleteCommand) handleFlagFilterDelete(nextToken, filter string) (in
}
}

numEvalsToDelete := len(evalsToDelete)

// The filter flags are operator controlled, therefore ensure we actually
// found some evals to delete. Otherwise, inform the operator their flags
// are potentially incorrect.
if len(evalsToDelete) == 0 {
if numEvalsToDelete == 0 {
if e.numDeleted > 0 {
return 0, nil
} else {
return 1, errors.New("failed to find any evals that matched filter criteria")
}
}

if code, err := e.batchDelete(evalsToDelete); err != nil {
if code, actioned, err := e.batchDelete(evalsToDelete); err != nil {
return code, err
} else if !actioned {
return code, nil
}

e.Ui.Info(fmt.Sprintf("Successfully deleted batch of %v %s",
numEvalsToDelete, correctGrammar("evaluation", numEvalsToDelete)))

// If there is another page of evaluations matching the filter, call this
// function again and delete the next batch of evals. We pause for a 500ms
// rather than just run as fast as the code and machine possibly can. This
Expand All @@ -282,7 +290,7 @@ func (e *EvalDeleteCommand) handleFlagFilterDelete(nextToken, filter string) (in
// any confirmation questions along the way. It will ask whether the operator
// want to list the evals before deletion, and optionally ask for confirmation
// before deleting based on input criteria.
func (e *EvalDeleteCommand) batchDelete(evals []*api.Evaluation) (int, error) {
func (e *EvalDeleteCommand) batchDelete(evals []*api.Evaluation) (int, bool, error) {

// Ask whether the operator wants to see the list of evaluations before
// moving forward with deletion. This will only happen if filters are used
Expand Down Expand Up @@ -318,21 +326,21 @@ func (e *EvalDeleteCommand) batchDelete(evals []*api.Evaluation) (int, error) {
e.Ui.Output("")

if !deleteEvals {
return code, nil
return code, deleteEvals, nil
}
}

_, err := e.client.Evaluations().Delete(ids, nil)
if err != nil {
return 1, err
return 1, false, err
}

// Calculate how many total evaluations we have deleted, so we can output
// this at the end of the process.
curDeleted := e.numDeleted
e.numDeleted = curDeleted + len(ids)

return 0, nil
return 0, true, nil
}

// askQuestion allows the command to ask the operator a question requiring a
Expand Down Expand Up @@ -360,3 +368,10 @@ func (e *EvalDeleteCommand) askQuestion(question, noResp string) (int, bool) {
}
return 0, true
}

func correctGrammar(word string, num int) string {
if num > 1 {
return word + "s"
}
return word
}

0 comments on commit 3ec5232

Please sign in to comment.