readFrom loads the lock file from an open file handle. Returns an empty file if the content is empty, corrupt, or incompatible.
(f *os.File)
| 51 | // readFrom loads the lock file from an open file handle. |
| 52 | // Returns an empty file if the content is empty, corrupt, or incompatible. |
| 53 | func readFrom(f *os.File) (*file, error) { |
| 54 | if _, err := f.Seek(0, 0); err != nil { |
| 55 | return nil, fmt.Errorf("could not seek lock file: %w", err) |
| 56 | } |
| 57 | data, err := io.ReadAll(f) |
| 58 | if err != nil { |
| 59 | return nil, fmt.Errorf("could not read lock file: %w", err) |
| 60 | } |
| 61 | if len(data) == 0 { |
| 62 | return newFile(), nil |
| 63 | } |
| 64 | |
| 65 | var lf file |
| 66 | if err := json.Unmarshal(data, &lf); err != nil { |
| 67 | return newFile(), nil //nolint:nilerr // graceful: corrupt file means fresh state |
| 68 | } |
| 69 | |
| 70 | if lf.Version != lockVersion || lf.Skills == nil { |
| 71 | return newFile(), nil |
| 72 | } |
| 73 | |
| 74 | return &lf, nil |
| 75 | } |
| 76 | |
| 77 | // writeTo persists the lock file through an open file handle. |
| 78 | func writeTo(f *os.File, lf *file) error { |
no test coverage detected