From b81691f3bb9a77df61878ad6874d87b5337e3ecb Mon Sep 17 00:00:00 2001 From: k1LoW Date: Fri, 6 Oct 2023 15:45:40 +0900 Subject: [PATCH] Detect artifact using GOOS and GOARCH --- dewy.go | 3 +++ dewy_test.go | 1 - registory/registory.go | 4 +++ repo/github_release.go | 60 ++++++++++++++++++++++++++++++++++++------ 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/dewy.go b/dewy.go index 78cb5ff..084a5f1 100644 --- a/dewy.go +++ b/dewy.go @@ -8,6 +8,7 @@ import ( "os" "os/signal" "path/filepath" + "runtime" "sort" "sync" "syscall" @@ -124,6 +125,8 @@ func (d *Dewy) Run() error { // Get current res, err := d.registory.Current(®istory.CurrentRequest{ + Arch: runtime.GOARCH, + OS: runtime.GOOS, ArtifactName: d.config.Repository.Artifact, }) if err != nil { diff --git a/dewy_test.go b/dewy_test.go index ea6f3fe..d29fa21 100644 --- a/dewy_test.go +++ b/dewy_test.go @@ -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{ diff --git a/registory/registory.go b/registory/registory.go index 3e4a2e4..7ab664d 100644 --- a/registory/registory.go +++ b/registory/registory.go @@ -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 diff --git a/repo/github_release.go b/repo/github_release.go index 77a44d1..4f381d1 100644 --- a/repo/github_release.go +++ b/repo/github_release.go @@ -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 ®istory.CurrentResponse{ ID: time.Now().Format(ISO8601),