jasmins little space

Determine the Go architecture in a justfile

These days, just is my favorite tool to invoke build scripts, run deploy commands etc. It’s just a binary, the justfile syntax is clean, there’s nothing to hate about it.

Unfortunately, when using a lot of Go-based programs, there’s a slight mismatch between the architecture reported by the arch() function and the architecture used by the Go compiler. As of today, there are about 20 reserved architectures reserved by the Go team. Not all of them are used (anymore).

But if I want to download a Go-based tool, like dbmate for running the database migrations, I either need to hardcode the architecture to amd64 or map from x86_64 to amd64. I would’ve used the hardcoded amd64 value in the past, cause ARM architectures were not as common back then. But, with the increased popularity of ARM for servers and clients, this is not longer suitable.

Therefore, I wrote this handy, just-native ugly if/else-block, which covers the most common architectures:

# Maps arch() to one of the known values inside the syslist.go
# https://github.com/golang/go/blob/3e9876cd3a5a83be9bb0f5cbc600aadf9b599558/src/go/build/syslist.go#L56
go_arch := if arch() =="aarch64" {
    "arm64"
} else if arch() == "arm" {
    "arm"
} else if arch() == "wasm32" {
    "wasm"
} else if arch() == "x86" {
    "386"
} else if arch() == "x86_64" {
    "amd64"
} else { error("unknown architecture {{ os() }}") }

It can be even extended to cover almost all supported architectures:

# Maps arch() to one of the known values inside the syslist.go
# https://github.com/golang/go/blob/3e9876cd3a5a83be9bb0f5cbc600aadf9b599558/src/go/build/syslist.go#L56
go_arch_complete := if arch() =="aarch64" {
    "arm64"
} else if arch() == "arm" {
    "arm"
} else if arch() == "mips" {
    "mips64"
} else if arch() == "powerpc" {
    "ppc"
} else if arch() == "powerpc64" {
    "ppc64"
} else if arch() == "s390x" {
    "s390x"
} else if arch() == "sparc" {
    "sparc64"
} else if arch() == "wasm32" {
    "wasm"
} else if arch() == "x86" {
    "386"
} else if arch() == "x86_64" {
    "amd64"
} else { error("unknown architecture {{ os() }}") }

Now I can use the variable to determine the correct download URL for dbmate, just like this:

install-tools:
    curl -fsSL -o ./bin/dbmate https://github.com/amacneil/dbmate/releases/download/{{ TOOLS_DBMATE_VERSION }}/dbmate-{{ os() }}-{{ go_arch }}
    chmod +x ./bin/dbmate