Refactor package CLI command (#1345)

This commit is contained in:
Ta-Ching Chen
2019-10-13 01:30:20 +08:00
committed by GitHub
parent f30e7df43d
commit e42f81a7a1
22 changed files with 1769 additions and 1172 deletions
+3 -25
View File
@@ -19,8 +19,6 @@ package fetcher
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
@@ -47,6 +45,7 @@ import (
"github.com/fission/fission/pkg/info"
storageSvcClient "github.com/fission/fission/pkg/storagesvc/client"
"github.com/fission/fission/pkg/types"
"github.com/fission/fission/pkg/utils"
)
type (
@@ -129,27 +128,6 @@ func downloadUrl(ctx context.Context, httpClient *http.Client, url string, local
return nil
}
func getChecksum(path string) (*fv1.Checksum, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
hasher := sha256.New()
_, err = io.Copy(hasher, f)
if err != nil {
return nil, err
}
c := hex.EncodeToString(hasher.Sum(nil))
return &fv1.Checksum{
Type: fv1.ChecksumTypeSHA256,
Sum: c,
}, nil
}
func verifyChecksum(fileChecksum, checksum *fv1.Checksum) error {
if checksum.Type != fv1.ChecksumTypeSHA256 {
return ferror.MakeError(ferror.ErrorInvalidArgument, "Unsupported checksum type")
@@ -328,7 +306,7 @@ func (fetcher *Fetcher) Fetch(ctx context.Context, pkg *fv1.Package, req types.F
return http.StatusBadRequest, errors.Wrapf(err, "%s %s", e, req.Url)
}
checksum, err := getChecksum(tmpPath)
checksum, err := utils.FileChecksum(tmpPath)
if err != nil {
e := "failed to get checksum"
fetcher.logger.Error(e, zap.Error(err))
@@ -534,7 +512,7 @@ func (fetcher *Fetcher) UploadHandler(w http.ResponseWriter, r *http.Request) {
return
}
sum, err := getChecksum(dstFilepath)
sum, err := utils.FileChecksum(dstFilepath)
if err != nil {
e := "error calculating checksum of zip file"
fetcher.logger.Error(e, zap.Error(err), zap.String("file", dstFilepath))