2019-06-15 23:03:27 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
|
|
import (
|
2019-07-07 23:42:52 +02:00
|
|
|
"archive/zip"
|
2019-06-15 23:03:27 +02:00
|
|
|
"context"
|
2019-06-17 22:30:17 +02:00
|
|
|
"fmt"
|
2019-07-07 23:42:52 +02:00
|
|
|
"io"
|
2019-06-15 23:03:27 +02:00
|
|
|
"net/http"
|
2019-06-17 22:30:17 +02:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
2019-06-15 23:03:27 +02:00
|
|
|
|
2019-06-17 22:45:49 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-06-15 23:03:27 +02:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//CreateBearerHTTPClient with given token
|
|
|
|
|
func CreateBearerHTTPClient(ctx context.Context, token string) *http.Client {
|
|
|
|
|
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{
|
|
|
|
|
AccessToken: token},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
client := oauth2.NewClient(ctx, tokenSource)
|
|
|
|
|
|
|
|
|
|
return client
|
|
|
|
|
}
|
2019-06-17 22:30:17 +02:00
|
|
|
|
|
|
|
|
// GetAccessToken lookup for the providers accesstoken
|
|
|
|
|
func GetAccessToken(providerName string) (string, error) {
|
|
|
|
|
var token string
|
|
|
|
|
var exists bool
|
|
|
|
|
envName := fmt.Sprintf("%s_ACCESS_TOKEN", strings.ToUpper(providerName))
|
|
|
|
|
|
2019-06-17 22:45:49 +02:00
|
|
|
log.Debugf("check if %s environment variable is set", envName)
|
|
|
|
|
|
2019-06-17 22:30:17 +02:00
|
|
|
if token, exists = os.LookupEnv(envName); !exists {
|
2019-06-18 21:35:51 +02:00
|
|
|
return "", fmt.Errorf("could not find %s in the enviroment variables. Please check if it is set", envName)
|
2019-06-17 22:30:17 +02:00
|
|
|
}
|
|
|
|
|
return token, nil
|
|
|
|
|
}
|
2019-07-07 23:42:52 +02:00
|
|
|
|
|
|
|
|
// ZipFile compress given file in zip format
|
|
|
|
|
func ZipFile(repository string, file string) (string, error) {
|
|
|
|
|
|
|
|
|
|
zipFileName := fmt.Sprintf("%s/%s", strings.TrimSuffix(repository, "/"), file)
|
|
|
|
|
zipFile, err := os.Create(zipFileName)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer zipFile.Close()
|
|
|
|
|
|
|
|
|
|
fileToZip, err := os.Open(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
defer fileToZip.Close()
|
|
|
|
|
|
|
|
|
|
fileToZipInfo, err := fileToZip.Stat()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
zipWriter := zip.NewWriter(zipFile)
|
|
|
|
|
|
|
|
|
|
fileToZipHeader, err := zip.FileInfoHeader(fileToZipInfo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileToZipHeader.Name = fileToZipInfo.Name()
|
|
|
|
|
|
|
|
|
|
fileToZipWriter, err := zipWriter.CreateHeader(fileToZipHeader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err = io.Copy(fileToZipWriter, fileToZip); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return zipFileName, nil
|
|
|
|
|
}
|