Skip to content

Commit

Permalink
Merge pull request #141 from AndreasSko/playlist_version
Browse files Browse the repository at this point in the history
Support JW Library 14
  • Loading branch information
AndreasSko authored Jul 24, 2023
2 parents 39bf323 + 24670b1 commit c4706de
Show file tree
Hide file tree
Showing 34 changed files with 1,034 additions and 1,008 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: '1.14'
go-version: '1.20'
- name: Checkout code
uses: actions/checkout@v3
- name: Run tests
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: '1.16'
go-version: '1.20'
- name: Checkout code
uses: actions/checkout@v3
- name: Run tests
Expand All @@ -32,9 +32,9 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: '1.16'
go-version: '1.20'
- name: Install Gomobile
run: go get golang.org/x/mobile/cmd/gomobile
run: go install golang.org/x/mobile/cmd/gomobile@latest
- name: Checkout code
uses: actions/checkout@v3
- name: Initialize Gomobile
Expand Down
42 changes: 26 additions & 16 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ automatically solve conflicts using the 'chooseLeft', 'chooseRight', and
'chooseNewest' resolvers (see Flags).`,
Example: `go-jwlm merge left.jwlibrary right.jwlibrary merged.jwlibrary
go-jwlm merge left.jwlibrary right.jwlibrary merged.jwlibrary --bookmarks chooseLeft --markings chooseRight --notes chooseNewest --inputFields chooseRight`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
leftFilename := args[0]
rightFilename := args[1]
mergedFilename := args[2]
merge(leftFilename, rightFilename, mergedFilename, terminal.Stdio{In: os.Stdin, Out: os.Stdout, Err: os.Stderr})
return merge(leftFilename, rightFilename, mergedFilename, terminal.Stdio{In: os.Stdin, Out: os.Stdout, Err: os.Stderr})
},
Args: cobra.ExactArgs(3),
}
Expand All @@ -49,19 +49,27 @@ var NoteResolver string
// InputFieldResolver represents a resolver that should be used for conflicting InputFields
var InputFieldResolver string

func merge(leftFilename string, rightFilename string, mergedFilename string, stdio terminal.Stdio) {
// SkipPlaylists indicates if playlists should be skipped when importing backups.
// It is meant as a temporary workaround until merging of playlists is implemented.
var SkipPlaylists bool

func merge(leftFilename string, rightFilename string, mergedFilename string, stdio terminal.Stdio) error {
fmt.Fprintln(stdio.Out, "Importing left backup")
left := model.Database{}
left := model.Database{
SkipPlaylists: SkipPlaylists,
}
err := left.ImportJWLBackup(leftFilename)
if err != nil {
log.Fatal(err)
return fmt.Errorf("failed to import left backup: %w", err)
}

fmt.Fprintln(stdio.Out, "Importing right backup")
right := model.Database{}
right := model.Database{
SkipPlaylists: SkipPlaylists,
}
err = right.ImportJWLBackup(rightFilename)
if err != nil {
log.Fatal(err)
return fmt.Errorf("failed to import right backup: %w", err)
}

fmt.Fprintln(stdio.Out, "⌛ Preparing Databases")
Expand All @@ -72,7 +80,7 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std
fmt.Fprintln(stdio.Out, "🧭 Merging Locations")
mergedLocations, locationIDChanges, err := merger.MergeLocations(left.Location, right.Location)
if err != nil {
log.Fatal(err)
return fmt.Errorf("failed to merge locations: %w", err)
}
merged.Location = mergedLocations
merger.UpdateLRIDs(left.Bookmark, right.Bookmark, "LocationID", locationIDChanges)
Expand Down Expand Up @@ -105,7 +113,7 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std
addToSolutions(bookmarksConflictSolution, newSolutions)
}
default:
log.Fatal(err)
return fmt.Errorf("failed to merge bookmarks: %w", err)
}
}
fmt.Fprintln(stdio.Out, "Done.")
Expand All @@ -132,7 +140,7 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std
addToSolutions(inputFieldConflictSolution, newSolutions)
}
default:
log.Fatal(err)
return fmt.Errorf("failed to merge inputFields: %w", err)
}
}
fmt.Fprintln(stdio.Out, "Done.")
Expand All @@ -150,7 +158,7 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std
case merger.MergeConflictError:
tagsConflictSolution = handleMergeConflict(err.Conflicts, nil, stdio) // TODO
default:
log.Fatal(err)
return fmt.Errorf("failed to merge tags: %w", err)
}
}
fmt.Fprintln(stdio.Out, "Done.")
Expand Down Expand Up @@ -179,7 +187,7 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std
addToSolutions(UMBRConflictSolution, newSolutions)
}
default:
log.Fatal(err)
return fmt.Errorf("failed to merge markings: %w", err)
}
}
fmt.Fprintln(stdio.Out, "Done.")
Expand Down Expand Up @@ -207,7 +215,7 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std
addToSolutions(notesConflictSolution, newSolutions)
}
default:
log.Fatal(err)
return fmt.Errorf("failed to merge notes: %w", err)
}
}
fmt.Fprintln(stdio.Out, "Done.")
Expand All @@ -224,7 +232,7 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std
case merger.MergeConflictError:
tagMapsConflictSolution = handleMergeConflict(err.Conflicts, nil, stdio)
default:
log.Fatal(err)
return fmt.Errorf("failed to merge tagMaps: %w", err)
}
}
fmt.Fprintln(stdio.Out, "Done.")
Expand All @@ -233,14 +241,15 @@ func merge(leftFilename string, rightFilename string, mergedFilename string, std

fmt.Fprintln(stdio.Out, "⌛ Preparing merged database for exporting")
if err := merger.PrepareDatabasesPostMerge(&merged); err != nil {
log.Fatal(err)
return fmt.Errorf("failed to prepare database after merging: %w", err)
}

fmt.Fprintln(stdio.Out, "Exporting merged database")
if err = merged.ExportJWLBackup(mergedFilename); err != nil {
log.Fatal(err)
return fmt.Errorf("failed to export backup: %w", err)
}

return nil
}

// addToSolutions adds new mergeSolutions to the existing map of mergeSolutions
Expand Down Expand Up @@ -320,4 +329,5 @@ func init() {
mergeCmd.Flags().StringVar(&MarkingResolver, "markings", "", "Resolve conflicting markings with resolver (can be 'chooseLeft' or 'chooseRight')")
mergeCmd.Flags().StringVar(&NoteResolver, "notes", "", "Resolve conflicting notes with resolver (can be 'chooseNewest', 'chooseLeft', or 'chooseRight')")
mergeCmd.Flags().StringVar(&InputFieldResolver, "inputFields", "", "Resolve conflicting inputFields with resolver (can be 'chooseLeft', or 'chooseRight')")
mergeCmd.Flags().BoolVar(&SkipPlaylists, "skipPlaylists", false, "Skip playlists when importing backups. It is meant as a temporary workaround until merging of playlists is implemented.")
}
48 changes: 48 additions & 0 deletions cmd/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,37 @@ func Test_merge(t *testing.T) {
merged.ImportJWLBackup(mergedAllLeftDBEmptyBRFilename)
assert.True(t, mergedAllLeftDBEmptyBR.Equals(merged))
})

// Attempt to merge with playlists, while skipPlaylists is set to false
RunCmdTest(t,
func(t *testing.T, c *expect.Console) {
c.ExpectString("Error: failed to import left backup: merging of playlists is not supported yet. " +
"Enable SkipPlaylists flag to skip this safety check")
c.ExpectEOF()
},
func(t *testing.T, c *expect.Console) {
merge("../model/testdata/backup_withPlaylist.jwlibrary",
"../model/testdata/backup_withPlaylist.jwlibrary",
mergedFilename,
terminal.Stdio{In: c.Tty(), Out: c.Tty(), Err: c.Tty()})
},
)

// Merge with playlists and skipPlaylists set to true
RunCmdTest(t,
func(t *testing.T, c *expect.Console) {
c.ExpectString("🎉 Finished merging!")
c.ExpectEOF()
},
func(t *testing.T, c *expect.Console) {
SkipPlaylists = true
defer func() { SkipPlaylists = false }()
merge("../model/testdata/backup_withPlaylist.jwlibrary",
"../model/testdata/backup_withPlaylist.jwlibrary",
mergedFilename,
terminal.Stdio{In: c.Tty(), Out: c.Tty(), Err: c.Tty()})
},
)
}

// https://github.com/AlecAivazis/survey/blob/master/survey_posix_test.go
Expand Down Expand Up @@ -486,6 +517,7 @@ var leftDB = &model.Database{
Title: sql.NullString{"Am Anfang erschuf Gott Himmel und Erde.", true},
Content: sql.NullString{"📝 for left version", true},
LastModified: "2020-09-15T13:45:38+00:00",
Created: "2020-09-15T13:45:38+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand All @@ -495,6 +527,7 @@ var leftDB = &model.Database{
Title: sql.NullString{"Same Note", true},
Content: sql.NullString{"This note is also available on the other side", true},
LastModified: "2020-09-15T13:52:25+00:00",
Created: "2020-09-15T13:52:25+00:00",
BlockType: 0,
},
},
Expand Down Expand Up @@ -654,6 +687,7 @@ var rightDB = &model.Database{
Title: sql.NullString{"So wurde die Erschaffung von Himmel und Erde und allem, was dazugehört, beendet.", true},
Content: sql.NullString{"📝 on the right side", true},
LastModified: "2020-09-15T13:47:56+00:00",
Created: "2020-09-15T13:47:56+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand All @@ -663,6 +697,7 @@ var rightDB = &model.Database{
Title: sql.NullString{"Same Note", true},
Content: sql.NullString{"This note is also available on the other side. Though this one is newer 😏", true},
LastModified: "2020-09-20T13:52:25+00:00",
Created: "2020-09-15T13:52:25+00:00",
BlockType: 0,
},
},
Expand Down Expand Up @@ -832,6 +867,7 @@ var mergedAllLeftDB = &model.Database{
Title: sql.NullString{"Am Anfang erschuf Gott Himmel und Erde.", true},
Content: sql.NullString{"📝 for left version", true},
LastModified: "2020-09-15T13:45:38+00:00",
Created: "2020-09-15T13:45:38+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand All @@ -841,6 +877,7 @@ var mergedAllLeftDB = &model.Database{
Title: sql.NullString{"Same Note", true},
Content: sql.NullString{"This note is also available on the other side", true},
LastModified: "2020-09-15T13:52:25+00:00",
Created: "2020-09-15T13:52:25+00:00",
BlockType: 0,
},
{
Expand All @@ -851,6 +888,7 @@ var mergedAllLeftDB = &model.Database{
Title: sql.NullString{"So wurde die Erschaffung von Himmel und Erde und allem, was dazugehört, beendet.", true},
Content: sql.NullString{"📝 on the right side", true},
LastModified: "2020-09-15T13:47:56+00:00",
Created: "2020-09-15T13:47:56+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand Down Expand Up @@ -1038,6 +1076,7 @@ var mergedAllRightDB = &model.Database{
Title: sql.NullString{"Am Anfang erschuf Gott Himmel und Erde.", true},
Content: sql.NullString{"📝 for left version", true},
LastModified: "2020-09-15T13:45:38+00:00",
Created: "2020-09-15T13:45:38+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand All @@ -1047,6 +1086,7 @@ var mergedAllRightDB = &model.Database{
Title: sql.NullString{"Same Note", true},
Content: sql.NullString{"This note is also available on the other side. Though this one is newer 😏", true},
LastModified: "2020-09-20T13:52:25+00:00",
Created: "2020-09-20T13:52:25+00:00",
BlockType: 0,
},
{
Expand All @@ -1057,6 +1097,7 @@ var mergedAllRightDB = &model.Database{
Title: sql.NullString{"So wurde die Erschaffung von Himmel und Erde und allem, was dazugehört, beendet.", true},
Content: sql.NullString{"📝 on the right side", true},
LastModified: "2020-09-15T13:47:56+00:00",
Created: "2020-09-15T13:47:56+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand Down Expand Up @@ -1696,6 +1737,7 @@ var leftDBEmptyBR = &model.Database{
Title: sql.NullString{"Am Anfang erschuf Gott Himmel und Erde.", true},
Content: sql.NullString{"📝 for left version", true},
LastModified: "2020-09-15T13:45:38+00:00",
Created: "2020-09-15T13:45:38+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand All @@ -1705,6 +1747,7 @@ var leftDBEmptyBR = &model.Database{
Title: sql.NullString{"Same Note", true},
Content: sql.NullString{"This note is also available on the other side", true},
LastModified: "2020-09-15T13:52:25+00:00",
Created: "2020-09-15T13:52:25+00:00",
BlockType: 0,
},
},
Expand Down Expand Up @@ -1872,6 +1915,7 @@ var RightDBEmptyBR = &model.Database{
Title: sql.NullString{"So wurde die Erschaffung von Himmel und Erde und allem, was dazugehört, beendet.", true},
Content: sql.NullString{"📝 on the right side", true},
LastModified: "2020-09-15T13:47:56+00:00",
Created: "2020-09-15T13:47:56+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand All @@ -1881,6 +1925,7 @@ var RightDBEmptyBR = &model.Database{
Title: sql.NullString{"Same Note", true},
Content: sql.NullString{"This note is also available on the other side. Though this one is newer 😏", true},
LastModified: "2020-09-20T13:52:25+00:00",
Created: "2020-09-20T13:52:25+00:00",
BlockType: 0,
},
},
Expand Down Expand Up @@ -2058,6 +2103,7 @@ var mergedAllLeftDBEmptyBR = &model.Database{
Title: sql.NullString{"Am Anfang erschuf Gott Himmel und Erde.", true},
Content: sql.NullString{"📝 for left version", true},
LastModified: "2020-09-15T13:45:38+00:00",
Created: "2020-09-15T13:45:38+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand All @@ -2067,6 +2113,7 @@ var mergedAllLeftDBEmptyBR = &model.Database{
Title: sql.NullString{"Same Note", true},
Content: sql.NullString{"This note is also available on the other side", true},
LastModified: "2020-09-15T13:52:25+00:00",
Created: "2020-09-15T13:52:25+00:00",
BlockType: 0,
},
{
Expand All @@ -2077,6 +2124,7 @@ var mergedAllLeftDBEmptyBR = &model.Database{
Title: sql.NullString{"So wurde die Erschaffung von Himmel und Erde und allem, was dazugehört, beendet.", true},
Content: sql.NullString{"📝 on the right side", true},
LastModified: "2020-09-15T13:47:56+00:00",
Created: "2020-09-15T13:47:56+00:00",
BlockType: 2,
BlockIdentifier: sql.NullInt32{1, true},
},
Expand Down
Loading

0 comments on commit c4706de

Please sign in to comment.