Skip to content

Commit

Permalink
Merge pull request #30 from k1LoW/detect-artifact
Browse files Browse the repository at this point in the history
Detect artifact using GOOS and GOARCH
  • Loading branch information
linyows authored Oct 8, 2023
2 parents a5b6116 + b81691f commit 9f805ed
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
3 changes: 3 additions & 0 deletions dewy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/signal"
"path/filepath"
"runtime"
"sort"
"sync"
"syscall"
Expand Down Expand Up @@ -124,6 +125,8 @@ func (d *Dewy) Run() error {

// Get current
res, err := d.registory.Current(&registory.CurrentRequest{
Arch: runtime.GOARCH,
OS: runtime.GOOS,
ArtifactName: d.config.Repository.Artifact,
})
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion dewy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func TestRun(t *testing.T) {
c.Repository = repo.Config{
Owner: "linyows",
Repo: "dewy",
Artifact: "dewy_darwin_x86_64.tar.gz",
DisableRecordShipping: true,
}
c.Cache = CacheConfig{
Expand Down
4 changes: 4 additions & 0 deletions registory/registory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ type Registory interface {

// CurrentRequest is the request to get the current artifact.
type CurrentRequest struct {
// Arch is the CPU architecture of deployment environment.
Arch string
// OS is the operating system of deployment environment.
OS string
// ArtifactName is the name of the artifact to fetch.
// FIXME: If possible, ArtifactName should be optional.
ArtifactName string
Expand Down
60 changes: 52 additions & 8 deletions repo/github_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,66 @@ func (g *GithubRelease) Current(req *registory.CurrentRequest) (*registory.Curre
if err != nil {
return nil, err
}
var artifactName string

found := false
for _, v := range release.Assets {
if v.GetName() == req.ArtifactName {
found = true
if req.ArtifactName != "" {
artifactName = req.ArtifactName
found := false
for _, v := range release.Assets {
if v.GetName() == artifactName {
found = true
log.Printf("[DEBUG] Fetched: %+v", v)
g.downloadURL = v.GetBrowserDownloadURL()
g.updatedAt = v.GetUpdatedAt()
break
}
}
if !found {
return nil, fmt.Errorf("artifact not found: %s", artifactName)
}
} else {
archMatchs := []string{req.Arch}
if req.Arch == "amd64" {
archMatchs = append(archMatchs, "x86_64")
}
osMatchs := []string{req.OS}
if req.OS == "darwin" {
osMatchs = append(osMatchs, "macos")
}
found := false
for _, v := range release.Assets {
n := strings.ToLower(v.GetName())
for _, arch := range archMatchs {
if strings.Contains(n, arch) {
found = true
break
}
}
if !found {
continue
}
found = false
for _, os := range osMatchs {
if strings.Contains(n, os) {
found = true
break
}
}
if !found {
continue
}
artifactName = v.GetName()
log.Printf("[DEBUG] Fetched: %+v", v)
g.downloadURL = v.GetBrowserDownloadURL()
g.updatedAt = v.GetUpdatedAt()
break
}
}
if !found {
return nil, fmt.Errorf("artifact not found: %s", req.ArtifactName)
if !found {
return nil, fmt.Errorf("artifact not found: %s", artifactName)
}
}

au := fmt.Sprintf("github_release://%s/%s/tag/%s/%s", g.owner, g.repo, release.GetTagName(), req.ArtifactName)
au := fmt.Sprintf("github_release://%s/%s/tag/%s/%s", g.owner, g.repo, release.GetTagName(), artifactName)

return &registory.CurrentResponse{
ID: time.Now().Format(ISO8601),
Expand Down

0 comments on commit 9f805ed

Please sign in to comment.