Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptnull committed Oct 30, 2024
1 parent 0cbd51f commit f7e5acf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
16 changes: 13 additions & 3 deletions internal/asset/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ const (
)

var (
IndexJsonPath = filepath.Join(AssetFolderPath, "index.json")
DownloadsFolderPath = filepath.Join(AssetFolderPath, "downloads")
ExtractsFolderPath = filepath.Join(AssetFolderPath, "extracts")
IndexJsonPath = filepath.Join(AssetFolderPath, "index.json")
)

func VersionFolder(namespace, name, version string) string {
return filepath.Join(AssetFolderPath, namespace, name, version)
func ConnectorVersionFolderForDownload(namespace, name, version string) string {
return filepath.Join(DownloadsFolderPath, namespace, name, version)
}

func ConnectorTarballDownloadPath(namespace, name, version string) string {
return filepath.Join(ConnectorVersionFolderForDownload(namespace, name, version), "connector-definition.tar.gz")
}

func ConnectorVersionFolderForExtracting(namespace, name, version string) string {
return filepath.Join(ExtractsFolderPath, namespace, name, version)
}

type Index struct {
Expand Down
45 changes: 43 additions & 2 deletions internal/ndchub/ndc_hub.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ndchub

import (
"archive/tar"
"compress/gzip"
"crypto/sha256"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -71,15 +73,15 @@ func GetConnectorPackaging(path string) (*ConnectorPackaging, error) {
func DownloadConnectorTarballs(connPkgs []ConnectorPackaging) error {
var connectorTarball errgroup.Group
for _, cp := range connPkgs {
versionFolder := asset.VersionFolder(cp.Namespace, cp.Name, cp.Version)
versionFolder := asset.ConnectorVersionFolderForDownload(cp.Namespace, cp.Name, cp.Version)
err := os.MkdirAll(versionFolder, 0777)
if err != nil {
return fmt.Errorf("error creating folder: %s %w", versionFolder, err)
}

connectorTarball.Go(func() error {
var err error
tarballPath := filepath.Join(versionFolder, "connector-definition.tar.gz")
tarballPath := asset.ConnectorTarballDownloadPath(cp.Namespace, cp.Name, cp.Version)

sha, _ := getSHAIfFileExists(tarballPath)
if sha == cp.Checksum.Value {
Expand Down Expand Up @@ -124,6 +126,45 @@ func DownloadConnectorTarballs(connPkgs []ConnectorPackaging) error {
return connectorTarball.Wait()
}

func ExtractConnectorTarballs(connPkgs []ConnectorPackaging) error {
var extract errgroup.Group
for _, cp := range connPkgs {
extract.Go(func() error {
srcTarball := asset.ConnectorTarballDownloadPath(cp.Namespace, cp.Name, cp.Version)

file, err := os.Open(srcTarball)
if err != nil {
return fmt.Errorf("could not open file: %v", err)
}
defer file.Close()

gzReader, err := gzip.NewReader(file)
if err != nil {
return fmt.Errorf("could not create gzip reader: %v", err)
}
defer gzReader.Close()

tarReader := tar.NewReader(gzReader)

for {
header, err := tarReader.Next()
if err == io.EOF {
break // end of archive
}
if err != nil {
return fmt.Errorf("could not read tar header: %v", err)
}

// TODO: make use of the header
fmt.Println(header)
}

return nil
})
}
return extract.Wait()
}

func getSHAIfFileExists(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
Expand Down

0 comments on commit f7e5acf

Please sign in to comment.