Skip to content

Commit

Permalink
Exclude timestamp (updatedAt) from cache key
Browse files Browse the repository at this point in the history
Cache key should be able to be specified with just a tag.

However, currently, "extension" is required, so it is implemented with tag and URL.
  • Loading branch information
k1LoW committed Oct 5, 2023
1 parent f37df0f commit 3d107f0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
17 changes: 6 additions & 11 deletions dewy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"context"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -115,20 +113,17 @@ func (d *Dewy) Run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if err := d.repo.Fetch(); err != nil {
log.Printf("[ERROR] Fetch failure: %#v", err)
return err
}

// Create latest cache key
du, ut := d.repo.LatestKey()
u, err := url.Parse(du)
// Get current
res, err := d.repo.Current(&repo.CurrentRequest{
ArtifactName: d.config.Repository.Artifact,
})
if err != nil {
log.Printf("[ERROR] Current failure: %#v", err)
return err
}
cacheKey := strings.Replace(fmt.Sprintf("%s--%d-%s", u.Host, ut.Unix(), u.RequestURI()), "/", "-", -1)

// Check cache
cacheKey := fmt.Sprintf("%s-%s", res.Tag, filepath.Base(res.ArtifactURL))
currentKey := "current.txt"
currentSourceKey, _ := d.cache.Read(currentKey)
found := false
Expand Down
30 changes: 17 additions & 13 deletions repo/github_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type GithubRelease struct {
name string
artifact string
downloadURL string
cacheKey string
releaseID int64
assetID int64
releaseURL string
Expand Down Expand Up @@ -103,32 +102,37 @@ func (g *GithubRelease) ReleaseURL() string {
return g.releaseURL
}

// Fetch to latest github release
func (g *GithubRelease) Fetch() error {
func (g *GithubRelease) Current(req *CurrentRequest) (*CurrentResponse, error) {
release, err := g.latest()
if err != nil {
return err
return nil, err
}

g.releaseID = *release.ID
g.releaseURL = *release.HTMLURL

found := false
for _, v := range release.Assets {
if *v.Name == g.artifact {
if v.GetName() == req.ArtifactName {
found = true
log.Printf("[DEBUG] Fetched: %+v", v)
g.downloadURL = *v.BrowserDownloadURL
g.releaseTag = *release.TagName
g.assetID = *v.ID
g.updatedAt = *v.UpdatedAt
g.downloadURL = v.GetBrowserDownloadURL()
g.releaseTag = release.GetTagName()
g.assetID = v.GetID()
g.updatedAt = v.GetUpdatedAt()
break
}
}
if !found {
return nil, fmt.Errorf("artifact not found: %s", req.ArtifactName)
}

return nil
}
au := fmt.Sprintf("github_release://%s/%s/tag/%s/%s", g.owner, g.name, release.GetTagName(), req.ArtifactName)

func (g *GithubRelease) LatestKey() (string, time.Time) {
return g.downloadURL, g.updatedAt.Time
return &CurrentResponse{
Tag: release.GetTagName(),
ArtifactURL: au,
}, nil
}

func (g *GithubRelease) latest() (*github.RepositoryRelease, error) {
Expand Down
20 changes: 17 additions & 3 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,33 @@ import (
"errors"
"io"
"path"
"time"
)

// CurrentRequest is the request to get the current artifact.
type CurrentRequest struct {
// ArtifactName is the name of the artifact to fetch.
// FIXME: If possible, ArtifactName should be optional.
ArtifactName string
}

// CurrentResponse is the response to get the current artifact.
type CurrentResponse struct {
// Tag uniquely identifies the artifact concerned.
Tag string
// ArtifactURL is the URL to download the artifact.
// The URL is not only "https://"
ArtifactURL string
}

// Repo interface for repository
type Repo interface {
String() string
Fetch() error
RecordShipping() error
ReleaseTag() string
ReleaseURL() string
OwnerURL() string
OwnerIconURL() string
LatestKey() (string, time.Time)
Current(req *CurrentRequest) (*CurrentResponse, error)
Download(w io.Writer) error
URL() string
}
Expand Down

0 comments on commit 3d107f0

Please sign in to comment.