in wrappers/golang/pgadapter.go [459:540]
func downloadAndUnpackJar(ctx context.Context, config Config) (string, error) {
ds := config.ExecutionEnvironment.(*Java).DownloadSettings
var err error
var dst string
if ds.DownloadLocation == "" {
dst, err = os.UserCacheDir()
if err != nil {
return "", err
}
dst = filepath.Join(dst, "pgadapter-downloads")
} else {
dst = ds.DownloadLocation
}
if !ds.DisableAutomaticDownload {
if config.Version == "" {
dst = filepath.Join(dst, "pgadapter-latest")
} else {
dst = filepath.Join(dst, fmt.Sprintf("pgadapter-%s", config.Version))
}
if err := os.MkdirAll(dst, 0755); err != nil {
return "", err
}
}
jarFi, jarErr := os.Stat(filepath.Join(dst, "pgadapter.jar"))
libFi, libErr := os.Stat(filepath.Join(dst, "lib"))
// If automatic downloads are disabled, then PGAdapter must already exist in the given location.
if ds.DisableAutomaticDownload {
if jarErr != nil {
return "", jarErr
}
if libErr != nil {
return "", libErr
}
// All seems OK, just return the download location.
return ds.DownloadLocation, nil
}
var url string
if config.Version == "" {
url = jarBaseDownloadUrl + latestJarFileName
} else {
url = jarBaseDownloadUrl + fmt.Sprintf(versionedJarFileName, config.Version)
}
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
var lastModified time.Time
lastModifiedString := resp.Header.Get("Last-Modified")
if lastModifiedString != "" {
lastModified, _ = http.ParseTime(lastModifiedString)
}
// Check if we need to download PGAdapter.
if jarErr == nil && libErr == nil {
// Both files seem to exist. Check the file info.
if jarFi != nil && libFi != nil {
// Check the types of the files and the last modified date.
if libFi.IsDir() && !jarFi.IsDir() && libFi.ModTime().After(lastModified) && jarFi.ModTime().After(lastModified) {
// Both the pgadapter.jar file and the lib dir exist and are up-to-date. Use these.
return dst, nil
}
}
}
tarGzFile := filepath.Join(dst, "pgadapter.tar.gz")
out, err := os.Create(tarGzFile)
if err != nil {
return "", err
}
defer out.Close()
if _, err := io.Copy(out, resp.Body); err != nil {
return "", err
}
if err := unpackJar(ctx, tarGzFile); err != nil {
return "", err
}
return dst, err
}