feat: add gitlab source for gitlab package registry

cleanup http source
This commit is contained in:
Technofab 2023-09-25 14:40:39 +02:00
parent 8ffad5a3ea
commit d655269156
No known key found for this signature in database
GPG key ID: A0AA746B951C8830
7 changed files with 180 additions and 70 deletions

View file

@ -3,8 +3,16 @@ package pkg
import (
"archive/tar"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/fatih/color"
"github.com/pkg/errors"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
@ -91,3 +99,64 @@ func GzipUntar(dst string, r io.Reader, subDir string) error {
}
}
}
func CreateTempDir(name, dir, version string) (string, error) {
pkgh := sha256.Sum256([]byte(fmt.Sprintf("jsonnetpkg-%s-%s", strings.Replace(name, "/", "-", -1), strings.Replace(version, "/", "-", -1))))
// using 16 bytes should be a good middle ground between length and collision resistance
tmpDir, err := ioutil.TempDir(filepath.Join(dir, ".tmp"), hex.EncodeToString(pkgh[:16]))
if err != nil {
return "", errors.Wrap(err, "failed to create tmp dir")
}
return tmpDir, nil
}
func DownloadFile(filepath string, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
color.Cyan("GET %s %d", url, resp.StatusCode)
if resp.StatusCode != 200 {
return fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func DownloadAndUntarTo(tmpDir, url, destPath string) error {
filename := filepath.Base(url)
err := DownloadFile(path.Join(tmpDir, filename), url)
if err != nil {
return errors.Wrap(err, "failed to download file")
}
var ar *os.File
ar, err = os.Open(path.Join(tmpDir, filename))
if err != nil {
return errors.Wrap(err, "failed to open downloaded archive")
}
defer ar.Close()
err = GzipUntar(destPath, ar, "")
if err != nil {
return errors.Wrap(err, "failed to unpack downloaded archive")
}
return nil
}