Skip to content

Commit

Permalink
feat: added hostinfo plugin for darwin (newrelic#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianciutea authored Jul 19, 2021
1 parent 0671792 commit debe71c
Show file tree
Hide file tree
Showing 8 changed files with 646 additions and 2 deletions.
56 changes: 56 additions & 0 deletions internal/os/distro/distro_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// +build darwin

package distro

import (
"fmt"
"github.com/newrelic/infrastructure-agent/pkg/helpers"
"regexp"
)

var (
productNameRegex = regexp.MustCompile("ProductName:\\s*(.*)")
productVersionRegex = regexp.MustCompile("ProductVersion:\\s*(.*)")

defaultProductName = "macOS"
)

type distroFetcherFn func() (string, error)

func fetchDistro() (string, error) {
return helpers.RunCommand("/usr/bin/sw_vers", "")
}

// GetDistro will parse the output for '/usr/bin/sw_vers' command to detect
// the ProductName and ProductVersion fields
func GetDistro() string {
return parseDistro(fetchDistro)
}

func parseDistro(distro distroFetcherFn) string {
var productName, productVersion string
output, err := distro()
if err != nil {
return defaultProductName
}

if productNameLine := productNameRegex.FindStringSubmatch(output); len(productNameLine) > 1 {
productName = productNameLine[1]
} else {
productName = defaultProductName
}

if productVersionLine := productVersionRegex.FindStringSubmatch(output); len(productVersionLine) > 1 {
productVersion = productVersionLine[1]
} else {
productVersion = "(Unknown)"
}

return fmt.Sprintf("%s %s", productName, productVersion)
}

func IsCentos5() bool {
return false
}
46 changes: 46 additions & 0 deletions internal/os/distro/distro_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// +build darwin

package distro

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestParseDistro_Success(t *testing.T) {
actual := parseDistro(func() (string, error) {
return `ProductName: macOS2
ProductVersion: 11.2.3
BuildVersion: 20D91`, nil
})

assert.Equal(t, "macOS2 11.2.3", actual)
}

func TestParseDistro_MissingProductName(t *testing.T) {
actual := parseDistro(func() (string, error) {
return `ProductVersion: 11.2.3
BuildVersion: 20D91`, nil
})

assert.Equal(t, "macOS 11.2.3", actual)
}

func TestParseDistro_MissingProductVersion(t *testing.T) {
actual := parseDistro(func() (string, error) {
return `BuildVersion: 20D91`, nil
})

assert.Equal(t, "macOS (Unknown)", actual)
}

func TestParseDistro_OnError(t *testing.T) {
actual := parseDistro(func() (string, error) {
return ``, fmt.Errorf("error")
})

assert.Equal(t, defaultProductName, actual)
}
2 changes: 1 addition & 1 deletion internal/os/distro/distro_unix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// +build linux darwin
// +build linux

package distro

Expand Down
Loading

0 comments on commit debe71c

Please sign in to comment.