From 3ec52322bb6a5974b2185ed5290da230ed6f4e2c Mon Sep 17 00:00:00 2001 From: James Rasell Date: Wed, 29 Jun 2022 15:33:38 +0100 Subject: [PATCH] cli: log batches of eval deletes, don't continue when cancelled. --- command/eval_delete.go | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/command/eval_delete.go b/command/eval_delete.go index 027e94e9511e..2d15fae6d2fd 100644 --- a/command/eval_delete.go +++ b/command/eval_delete.go @@ -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") } @@ -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 @@ -251,10 +252,12 @@ 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 { @@ -262,10 +265,15 @@ func (e *EvalDeleteCommand) handleFlagFilterDelete(nextToken, filter string) (in } } - 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 @@ -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 @@ -318,13 +326,13 @@ 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 @@ -332,7 +340,7 @@ func (e *EvalDeleteCommand) batchDelete(evals []*api.Evaluation) (int, error) { 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 @@ -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 +}