Skip to content

Commit 0a9cdc0

Browse files
committed
unify delete info message
1 parent 850a6fb commit 0a9cdc0

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

cmd/pbm/delete.go

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,13 @@ func deleteBackupByName(ctx context.Context, pbm sdk.Client, d *deleteBcpOpts) (
9292
return sdk.NoOpID, errors.Wrap(err, "backup cannot be deleted")
9393
}
9494

95-
fmt.Println(shortBcpInfo(bcp))
96-
if bcp.Type == sdk.IncrementalBackup {
97-
for _, increments := range bcp.Increments {
98-
for _, inc := range increments {
99-
fmt.Println(shortBcpInfo(inc))
100-
}
95+
allBackups := []sdk.BackupMetadata{*bcp}
96+
for _, increments := range bcp.Increments {
97+
for _, inc := range increments {
98+
allBackups = append(allBackups, *inc)
10199
}
102100
}
101+
printDeleteInfoTo(os.Stdout, allBackups, nil)
103102

104103
if d.dryRun {
105104
return sdk.NoOpID, nil
@@ -136,9 +135,7 @@ func deleteManyBackup(ctx context.Context, pbm sdk.Client, d *deleteBcpOpts) (sd
136135
return sdk.NoOpID, errors.Wrap(err, "fetch backup list")
137136
}
138137

139-
for i := range backups {
140-
fmt.Println(shortBcpInfo(&backups[i]))
141-
}
138+
printDeleteInfoTo(os.Stdout, backups, nil)
142139

143140
if d.dryRun {
144141
return sdk.NoOpID, nil
@@ -153,20 +150,6 @@ func deleteManyBackup(ctx context.Context, pbm sdk.Client, d *deleteBcpOpts) (sd
153150
return cid, errors.Wrap(err, "schedule delete")
154151
}
155152

156-
func shortBcpInfo(bcp *sdk.BackupMetadata) string {
157-
size := fmtSize(bcp.Size)
158-
159-
t := string(bcp.Type)
160-
if bcp.Type == sdk.LogicalBackup && len(bcp.Namespaces) != 0 {
161-
t += ", selective"
162-
} else if bcp.Type == defs.IncrementalBackup && bcp.SrcBackup == "" {
163-
t += ", base"
164-
}
165-
166-
restoreTime := time.Unix(int64(bcp.LastWriteTS.T), 0).UTC().Format(time.RFC3339)
167-
return fmt.Sprintf("%q [size: %s type: <%s>, restore time: %s]", bcp.Name, size, t, restoreTime)
168-
}
169-
170153
type deletePitrOpts struct {
171154
olderThan string
172155
yes bool
@@ -251,14 +234,13 @@ func doCleanup(ctx context.Context, conn connect.Client, pbm sdk.Client, d *clea
251234
return outMsg{"nothing to delete"}, nil
252235
}
253236

237+
printDeleteInfoTo(os.Stdout, info.Backups, info.Chunks)
238+
254239
if d.dryRun {
255-
b := &strings.Builder{}
256-
printCleanupInfoTo(b, info.Backups, info.Chunks)
257-
return b, nil
240+
return nil, nil
258241
}
259-
260242
if !d.yes {
261-
if err := askCleanupConfirmation(info); err != nil {
243+
if err := askConfirmation("Are you sure you want to delete?"); err != nil {
262244
if errors.Is(err, errUserCanceled) {
263245
return outMsg{err.Error()}, nil
264246
}
@@ -319,19 +301,22 @@ func parseDuration(s string) (time.Duration, error) {
319301
return time.Duration(d * 24 * int64(time.Hour)), nil
320302
}
321303

322-
func printCleanupInfoTo(w io.Writer, backups []backup.BackupMeta, chunks []oplog.OplogChunk) {
304+
func printDeleteInfoTo(w io.Writer, backups []backup.BackupMeta, chunks []oplog.OplogChunk) {
323305
if len(backups) != 0 {
324306
fmt.Fprintln(w, "Snapshots:")
325307
for i := range backups {
326308
bcp := &backups[i]
309+
327310
t := string(bcp.Type)
328-
if util.IsSelective(bcp.Namespaces) {
311+
if bcp.Type == sdk.LogicalBackup && len(bcp.Namespaces) != 0 {
329312
t += ", selective"
330313
} else if bcp.Type == defs.IncrementalBackup && bcp.SrcBackup == "" {
331314
t += ", base"
332315
}
333-
fmt.Fprintf(w, " - %s <%s> [restore_time: %s]\n",
334-
bcp.Name, t, fmtTS(int64(bcp.LastWriteTS.T)))
316+
317+
restoreTime := time.Unix(int64(bcp.LastWriteTS.T), 0).UTC().Format(time.RFC3339)
318+
fmt.Fprintf(w, " - %q [size: %s type: <%s>, restore time: %s]",
319+
bcp.Name, fmtSize(bcp.Size), t, restoreTime)
335320
}
336321
}
337322

@@ -371,11 +356,6 @@ func printCleanupInfoTo(w io.Writer, backups []backup.BackupMeta, chunks []oplog
371356
}
372357
}
373358

374-
func askCleanupConfirmation(info backup.CleanupInfo) error {
375-
printCleanupInfoTo(os.Stdout, info.Backups, info.Chunks)
376-
return askConfirmation("Are you sure you want to delete?")
377-
}
378-
379359
var errUserCanceled = errors.New("canceled")
380360

381361
func askConfirmation(question string) error {

0 commit comments

Comments
 (0)