From 194654ae4bea85dc0c42feb5cd7af31dd5286efa Mon Sep 17 00:00:00 2001 From: Karthikeyan Chinnakonda Date: Thu, 24 Oct 2024 23:04:49 +0530 Subject: [PATCH] Minor fixes in the connector publishing automation (#359) 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. --- registry-automation/cmd/utils.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/registry-automation/cmd/utils.go b/registry-automation/cmd/utils.go index 178ec188..0d4128ff 100644 --- a/registry-automation/cmd/utils.go +++ b/registry-automation/cmd/utils.go @@ -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 { @@ -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) @@ -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 { @@ -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