adding checksum options

This commit is contained in:
jackspirou 2016-01-15 10:46:00 -06:00
parent 58efb17e13
commit e79e7f293b
7 changed files with 96 additions and 5 deletions

View File

@ -1 +1 @@
eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhHQ00ifQ.R-s00gRELUNOzcRuOcoVhqltGWZKP3xQyM-WCqyszETHKr2OVBtN6dfYEEYv1jB5a_4SUs3WDJsjdXieeQsL5sWiDwSzS8cjCeLF3J0XjgmwaPL2EiJn78yHcSOfV-YhHL6fM2zdxKKKgATlKLPGyi6p-O2saL-knl3gnKnO13-ZDWoVIsYVyklFH8UscVEwI4kthGP6rFGLZon46HePvrUxdBcczBZmuqQLGxXmFBr93cX2eJ6QK8K4j6pZwtC5q8ZcrnXwwOoJW_UjZugKuzEHcS_DX_59vb5oJ1MdWhdb8Sm5MBs7nYjRpx900KZzqxG8jzQYuo5PsH4r819GeQ.1BShEZlbixdfZWJB.PuIVYaAJEsbvhqcqTQD7FLQvFDscDvQLorCq7-Gh0LoZBO-5N3XIJVtcEtetW2Dqs6srNhYwEDqryqVp7O3MHlesSuPNy8ulGjCziuqPa5qmCI2ds6gv19RAtXpzot-UobDERID0W9CfL4ZeaiW1AKQmxB52jgNnmTZ3Gqbad4ZPIKBu5BvkIPtAOmbWu7I2SKFvyp8PzD9BMfH_k-Y3_y1f0hrvFYIcl3iuL7z0LW0OAot3aLfeGSuct65Zp5qZuXdUCTCDqYInNPjIcMczZkh3GOKtSm2grRKeEE3ub1_Kmyiw_F8CWGdlYXtOXM47Y9wxmQsKuQFT1WffUW3qdJCXTMvg3ILXJ5AzeuLcX1UkD4cPYg0AfqEEaLAee_0Btfoxpg.Ai8KGIEtc3W1h_vTAZahgg
eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhHQ00ifQ.c6isPvujaqNjwxAqXRITLL1l6wgPA-MwKho1OvVLdaQEFeDmhrgqJu8_bVnBrDmFxwFbAc2B7_E7MrhG1q29hvB2GETC2oufCn19n9btwX9zrgQu_M0sAFRAfd7caBRtlEStl2sbAZDIPruwEcIgx1aMNhVUjR4vYhv7kdZ3xIa0zfJJ4zKjaqA2DgtOYP9m_fQgIeyheqryV8P6AZW8l2f2ZUwjpgr3DY4vg8pPpnSJoQKTSjuzV2udNzcCMgNxV9Uu0MpCKP8gABfniUR-A8V2BWhWAXFWhl6i6msarD-ugnIyyUELu7dbagwy6aMWIk5hu4RmYroE6CmfF7I4Bg.NUqxC8J2VPLEH4Fo.Ie-fea-F1Ih185vnwBEyy0pe60cWath-4OOJcb2NHcUSIjN5aD8w6Gads-dUpjdU6uQwDaNJHB6vG5xPGTiy059zKampcJtFWe1lhy4J63l03TkY6LcgDRCot5eml06X69yRv-z1YmAuHvMMJfDATo2oFVNhXgC9CF8zzcScpIJSTKbe5suoSxzLSUNhz94vwgG7hCKZ.Z_R67Wzh1KoOn__PA0Z-VA

View File

@ -7,10 +7,10 @@ build:
publish:
docker:
username: drone
username: jackspirou
password: $$DOCKER_PASS
email: $$DOCKER_EMAIL
repo: plugins/drone-github-release
repo: jackspirou/drone-github-release
when:
branch: master
@ -18,7 +18,7 @@ plugin:
name: GitHub Release
desc: Publishs files and artifacts to GitHub Releases
type: publish
image: plugins/drone-github-release
image: jackspirou/drone-github-release
labels:
- github
- release

1
.gitignore vendored
View File

@ -24,3 +24,4 @@ _testmain.go
*.prof
drone-github-release
secrets.yml

View File

@ -22,4 +22,4 @@ build:
go build -ldflags '-s -w $(LDFLAGS)'
docker:
docker build --rm=true -t plugins/drone-github-release .
docker build --rm=true -t jackspirou/drone-github-release .

38
checksum.go Normal file
View File

@ -0,0 +1,38 @@
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash/adler32"
"hash/crc32"
"io"
"io/ioutil"
"strconv"
)
func checksum(r io.Reader, method string) (string, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}
switch method {
case "md5":
return fmt.Sprintf("%x", md5.Sum(b)), nil
case "sha1":
return fmt.Sprintf("%x", sha1.Sum(b)), nil
case "sha256":
return fmt.Sprintf("%x", sha256.Sum256(b)), nil
case "sha512":
return fmt.Sprintf("%x", sha512.Sum512(b)), nil
case "adler32":
return strconv.FormatUint(uint64(adler32.Checksum(b)), 10), nil
case "crc32":
return strconv.FormatUint(uint64(crc32.ChecksumIEEE(b)), 10), nil
default:
return "", fmt.Errorf("hashing method %s is not supported", method)
}
}

51
main.go
View File

@ -71,6 +71,15 @@ func main() {
}
}
if len(vargs.Checksums) > 0 {
var err error
files, err = writeChecksums(files, vargs.Checksums)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
baseURL, err := url.Parse(vargs.BaseURL)
if err != nil {
fmt.Printf("Failed to parse base URL\n")
@ -173,3 +182,45 @@ func uploadFiles(client *github.Client, owner string, repo string, id int, files
return nil
}
func writeChecksums(files, methods []string) ([]string, error) {
checksums := make(map[string][]string)
for _, method := range methods {
for _, file := range files {
handle, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("Failed to read %s artifact: %s", file, err)
}
hash, err := checksum(handle, method)
if err != nil {
return nil, err
}
fmt.Println(file)
fmt.Println(method)
fmt.Println(hash)
checksums[method] = append(checksums[method], hash, file)
}
}
for method, results := range checksums {
filename := method + "sum.txt"
f, err := os.Create(filename)
if err != nil {
return nil, err
}
for i := 0; i < len(results); i += 2 {
hash := results[i]
file := results[i+1]
if _, err := f.WriteString(fmt.Sprintf("%s %s\n", hash, file)); err != nil {
return nil, err
}
}
files = append(files, filename)
}
return files, nil
}

View File

@ -6,4 +6,5 @@ type Params struct {
UploadURL string `json:"upload_url"`
APIKey string `json:"api_key"`
Files []string `json:"files"`
Checksums []string `json:"checksums"`
}