GetTwoLatestSnapshotsForASource fetches two latest snapshot manifests for a given source if they exist.
(ctx context.Context, rep repo.Repository, source snapshot.SourceInfo)
| 442 | |
| 443 | // GetTwoLatestSnapshotsForASource fetches two latest snapshot manifests for a given source if they exist. |
| 444 | func GetTwoLatestSnapshotsForASource(ctx context.Context, rep repo.Repository, source snapshot.SourceInfo) (secondLast, last *snapshot.Manifest, err error) { |
| 445 | snapshots, err := snapshot.ListSnapshots(ctx, rep, source) |
| 446 | if err != nil { |
| 447 | return nil, nil, errors.Wrap(err, "failed to list all snapshots") |
| 448 | } |
| 449 | |
| 450 | const minimumReqSnapshots = 2 |
| 451 | |
| 452 | sizeSnapshots := len(snapshots) |
| 453 | if sizeSnapshots < minimumReqSnapshots { |
| 454 | return nil, nil, errors.New("snapshot source has less than two snapshots") |
| 455 | } |
| 456 | |
| 457 | sort.Slice(snapshots, func(i, j int) bool { |
| 458 | return snapshots[i].StartTime.Before(snapshots[j].StartTime) |
| 459 | }) |
| 460 | |
| 461 | return snapshots[sizeSnapshots-2], snapshots[sizeSnapshots-1], nil |
| 462 | } |