Skip to content

Commit

Permalink
Minor fixes in the connector publishing automation (#359)
Browse files Browse the repository at this point in the history
1. Checks the HTTP response status to be 200 OK after downloading the
tar ball.
2. While extracting the tgz file, output the stdout and the stderr in
case of error.
  • Loading branch information
codingkarthik authored Oct 24, 2024
1 parent 037bedd commit 194654a
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions registry-automation/cmd/utils.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
"io"
"net/http"
"os"
"os/exec"

"gopkg.in/yaml.v2"
)

func generateGCPObjectName(namespace, connectorName, version string) string {
Expand All @@ -16,7 +18,15 @@ func generateGCPObjectName(namespace, connectorName, version string) string {

func downloadFile(sourceURL, destination string, headers map[string]string) error {
// Create a new HTTP client
client := &http.Client{}
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// Copy headers to redirected request
for key, values := range headers {
req.Header.Set(key, values)
}
return nil
},
}

// Create a new GET request
req, err := http.NewRequest("GET", sourceURL, nil)
Expand All @@ -36,6 +46,11 @@ func downloadFile(sourceURL, destination string, headers map[string]string) erro
}
defer resp.Body.Close()

// check the response status
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error downloading file: %s. HINT: Make sure that the tarball can be downloaded without any authentication", resp.Status)
}

// Create the destination file
outFile, err := os.Create(destination)
if err != nil {
Expand Down Expand Up @@ -111,12 +126,16 @@ func extractTarGz(src, dest string) (string, error) {
if err := os.MkdirAll(filepath, 0755); err != nil {
return "", fmt.Errorf("error creating destination directory: %v", err)
}
var stdout, stderr bytes.Buffer
// Run the tar command with the -xvzf options
cmd := exec.Command("tar", "-xvzf", src, "-C", dest)
cmd.Stdout = &stdout
cmd.Stderr = &stderr

// Execute the command
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("error extracting tar.gz file: %v", err)
return "", fmt.Errorf("error extracting file:\nerror: %v\nstdout: %s\nstderr: %s",
err, stdout.String(), stderr.String())
}

return fmt.Sprintf("%s/.hasura-connector/connector-metadata.yaml", filepath), nil
Expand Down

0 comments on commit 194654a

Please sign in to comment.