Add pkg/parsers/architecture and pkg/platform
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
parent
6dab26120b
commit
4ee0608373
4 changed files with 118 additions and 0 deletions
15
platform/architecture_freebsd.go
Normal file
15
platform/architecture_freebsd.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package platform
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …)
|
||||
func GetRuntimeArchitecture() (string, error) {
|
||||
cmd := exec.Command("uname", "-m")
|
||||
machine, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(machine), nil
|
||||
}
|
28
platform/architecture_linux.go
Normal file
28
platform/architecture_linux.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Package platform provides helper function to get the runtime architecture
|
||||
// for different platforms.
|
||||
package platform
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …)
|
||||
func GetRuntimeArchitecture() (string, error) {
|
||||
utsname := &syscall.Utsname{}
|
||||
if err := syscall.Uname(utsname); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return charsToString(utsname.Machine), nil
|
||||
}
|
||||
|
||||
func charsToString(ca [65]int8) string {
|
||||
s := make([]byte, len(ca))
|
||||
var lens int
|
||||
for ; lens < len(ca); lens++ {
|
||||
if ca[lens] == 0 {
|
||||
break
|
||||
}
|
||||
s[lens] = uint8(ca[lens])
|
||||
}
|
||||
return string(s[0:lens])
|
||||
}
|
52
platform/architecture_windows.go
Normal file
52
platform/architecture_windows.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package platform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
procGetSystemInfo = modkernel32.NewProc("GetSystemInfo")
|
||||
)
|
||||
|
||||
// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx
|
||||
type systeminfo struct {
|
||||
wProcessorArchitecture uint16
|
||||
wReserved uint16
|
||||
dwPageSize uint32
|
||||
lpMinimumApplicationAddress uintptr
|
||||
lpMaximumApplicationAddress uintptr
|
||||
dwActiveProcessorMask uintptr
|
||||
dwNumberOfProcessors uint32
|
||||
dwProcessorType uint32
|
||||
dwAllocationGranularity uint32
|
||||
wProcessorLevel uint16
|
||||
wProcessorRevision uint16
|
||||
}
|
||||
|
||||
// Constants
|
||||
const (
|
||||
ProcessorArchitecture64 = 9 // PROCESSOR_ARCHITECTURE_AMD64
|
||||
ProcessorArchitectureIA64 = 6 // PROCESSOR_ARCHITECTURE_IA64
|
||||
ProcessorArchitecture32 = 0 // PROCESSOR_ARCHITECTURE_INTEL
|
||||
ProcessorArchitectureArm = 5 // PROCESSOR_ARCHITECTURE_ARM
|
||||
)
|
||||
|
||||
var sysinfo systeminfo
|
||||
|
||||
// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …)
|
||||
func GetRuntimeArchitecture() (string, error) {
|
||||
syscall.Syscall(procGetSystemInfo.Addr(), 1, uintptr(unsafe.Pointer(&sysinfo)), 0, 0)
|
||||
switch sysinfo.wProcessorArchitecture {
|
||||
case ProcessorArchitecture64, ProcessorArchitectureIA64:
|
||||
return "x86_64", nil
|
||||
case ProcessorArchitecture32:
|
||||
return "i686", nil
|
||||
case ProcessorArchitectureArm:
|
||||
return "arm", nil
|
||||
default:
|
||||
return "", fmt.Errorf("Unknown processor architecture")
|
||||
}
|
||||
}
|
23
platform/platform.go
Normal file
23
platform/platform.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package platform
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
// Architecture holds the runtime architecture of the process.
|
||||
Architecture string
|
||||
// OSType holds the runtime operating system type (Linux, …) of the process.
|
||||
OSType string
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
Architecture, err = GetRuntimeArchitecture()
|
||||
if err != nil {
|
||||
logrus.Errorf("Could no read system architecture info: %v", err)
|
||||
}
|
||||
OSType = runtime.GOOS
|
||||
}
|
Loading…
Reference in a new issue