Skip to content

Commit

Permalink
feat: script support read md5 from url (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 authored Jun 6, 2022
1 parent 3e14bcc commit 7a2534f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

a.out*
zz_*
/_build
/_build*
*.dylib
*.lock
.kclvm
8 changes: 4 additions & 4 deletions scripts/kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ var (

KclvmMd5sum = map[string]string{
// https://github.com/KusionStack/KCLVM/releases/tag/v0.4.2-alpha.4
"kclvm-v0.4.2-alpha.4-Darwin.tar.gz": "5e713e4275d45da971a9b6aacc156074",
"kclvm-v0.4.2-alpha.4-Darwin-arm64.tar.gz": "40ea8f768975d2d59946b2cd38957932",
"kclvm-v0.4.2-alpha.4-centos.tar.gz": "afe52170ccd3b01ffefa48b73ac655d1",
"kclvm-v0.4.2-alpha.4-ubuntu.tar.gz": "a4a7fd7ced93cfb081de3da1207ce641",
"kclvm-v0.4.2-alpha.4-Darwin.tar.gz": "", // read from *.md5.txt
"kclvm-v0.4.2-alpha.4-Darwin-arm64.tar.gz": "", // read from *.md5.txt
"kclvm-v0.4.2-alpha.4-centos.tar.gz": "", // read from *.md5.txt
"kclvm-v0.4.2-alpha.4-ubuntu.tar.gz": "", // read from *.md5.txt

// https://github.com/KusionStack/KCLVM/releases/tag/v0.4.2-alpha.3
"kclvm-v0.4.2-alpha.3-Darwin.tar.gz": "9727d804b49f225682af9b7383c0ab6a",
Expand Down
28 changes: 28 additions & 0 deletions scripts/kclvm_asset_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,47 @@ func (p *KclvmAssetHelper) GetFilename() string {

func (p *KclvmAssetHelper) GetFileMd5um() string {
kclvmFilename := p.GetFilename()
md5OrUrl := KclvmMd5sum[kclvmFilename]
if IsMd5Text(md5OrUrl) {
return md5OrUrl
}
if strings.HasSuffix(md5OrUrl, ".md5.txt") {
if !strings.HasPrefix(md5OrUrl, "http") {
md5OrUrl = p.GetDownloadMd5Url(KclvmDownloadUrlBase)
}
if data, err := HttpGetData(nil, md5OrUrl, true); err == nil {
if s := strings.TrimSpace(string(data)); IsMd5Text(s) {
return s
}
}
}
if md5OrUrl == "" {
md5OrUrl = p.GetDownloadMd5Url(KclvmDownloadUrlBase)
if data, err := HttpGetData(nil, md5OrUrl, true); err == nil {
if s := strings.TrimSpace(string(data)); IsMd5Text(s) {
return s
}
}
}

return KclvmMd5sum[kclvmFilename]
}

func (p *KclvmAssetHelper) GetDownloadUrl(baseUrl string) string {
baseUrl = strings.TrimRight(baseUrl, "/")
return fmt.Sprintf("%s/%s/%s", baseUrl, p.Version, p.GetFilename())
}
func (p *KclvmAssetHelper) GetDownloadMd5Url(baseUrl string) string {
baseUrl = strings.TrimRight(baseUrl, "/")
return fmt.Sprintf("%s/%s/%s.md5.txt", baseUrl, p.Version, p.GetFilename())
}

func (p *KclvmAssetHelper) DownloadFile(localFilename string) error {
md5sum := p.GetFileMd5um()
if md5sum == "" {
return fmt.Errorf("%s: not found, md5sum missing", p.GetFilename())
}

if MD5File(localFilename) == md5sum {
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions scripts/util_md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ import (
"fmt"
"io"
"os"
"regexp"
"strings"
)

func IsMd5Text(s string) bool {
s = strings.TrimSpace(s)
matched, err := regexp.MatchString(`^[a-f0-9]{32}$`, s)
if err != nil {
panic(err)
}
return matched
}

func MD5File(filename string) string {
f, err := os.Open(filename)
if err != nil {
Expand Down

0 comments on commit 7a2534f

Please sign in to comment.