| 498 | } |
| 499 | |
| 500 | func migrateToV2(ctx context.DnoteCtx) error { |
| 501 | notePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) |
| 502 | |
| 503 | b, err := os.ReadFile(notePath) |
| 504 | if err != nil { |
| 505 | return errors.Wrap(err, "Failed to read the note file") |
| 506 | } |
| 507 | |
| 508 | var preDnote migrateToV2PreDnote |
| 509 | postDnote := migrateToV2PostDnote{} |
| 510 | |
| 511 | err = json.Unmarshal(b, &preDnote) |
| 512 | if err != nil { |
| 513 | return errors.Wrap(err, "Failed to unmarshal existing dnote into JSON") |
| 514 | } |
| 515 | |
| 516 | for bookName, book := range preDnote { |
| 517 | var notes = make([]migrateToV2PostNote, 0, len(book)) |
| 518 | for _, note := range book { |
| 519 | noteUUID, err := genUUID() |
| 520 | if err != nil { |
| 521 | return errors.Wrap(err, "generating uuid") |
| 522 | } |
| 523 | |
| 524 | newNote := migrateToV2PostNote{ |
| 525 | UUID: noteUUID, |
| 526 | Content: note.Content, |
| 527 | AddedOn: note.AddedOn, |
| 528 | EditedOn: 0, |
| 529 | } |
| 530 | |
| 531 | notes = append(notes, newNote) |
| 532 | } |
| 533 | |
| 534 | b := migrateToV2PostBook{ |
| 535 | Name: bookName, |
| 536 | Notes: notes, |
| 537 | } |
| 538 | |
| 539 | postDnote[bookName] = b |
| 540 | } |
| 541 | |
| 542 | d, err := json.MarshalIndent(postDnote, "", " ") |
| 543 | if err != nil { |
| 544 | return errors.Wrap(err, "Failed to marshal new dnote into JSON") |
| 545 | } |
| 546 | |
| 547 | err = os.WriteFile(notePath, d, 0644) |
| 548 | if err != nil { |
| 549 | return errors.Wrap(err, "Failed to write the new dnote into the file") |
| 550 | } |
| 551 | |
| 552 | return nil |
| 553 | } |
| 554 | |
| 555 | // migrateToV3 generates actions for existing dnote |
| 556 | func migrateToV3(ctx context.DnoteCtx) error { |