package scout

import (
	"context"
	"strings"

	"github.com/datawire/dlib/dexec"
	"github.com/datawire/dlib/dlog"
)

func runSwVers(ctx context.Context, versionName string) string {
	cmd := dexec.CommandContext(ctx, "sw_vers", "-"+versionName)
	cmd.DisableLogging = true
	r, err := cmd.Output()
	if err != nil {
		dlog.Warnf(ctx, "Could not get os metadata %s: %v", versionName, err)
		return "unknown"
	}
	return strings.TrimSpace(string(r))
}

func getOsMetadata(ctx context.Context) map[string]interface{} {
	osMeta := map[string]interface{}{
		"os_version":       runSwVers(ctx, "productVersion"),
		"os_build_version": runSwVers(ctx, "buildVersion"),
		"os_name":          runSwVers(ctx, "productName"),
	}
	return osMeta
}

package scout

import (
	"bufio"
	"bytes"
	"context"
	"strings"

	"github.com/datawire/dlib/dexec"
	"github.com/datawire/dlib/dlog"
)

func getOsMetadata(ctx context.Context) map[string]interface{} {
	osMeta := map[string]interface{}{
		"os_version":       "unknown",
		"os_build_version": "unknown",
		"os_name":          "unknown",
	}
	cmd := dexec.CommandContext(ctx, "sw_vers")
	cmd.DisableLogging = true
	if r, err := cmd.Output(); err != nil {
		dlog.Warnf(ctx, "Could not get os metadata: %v", err)
	} else {
		sc := bufio.NewScanner(bytes.NewReader(r))
		for sc.Scan() {
			fs := strings.Fields(sc.Text())
			if len(fs) == 2 {
				switch fs[0] {
				case "ProductName:":
					osMeta["os_name"] = fs[1]
				case "ProductVersion:":
					osMeta["os_version"] = fs[1]
				case "BuildVersion:":
					osMeta["os_build_version"] = fs[1]
				}
			}
		}
	}
	return osMeta
}

Speed up getOsMetadata() on macOS
Speed up getOsMetadata() on macOS

Shell out to `sw_vers` once instead of three times.

Signed-off-by: Thomas Hallgren <5f50a84c1fa3bcff146405017f36aec1a10a9e38@datawire.io>
