Skip to content

Commit

Permalink
✨ GetSnippet
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasSko committed Jul 4, 2021
1 parent 9822fa8 commit 153ba9e
Show file tree
Hide file tree
Showing 12 changed files with 487 additions and 16 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ jobs:
go-version: '1.16'
- name: Checkout code
uses: actions/checkout@v2

- name: Use github.com/AndreasSko/jwpub-snippets instead of fake implementation
uses: AndreasSko/find-and-replace-action@main
with:
from: ^replace github.com\/AndreasSko\/jwpub-snippets.*$
to: ""

- name: Inject insteadOf config for private repo
env:
JWPUB_SNIPPET_GITHUB_TOKEN: ${{ secrets.JWPUB_SNIPPET_GITHUB_TOKEN }}
run: git config --global url."https://${JWPUB_SNIPPET_GITHUB_TOKEN}:[email protected]".insteadOf "https://github.com"

- run: go mod tidy
env:
GOPRIVATE: github.com/AndreasSko

- name: Run tests
run: go test ./...

Expand All @@ -33,13 +49,33 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: '1.16'

- name: Install Gomobile
run: go get golang.org/x/mobile/cmd/gomobile

- name: Checkout code
uses: actions/checkout@v2

- name: Initialize Gomobile
run: gomobile init
working-directory: gomobile

- name: Use github.com/AndreasSko/jwpub-snippets instead of fake implementation
uses: jacobtomlinson/[email protected]
with:
include: go.mod
find: ^replace github.com\/AndreasSko\/jwpub-snippets.*$
replace: ""

- name: Inject insteadOf config for private repo
env:
JWPUB_SNIPPET_GITHUB_TOKEN: ${{ secrets.JWPUB_SNIPPET_GITHUB_TOKEN }}
run: git config --global url."https://${JWPUB_SNIPPET_GITHUB_TOKEN}:[email protected]".insteadOf "https://github.com"

- run: go mod tidy
env:
GOPRIVATE: github.com/AndreasSko

- name: Build for iOS
run: gomobile bind -target ios -v
working-directory: gomobile
17 changes: 3 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,30 @@ go 1.16

require (
github.com/AlecAivazis/survey/v2 v2.2.12
github.com/AndreasSko/jwpub-snippets v0.0.0-20210627134355-912640c387c2
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Jeffail/gabs v1.4.0
github.com/MakeNowJust/heredoc v1.0.0
github.com/Netflix/go-expect v0.0.0-20200312175327-da48e75238e2
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/buger/goterm v1.0.0
github.com/cavaliercoder/grab v1.0.1-0.20201108051000-98a5bfe305ec
github.com/codeclysm/extract/v3 v3.0.2
github.com/davecgh/go-spew v1.1.1
github.com/go-openapi/errors v0.20.0 // indirect
github.com/go-openapi/strfmt v0.20.0 // indirect
github.com/h2non/filetype v1.1.1 // indirect
github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/juju/errors v0.0.0-20200330140219-3fe23663418f // indirect
github.com/klauspost/compress v1.13.1
github.com/kr/pty v1.1.8 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/mattn/go-sqlite3 v1.14.7
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-wordwrap v1.0.1
github.com/pkg/errors v0.9.1
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sergi/go-diff v1.2.0
github.com/sirupsen/logrus v1.8.1
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
go.mongodb.org/mongo-driver v1.5.0 // indirect
golang.org/x/crypto v0.0.0-20210317152858-513c2a44f670 // indirect
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect
golang.org/x/term v0.0.0-20210317153231-de623e64d2a6 // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

replace github.com/AndreasSko/jwpub-snippets v0.0.0-20210627134355-912640c387c2 => ./publication/jwpub-snippets
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/AlecAivazis/survey/v2 v2.2.12 h1:5a07y93zA6SZ09gOa9wLVLznF5zTJMQ+pJ3cZK4IuO8=
github.com/AlecAivazis/survey/v2 v2.2.12/go.mod h1:6d4saEvBsfSHXeN1a5OA5m2+HJ2LuVokllnC77pAIKI=
github.com/AndreasSko/go-jwlm v0.0.0-20210626065356-cc5e9e829b5f/go.mod h1:5oGuOLP/T3tDm/jhelqhPjvjzXnrxd1r8fNf89yxLIQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
Expand Down Expand Up @@ -550,6 +551,7 @@ golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -626,6 +628,7 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 h1:faDu4veV+8pcThn4fewv6TVlNCezafGoC1gM/mxQLbQ=
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -703,7 +706,6 @@ golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
Expand Down
96 changes: 96 additions & 0 deletions gomobile/Publication.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package gomobile

import (
"context"
"encoding/json"
"fmt"

"github.com/AndreasSko/go-jwlm/model"
"github.com/AndreasSko/go-jwlm/publication"
)

Expand Down Expand Up @@ -35,3 +38,96 @@ func LookupPublication(dbPath string, query *PublicationLookup) string {

return string(jsn)
}

// GetSnippet fetches the snippet related to a mergeConflict
func (mcw *MergeConflictsWrapper) GetSnippet(catalogDir, publDir string, conflictKey string) (string, error) {
conflict, ok := mcw.conflicts[conflictKey]
if !ok {
return "", fmt.Errorf("conflict with key %s does not exist", conflictKey)
}

umbr, ok := conflict.Left.(*model.UserMarkBlockRange)
if !ok {
return "", fmt.Errorf("only UserMarkBlockRanges are supported for getting a snippet, %T given", conflict)
}

location := umbr.RelatedEntries(mcw.DBWrapper.left).Location

publQuery := publication.Lookup{
DocumentID: int(location.DocumentID.Int32),
KeySymbol: location.KeySymbol.String,
IssueTagNumber: location.IssueTagNumber,
MepsLanguage: location.MepsLanguage,
}
publ, err := publication.LookupPublication(catalogDir, publQuery)
if err != nil {
return "", fmt.Errorf("could not lookup publication: %w", err)
}

snippets, err := publication.GetSnippet(publDir, publ, *location, *umbr)
if err != nil {
return "", fmt.Errorf("could not get snippet: %w", err)
}

result, err := json.Marshal(snippets)
if err != nil {
return "", fmt.Errorf("could not marshal snippets to JSON: %w", err)
}

return string(result), nil
}

// GetPublicationPath generates the filename of the publication (given in JSON format)
// and checks if it exists in the publDir
func GetPublicationPath(publJSON string, publDir string) (string, error) {
publ := publication.Publication{}
if err := json.Unmarshal([]byte(publJSON), &publ); err != nil {
return "", fmt.Errorf("could not unmarshal publication: %w", err)
}

return publication.GetPublicationPath(publ, publDir)
}

// DownloadPublication downloads the publication (given in JSON format) and saves it at dst. The
// returned DownloadManager allows to keep track and manage the running download
func DownloadPublication(publJSON string, dst string) *DownloadManager {
publ := publication.Publication{}
if err := json.Unmarshal([]byte(publJSON), &publ); err != nil {
return &DownloadManager{
Progress: &DownloadProgress{
Done: true,
},
err: err,
}
}

ctx, cancel := context.WithCancel(context.Background())
dm := &DownloadManager{
Progress: &DownloadProgress{},
prgrsChan: make(chan publication.Progress),
ctx: ctx,
cancel: cancel,
}

// Start download in sub-goroutine, while monitoring its progress
go func() {
done := make(chan struct{})
go func() {
_, err := publication.DownloadPublication(ctx, dm.prgrsChan, publ, dst)
if err != nil {
dm.err = err
}
done <- struct{}{}
}()
for progress := range dm.prgrsChan {
dm.Progress.Size = progress.Size
dm.Progress.BytesComplete = progress.BytesComplete
dm.Progress.BytesPerSecond = progress.BytesPerSecond
dm.Progress.Progress = progress.Progress
}
<-done
dm.Progress.Done = true
}()

return dm
}
Loading

0 comments on commit 153ba9e

Please sign in to comment.