Skip to content

Commit 3aac33f

Browse files
committed
Added the migrate command to update old library files.
1 parent dd74817 commit 3aac33f

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ This is a simple CLI tool that allows you to manage your audio book library.
44

55
![Example using the table formatter](https://raw.githubusercontent.com/b0wter/fbrary/master/example.png)
66

7+
Updating from version 1.x
8+
=========================
9+
In case you have an old library files from a previous version you need run the `migrate` command to update your library file to the current standard:
10+
```bash
11+
./fbrary -l $LIBRARY_FILENAME migrate
12+
```
13+
714
Commands
815
========
916
The application is run in a style similar to `git`. There are several commands to manipulate/list your library.

src/cli/Arguments.fs

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ module Arguments =
9898
| [<CliPrefix(CliPrefix.None)>] Files of ParseResults<FilesArgs>
9999
| [<CliPrefix(CliPrefix.None)>] Unmatched of string
100100
| [<CliPrefix(CliPrefix.None)>] Write of ParseResults<WriteArgs>
101+
| [<CliPrefix(CliPrefix.None)>] Migrate
101102
| [<CliPrefix(CliPrefix.DoubleDash); First>] Version
102103
interface IArgParserTemplate with
103104
member s.Usage =
@@ -115,5 +116,6 @@ module Arguments =
115116
| Files _ -> "List all files of an audio book. Use the `list` command to find book ids."
116117
| Unmatched _ -> "Reads all mp3/ogg files in the given paths and checks if all files are known to the library."
117118
| Write _ -> "Write the meta data stored in the library to the actual mp3/ogg files."
119+
| Migrate -> "Migrate an old library file to the current format."
118120
| Version -> "Echo the version of this software."
119121

src/cli/Config.fs

+3
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ module Config =
174174
| Files of FilesConfig
175175
| Uninitialized
176176
| Write of WriteConfig
177+
| Migrate
177178
| Version
178179

179180
type Config = {
@@ -316,6 +317,8 @@ module Config =
316317
| _ -> emptyWriteConfig
317318
let updatedWriteConfig = w.GetAllResults() |> List.fold applyWriteArg writeConfig
318319
{ config with Command = Write updatedWriteConfig }
320+
| MainArgs.Migrate ->
321+
{ config with Command = Migrate }
319322
| MainArgs.Version ->
320323
{ config with Command = Version }
321324

src/cli/Program.fs

+38
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,42 @@ module Program =
380380
return 0
381381
}
382382

383+
let migrateLibrary (libraryFile: string) =
384+
result {
385+
// Make sure the config no longer uses the `LastScanned` field.
386+
do printfn "Making sure the `LastScanned` field is replaced with `LastUpdated`."
387+
let! content = IO.readTextFromFile libraryFile
388+
let updatedContent = content.Replace("\"LastScanned\": \"", "\"LastUpdated\": \"")
389+
let! library = Library.deserialize updatedContent
390+
let updatedLibrary = { library with LastUpdated = DateTime.Now }
391+
392+
// Update paths in the config to be absolute.
393+
do printfn "Making sure the sources use absolute paths."
394+
let rootPath = Path.GetDirectoryName libraryFile
395+
let rootPath = Path.Combine(Environment.CurrentDirectory, rootPath)
396+
let updateSource (book: Audiobook.Audiobook) : Audiobook.Audiobook =
397+
do printfn "Migrating relative path to use the root '%s'." rootPath
398+
let combinator (rootPath: string) (filename: string) =
399+
if filename.StartsWith(rootPath) then
400+
do printfn "Skipping '%s' because it already starts with the root path." filename
401+
filename
402+
else
403+
let updated = Path.Combine(rootPath, filename)
404+
do printfn "Updated '%s' to '%s'." filename updated
405+
updated
406+
407+
let configuredCombinator = combinator rootPath
408+
let updatedSource = match book.Source with
409+
| Audiobook.AudiobookSource.SingleFile f ->
410+
configuredCombinator f |> Audiobook.AudiobookSource.SingleFile
411+
| Audiobook.AudiobookSource.MultiFile m ->
412+
m |> List.map configuredCombinator |> Audiobook.AudiobookSource.MultiFile
413+
{ book with Source = updatedSource }
414+
let updatedFilesLibrary = { library with Audiobooks = updatedLibrary.Audiobooks |> List.map updateSource }
415+
do! updatedFilesLibrary |> Library.serialize |> IO.writeTextToFile libraryFile
416+
return 0
417+
}
418+
383419
[<EntryPoint>]
384420
let main argv =
385421
let r = result {
@@ -420,6 +456,8 @@ module Program =
420456
return 1
421457
| Config.Write writeConfig ->
422458
return! write config.LibraryFile writeConfig
459+
| Config.Migrate ->
460+
return! migrateLibrary config.LibraryFile
423461
| Config.Version ->
424462
do printfn "%s" Version.current
425463
return 0

0 commit comments

Comments
 (0)