tryToConvertPathToID checks if the source is a path and in this case returns the ID of the snapshot containing the latest version available.
(ctx context.Context, rep repo.Repository, source string)
| 464 | // tryToConvertPathToID checks if the source is a path and in this case returns the ID of the snapshot |
| 465 | // containing the latest version available. |
| 466 | func (c *commandRestore) tryToConvertPathToID(ctx context.Context, rep repo.Repository, source string) (string, error) { |
| 467 | pathElements := strings.Split(filepath.ToSlash(source), "/") |
| 468 | |
| 469 | if pathElements[0] != "" { |
| 470 | _, err := object.ParseID(pathElements[0]) |
| 471 | if err == nil { |
| 472 | // source is an ID |
| 473 | return source, nil |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // Consider source as a path |
| 478 | |
| 479 | if c.snapshotTime == "" { |
| 480 | return "", errors.New("a snapshot time is needed to use a path as source") |
| 481 | } |
| 482 | |
| 483 | filter, err := createSnapshotTimeFilter(c.snapshotTime) |
| 484 | if err != nil { |
| 485 | return "", err |
| 486 | } |
| 487 | |
| 488 | si, err := snapshot.ParseSourceInfo(source, rep.ClientOptions().Hostname, rep.ClientOptions().Username) |
| 489 | if err != nil { |
| 490 | return "", errors.Errorf("invalid directory: '%s': %s", source, err) |
| 491 | } |
| 492 | |
| 493 | if si.Path == "" { |
| 494 | return "", errors.New("the source must contain a path element") |
| 495 | } |
| 496 | |
| 497 | manifestIDs, err := findSnapshotsForSource(ctx, rep, si, map[string]string{}) |
| 498 | if err != nil { |
| 499 | return "", err |
| 500 | } |
| 501 | |
| 502 | if len(manifestIDs) == 0 { |
| 503 | return "", errors.Errorf("no snapshots contain data for %v", source) |
| 504 | } |
| 505 | |
| 506 | ms, err := snapshot.LoadSnapshots(ctx, rep, manifestIDs) |
| 507 | if err != nil { |
| 508 | return "", errors.Wrap(err, "unable to load snapshots") |
| 509 | } |
| 510 | |
| 511 | m, relPath, ohid := findLastManifestWithPath(ctx, rep, ms, si.Path, filter) |
| 512 | if m == nil { |
| 513 | return "", errors.Errorf("no snapshots contain data for %v", source) |
| 514 | } |
| 515 | |
| 516 | log(ctx).Infof("Restoring from\n"+ |
| 517 | " Snapshot source: %v\n"+ |
| 518 | " Snapshot time: %v\n"+ |
| 519 | " Relative path: %v\n"+ |
| 520 | " Object ID: %v", m.Source, formatTimestamp(m.StartTime.ToTime()), relPath, ohid) |
| 521 | |
| 522 | return ohid.String(), nil |
| 523 | } |
no test coverage detected