Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Commit

Permalink
move binary downloading into plugin file
Browse files Browse the repository at this point in the history
  • Loading branch information
VJftw committed Aug 2, 2019
1 parent a7c2a59 commit 468f68a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 47 deletions.
47 changes: 0 additions & 47 deletions backend/pkg/velocity/build/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"

"go.uber.org/zap"

"github.com/gosimple/slug"
"github.com/velocity-ci/velocity/backend/pkg/exec"
"github.com/velocity-ci/velocity/backend/pkg/velocity/config"
"github.com/velocity-ci/velocity/backend/pkg/velocity/logging"
)

func getSecrets(params map[string]*Parameter) (r []string) {
Expand Down Expand Up @@ -122,47 +116,6 @@ type BackupResolver interface {
Resolve(paramName string) (string, error)
}

func getBinary(projectRoot, u string, writer io.Writer) (binaryLocation string, _ error) {

parsedURL, err := url.Parse(u)
if err != nil {
return "", err
}

binaryLocation = fmt.Sprintf("%s/.velocityci/plugins/%s", projectRoot, slug.Make(parsedURL.Path))

if _, err := os.Stat(binaryLocation); os.IsNotExist(err) {
logging.GetLogger().Debug("downloading binary", zap.String("from", u), zap.String("to", binaryLocation))
writer.Write([]byte(fmt.Sprintf("Downloading binary: %s", parsedURL.String())))
outFile, err := os.Create(binaryLocation)
if err != nil {
return "", err
}
defer outFile.Close()
resp, err := http.Get(u)
if err != nil {
return "", err
}
defer resp.Body.Close()

size, err := io.Copy(outFile, resp.Body)
if err != nil {
return "", err
}
writer.Write([]byte(fmt.Sprintf(
"Downloaded binary: %s to %s. %d bytes",
parsedURL.String(),
binaryLocation,
size,
)))

logging.GetLogger().Debug("downloaded binary", zap.String("from", u), zap.String("to", binaryLocation), zap.Int64("bytes", size))
outFile.Chmod(os.ModePerm)
}

return binaryLocation, nil
}

func getExportedParameterName(pMapping map[string]string, exportedParam string) string {
if val, ok := pMapping[exportedParam]; ok {
return val
Expand Down
54 changes: 54 additions & 0 deletions backend/pkg/velocity/build/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package build

import (
"fmt"
"io"
"net/http"
"net/url"
"os"

"github.com/gosimple/slug"
"github.com/velocity-ci/velocity/backend/pkg/velocity/logging"
"go.uber.org/zap"
)

func getBinary(projectRoot, u string, writer io.Writer) (binaryLocation string, _ error) {

parsedURL, err := url.Parse(u)
if err != nil {
return "", err
}

binaryLocation = fmt.Sprintf("%s/.velocityci/plugins/%s", projectRoot, slug.Make(parsedURL.Path))

if _, err := os.Stat(binaryLocation); os.IsNotExist(err) {
logging.GetLogger().Debug("downloading binary", zap.String("from", u), zap.String("to", binaryLocation))
writer.Write([]byte(fmt.Sprintf("Downloading binary: %s", parsedURL.String())))
outFile, err := os.Create(binaryLocation)
if err != nil {
return "", err
}
defer outFile.Close()
resp, err := http.Get(u)
if err != nil {
return "", err
}
defer resp.Body.Close()

size, err := io.Copy(outFile, resp.Body)
if err != nil {
return "", err
}
writer.Write([]byte(fmt.Sprintf(
"Downloaded binary: %s to %s. %d bytes",
parsedURL.String(),
binaryLocation,
size,
)))

logging.GetLogger().Debug("downloaded binary", zap.String("from", u), zap.String("to", binaryLocation), zap.Int64("bytes", size))
outFile.Chmod(os.ModePerm)
}

return binaryLocation, nil
}

0 comments on commit 468f68a

Please sign in to comment.