From 05048bb24b93d707e22f286fa0b5c3df359071a9 Mon Sep 17 00:00:00 2001 From: Faraz Fallahi Date: Fri, 28 Oct 2022 00:49:56 -0400 Subject: [PATCH] support building on windows --- kernel_freebsd.go | 11 +++++++++++ kernel_windows.go | 11 +++++++++++ network_darwin.go | 13 +++++++++++++ network_freebsd.go | 13 +++++++++++++ network.go => network_linux.go | 0 network_windows.go | 13 +++++++++++++ node.go | 10 +++++----- sysinfo.go | 6 ++++++ util.go | 2 +- 9 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 kernel_freebsd.go create mode 100644 kernel_windows.go create mode 100644 network_darwin.go create mode 100644 network_freebsd.go rename network.go => network_linux.go (100%) create mode 100644 network_windows.go diff --git a/kernel_freebsd.go b/kernel_freebsd.go new file mode 100644 index 0000000..3c003dd --- /dev/null +++ b/kernel_freebsd.go @@ -0,0 +1,11 @@ +package sysinfo + +// Kernel information. +type Kernel struct { + Release string `json:"release,omitempty"` + Version string `json:"version,omitempty"` + Architecture string `json:"architecture,omitempty"` +} + +func (si *SysInfo) getKernelInfo() { +} diff --git a/kernel_windows.go b/kernel_windows.go new file mode 100644 index 0000000..3c003dd --- /dev/null +++ b/kernel_windows.go @@ -0,0 +1,11 @@ +package sysinfo + +// Kernel information. +type Kernel struct { + Release string `json:"release,omitempty"` + Version string `json:"version,omitempty"` + Architecture string `json:"architecture,omitempty"` +} + +func (si *SysInfo) getKernelInfo() { +} diff --git a/network_darwin.go b/network_darwin.go new file mode 100644 index 0000000..ecf210a --- /dev/null +++ b/network_darwin.go @@ -0,0 +1,13 @@ +package sysinfo + +// NetworkDevice information. +type NetworkDevice struct { + Name string `json:"name,omitempty"` + Driver string `json:"driver,omitempty"` + MACAddress string `json:"macaddress,omitempty"` + Port string `json:"port,omitempty"` + Speed uint `json:"speed,omitempty"` // device max supported speed in Mbps +} + +func (si *SysInfo) getNetworkInfo() { +} diff --git a/network_freebsd.go b/network_freebsd.go new file mode 100644 index 0000000..ecf210a --- /dev/null +++ b/network_freebsd.go @@ -0,0 +1,13 @@ +package sysinfo + +// NetworkDevice information. +type NetworkDevice struct { + Name string `json:"name,omitempty"` + Driver string `json:"driver,omitempty"` + MACAddress string `json:"macaddress,omitempty"` + Port string `json:"port,omitempty"` + Speed uint `json:"speed,omitempty"` // device max supported speed in Mbps +} + +func (si *SysInfo) getNetworkInfo() { +} diff --git a/network.go b/network_linux.go similarity index 100% rename from network.go rename to network_linux.go diff --git a/network_windows.go b/network_windows.go new file mode 100644 index 0000000..ecf210a --- /dev/null +++ b/network_windows.go @@ -0,0 +1,13 @@ +package sysinfo + +// NetworkDevice information. +type NetworkDevice struct { + Name string `json:"name,omitempty"` + Driver string `json:"driver,omitempty"` + MACAddress string `json:"macaddress,omitempty"` + Port string `json:"port,omitempty"` + Speed uint `json:"speed,omitempty"` // device max supported speed in Mbps +} + +func (si *SysInfo) getNetworkInfo() { +} diff --git a/node.go b/node.go index 480ac0d..a59c8e8 100644 --- a/node.go +++ b/node.go @@ -40,21 +40,21 @@ func (si *SysInfo) getSetMachineID() { } // They both exist, but they don't match! Copy systemd machine id to DBUS machine id. - spewFile(pathDbusMachineID, systemdMachineID, 0444) + spewFile(pathDbusMachineID, systemdMachineID, 0o444) si.Node.MachineID = systemdMachineID return } // Copy DBUS machine id to non-existent systemd machine id. if systemdMachineID == "" && dbusMachineID != "" { - spewFile(pathSystemdMachineID, dbusMachineID, 0444) + spewFile(pathSystemdMachineID, dbusMachineID, 0o444) si.Node.MachineID = dbusMachineID return } // Copy systemd machine id to non-existent DBUS machine id. if systemdMachineID != "" && dbusMachineID == "" { - spewFile(pathDbusMachineID, systemdMachineID, 0444) + spewFile(pathDbusMachineID, systemdMachineID, 0o444) si.Node.MachineID = systemdMachineID return } @@ -68,8 +68,8 @@ func (si *SysInfo) getSetMachineID() { } newMachineID := fmt.Sprintf("%x%x", random, time.Now().Unix()) - spewFile(pathSystemdMachineID, newMachineID, 0444) - spewFile(pathDbusMachineID, newMachineID, 0444) + spewFile(pathSystemdMachineID, newMachineID, 0o444) + spewFile(pathDbusMachineID, newMachineID, 0o444) si.Node.MachineID = newMachineID } diff --git a/sysinfo.go b/sysinfo.go index 30b851d..f56ebb9 100644 --- a/sysinfo.go +++ b/sysinfo.go @@ -5,6 +5,8 @@ // Package sysinfo is a Go library providing Linux OS / kernel / hardware system information. package sysinfo +import "runtime" + // SysInfo struct encapsulates all other information structs. type SysInfo struct { Meta Meta `json:"sysinfo"` @@ -23,6 +25,10 @@ type SysInfo struct { // GetSysInfo gathers all available system information. func (si *SysInfo) GetSysInfo() { + if runtime.GOOS != "linux" { + return + } + // Meta info si.getMetaInfo() diff --git a/util.go b/util.go index cd499a4..3a9ad8b 100644 --- a/util.go +++ b/util.go @@ -21,6 +21,6 @@ func slurpFile(path string) string { } // Write one-liner text files, add newline, ignore errors (best effort). -func spewFile(path string, data string, perm os.FileMode) { +func spewFile(path, data string, perm os.FileMode) { _ = ioutil.WriteFile(path, []byte(data+"\n"), perm) }