...

Source file src/github.com/fergusstrange/embedded-postgres/version_strategy.go

Documentation: github.com/fergusstrange/embedded-postgres

     1  package embeddedpostgres
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"strings"
     8  )
     9  
    10  // VersionStrategy provides a strategy that can be used to determine which version of Postgres should be used based on
    11  // the operating system, architecture and desired Postgres version.
    12  type VersionStrategy func() (operatingSystem string, architecture string, postgresVersion PostgresVersion)
    13  
    14  func defaultVersionStrategy(config Config, goos, arch string, linuxMachineName func() string, shouldUseAlpineLinuxBuild func() bool) VersionStrategy {
    15  	return func() (string, string, PostgresVersion) {
    16  		goos := goos
    17  		arch := arch
    18  
    19  		if goos == "linux" {
    20  			// the zonkyio/embedded-postgres-binaries project produces
    21  			// arm binaries with the following name schema:
    22  			// 32bit: arm32v6 / arm32v7
    23  			// 64bit (aarch64): arm64v8
    24  			if arch == "arm64" {
    25  				arch += "v8"
    26  			} else if arch == "arm" {
    27  				machineName := linuxMachineName()
    28  				if strings.HasPrefix(machineName, "armv7") {
    29  					arch += "32v7"
    30  				} else if strings.HasPrefix(machineName, "armv6") {
    31  					arch += "32v6"
    32  				}
    33  			}
    34  
    35  			if shouldUseAlpineLinuxBuild() {
    36  				arch += "-alpine"
    37  			}
    38  		}
    39  
    40  		// postgres below version 14.2 is not available for macos on arm
    41  		if goos == "darwin" && arch == "arm64" {
    42  			var majorVer, minorVer int
    43  			if _, err := fmt.Sscanf(string(config.version), "%d.%d", &majorVer, &minorVer); err == nil &&
    44  				(majorVer < 14 || (majorVer == 14 && minorVer < 2)) {
    45  				arch = "amd64"
    46  			} else {
    47  				arch += "v8"
    48  			}
    49  		}
    50  
    51  		return goos, arch, config.version
    52  	}
    53  }
    54  
    55  func linuxMachineName() string {
    56  	var uname string
    57  
    58  	if output, err := exec.Command("uname", "-m").Output(); err == nil {
    59  		uname = string(output)
    60  	}
    61  
    62  	return uname
    63  }
    64  
    65  func shouldUseAlpineLinuxBuild() bool {
    66  	_, err := os.Stat("/etc/alpine-release")
    67  	return err == nil
    68  }
    69  

View as plain text