From ace810817b99de2942e9ca2f746fc5a1860bf406 Mon Sep 17 00:00:00 2001 From: kruskal <99559985+kruskall@users.noreply.github.com> Date: Thu, 8 Sep 2022 23:11:04 +0200 Subject: [PATCH] feat: implement darwing boottime without using cgo Use sysctl from the stdlib and add a test to make sure the implementation works with CGO_ENABLED=0 --- providers/darwin/boottime_darwin.go | 11 +++++----- providers/darwin/boottime_test.go | 32 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 providers/darwin/boottime_test.go diff --git a/providers/darwin/boottime_darwin.go b/providers/darwin/boottime_darwin.go index c1eda1a6..989c7080 100644 --- a/providers/darwin/boottime_darwin.go +++ b/providers/darwin/boottime_darwin.go @@ -15,22 +15,23 @@ // specific language governing permissions and limitations // under the License. -//go:build (amd64 && cgo) || (arm64 && cgo) -// +build amd64,cgo arm64,cgo +//go:build amd64 || arm64 +// +build amd64 arm64 package darwin import ( "fmt" - "syscall" "time" + + "golang.org/x/sys/unix" ) const kernBoottimeMIB = "kern.boottime" func BootTime() (time.Time, error) { - var tv syscall.Timeval - if err := sysctlByName(kernBoottimeMIB, &tv); err != nil { + tv, err := unix.SysctlTimeval(kernBoottimeMIB) + if err != nil { return time.Time{}, fmt.Errorf("failed to get host uptime: %w", err) } diff --git a/providers/darwin/boottime_test.go b/providers/darwin/boottime_test.go new file mode 100644 index 00000000..3121e52c --- /dev/null +++ b/providers/darwin/boottime_test.go @@ -0,0 +1,32 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package darwin + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestBoottime(t *testing.T) { + bt, err := BootTime() + assert.NoError(t, err) + + t.Logf(bt.Format(time.RFC1123)) +}