localrelease.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strings"
  11. "time"
  12. )
  13. type releaseLocalSources struct {
  14. path string
  15. artifactConfigurations []buildArtifact
  16. }
  17. func (r releaseLocalSources) prepareRelease(baseArchiveUrl, whatsNewUrl string, releaseNotesUrl string, nightly bool) (*release, error) {
  18. buildData := r.findBuilds(baseArchiveUrl)
  19. rel := release{
  20. Version: buildData.version,
  21. ReleaseDate: time.Now().UTC(),
  22. Stable: false,
  23. Beta: false,
  24. Nightly: nightly,
  25. WhatsNewUrl: whatsNewUrl,
  26. ReleaseNotesUrl: releaseNotesUrl,
  27. Builds: buildData.builds,
  28. }
  29. return &rel, nil
  30. }
  31. type buildData struct {
  32. version string
  33. builds []build
  34. }
  35. func (r releaseLocalSources) findBuilds(baseArchiveUrl string) buildData {
  36. data := buildData{}
  37. filepath.Walk(r.path, createBuildWalker(r.path, &data, r.artifactConfigurations, baseArchiveUrl))
  38. return data
  39. }
  40. func createBuildWalker(path string, data *buildData, archiveTypes []buildArtifact, baseArchiveUrl string) func(path string, f os.FileInfo, err error) error {
  41. return func(path string, f os.FileInfo, err error) error {
  42. if err != nil {
  43. log.Printf("error: %v", err)
  44. }
  45. if f.Name() == path || strings.HasSuffix(f.Name(), ".sha256") {
  46. return nil
  47. }
  48. for _, archive := range archiveTypes {
  49. if strings.HasSuffix(f.Name(), archive.urlPostfix) {
  50. shaBytes, err := ioutil.ReadFile(path + ".sha256")
  51. if err != nil {
  52. log.Fatalf("Failed to read sha256 file %v", err)
  53. }
  54. version, err := grabVersion(f.Name(), archive.urlPostfix)
  55. if err != nil {
  56. log.Println(err)
  57. continue
  58. }
  59. data.version = version
  60. data.builds = append(data.builds, build{
  61. Os: archive.os,
  62. Url: archive.getUrl(baseArchiveUrl, version, false),
  63. Sha256: string(shaBytes),
  64. Arch: archive.arch,
  65. })
  66. return nil
  67. }
  68. }
  69. return nil
  70. }
  71. }
  72. func grabVersion(name string, suffix string) (string, error) {
  73. match := regexp.MustCompile(fmt.Sprintf(`grafana(-enterprise)?[-_](.*)%s`, suffix)).FindSubmatch([]byte(name))
  74. if len(match) > 0 {
  75. return string(match[2]), nil
  76. }
  77. return "", errors.New("No version found.")
  78. }