Skip to content

Commit

Permalink
Add an arch package
Browse files Browse the repository at this point in the history
Many users of this package will want to translate between
Go and RPM architectures.  This moves code that exists
in coreos-assembler (and can then be deleted from there):
https://github.com/coreos/coreos-assembler/blob/master/mantle/system/arch.go
  • Loading branch information
cgwalters committed Jan 21, 2021
1 parent 7575c72 commit 882e268
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PKGS := release stream fedoracoreos
PKGS := arch release stream fedoracoreos

build:
for pkg in $(PKGS); do (cd $$pkg && go build -mod=vendor); done
Expand Down
48 changes: 48 additions & 0 deletions arch/arch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// package arch contains mappings between the Golang architecture and
// the RPM architecture used by Fedora CoreOS and derivatives.
package arch

import "runtime"

type mapping struct {
rpmArch string
goArch string
}

// If an architecture isn't defined here, we assume it's
// pass through.
var translations = []mapping{
mapping{
rpmArch: "x86_64",
goArch: "amd64",
},
mapping{
rpmArch: "aarch64",
goArch: "arm64",
},
}

// CurrentRpmArch returns the current architecture in RPM terms.
func CurrentRpmArch() string {
return RpmArch(runtime.GOARCH)
}

// RpmArch translates a Go architecture to RPM.
func RpmArch(goarch string) string {
for _, m := range translations {
if m.goArch == goarch {
return m.rpmArch
}
}
return goarch
}

// GoArch translates an RPM architecture to Go.
func GoArch(rpmarch string) string {
for _, m := range translations {
if m.rpmArch == rpmarch {
return m.goArch
}
}
return rpmarch
}
19 changes: 19 additions & 0 deletions arch/arch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// package arch contains mappings between the Golang architecture and
// the RPM architecture used by Fedora CoreOS and derivatives.
package arch

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"
)

func testMapping(t *testing.T) {
// Validate bidirectional mapping for current architecture
assert.Equal(t, GoArch(CurrentRpmArch()), runtime.GOARCH)

assert.Equal(t, GoArch("x86_64"), "amd64")
assert.Equal(t, GoArch("aarch64"), "arm64")
assert.Equal(t, GoArch("ppc64le"), "ppc64le")
}

0 comments on commit 882e268

Please sign in to comment.