From ded8e88dd1f003775400b9e4baa1feb92da9875d Mon Sep 17 00:00:00 2001 From: yseto Date: Wed, 6 Jan 2021 15:24:40 +0900 Subject: [PATCH 1/2] Support collect assets under specified directories --- cmd/mackerel-github-release/main.go | 33 ++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/cmd/mackerel-github-release/main.go b/cmd/mackerel-github-release/main.go index 1afbf42..1b961b4 100644 --- a/cmd/mackerel-github-release/main.go +++ b/cmd/mackerel-github-release/main.go @@ -69,7 +69,7 @@ func run(argv []string) int { return exitError } log.Printf("Start uploading files to GitHub Releases. version: %s, staging: %t, dry-run: %t\n", v.Version, *staging, *dryRun) - err = uploadToGithubRelease(proj, v.Version, *staging, *dryRun) + err = uploadToGithubRelease(proj, v.Version, *staging, *dryRun, fs.Args()) if err != nil { log.Printf("error occured while uploading artifacts to github: %+v\n", err) return exitError @@ -79,7 +79,7 @@ func run(argv []string) int { var errAlreadyReleased = fmt.Errorf("the release of this version has already existed at GitHub Releases, so skip the process") -func uploadToGithubRelease(proj *github.Project, releaseVer string, staging, dryRun bool) error { +func uploadToGithubRelease(proj *github.Project, releaseVer string, staging, dryRun bool, directories []string) error { tag := "staging" if !staging { tag = "v" + releaseVer @@ -102,7 +102,13 @@ func uploadToGithubRelease(proj *github.Project, releaseVer string, staging, dry } body := pr.Body - assets, err := collectAssets() + + var assets []string + if len(directories) > 0 { + assets, err = specifiedCollectAssets(directories) + } else { + assets, err = collectAssets() + } if err != nil { return fmt.Errorf("error occured while collecting releasing assets: %w", err) } @@ -243,6 +249,27 @@ func collectAssets() (assets []string, err error) { return assets, nil } +func specifiedCollectAssets(directories []string) (assets []string, err error) { + for _, dir := range directories { + if _, err := os.Stat(dir); err == nil { + errWalk := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + assets = append(assets, path) + return nil + }) + if errWalk != nil { + return nil, errWalk + } + } + } + return assets, nil +} + func uploadAssets(gh *github.Client, release *github.Release, assets []string) error { for _, asset := range assets { err := retry.Retry(3, 3*time.Second, func() error { From 72bf62e7bc68ff60da87719e397dd808ff8c6b60 Mon Sep 17 00:00:00 2001 From: yseto Date: Wed, 6 Jan 2021 16:34:56 +0900 Subject: [PATCH 2/2] Fix It was supposed to ignore the existence of the directory --- cmd/mackerel-github-release/main.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/cmd/mackerel-github-release/main.go b/cmd/mackerel-github-release/main.go index 1b961b4..118841d 100644 --- a/cmd/mackerel-github-release/main.go +++ b/cmd/mackerel-github-release/main.go @@ -251,20 +251,18 @@ func collectAssets() (assets []string, err error) { func specifiedCollectAssets(directories []string) (assets []string, err error) { for _, dir := range directories { - if _, err := os.Stat(dir); err == nil { - errWalk := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if info.IsDir() { - return nil - } - assets = append(assets, path) + errWalk := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { return nil - }) - if errWalk != nil { - return nil, errWalk } + assets = append(assets, path) + return nil + }) + if errWalk != nil { + return nil, errWalk } } return assets, nil