Source File
os_release_unix.go
Belonging Package
go.opentelemetry.io/otel/sdk/resource
// Copyright The OpenTelemetry Authors// SPDX-License-Identifier: Apache-2.0//go:build aix || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos// +build aix dragonfly freebsd linux netbsd openbsd solaris zospackage resource // import "go.opentelemetry.io/otel/sdk/resource"import ()// osRelease builds a string describing the operating system release based on the// properties of the os-release file. If no os-release file is found, or if the// required properties to build the release description string are missing, an empty// string is returned instead. For more information about os-release files, see:// https://www.freedesktop.org/software/systemd/man/os-release.htmlfunc osRelease() string {, := getOSReleaseFile()if != nil {return ""}defer .Close():= parseOSReleaseFile()return buildOSRelease()}// getOSReleaseFile returns a *os.File pointing to one of the well-known os-release// files, according to their order of preference. If no file can be opened, it// returns an error.func getOSReleaseFile() (*os.File, error) {return getFirstAvailableFile([]string{"/etc/os-release", "/usr/lib/os-release"})}// parseOSReleaseFile process the file pointed by `file` as an os-release file and// returns a map with the key-values contained in it. Empty lines or lines starting// with a '#' character are ignored, as well as lines with the missing key=value// separator. Values are unquoted and unescaped.func parseOSReleaseFile( io.Reader) map[string]string {:= make(map[string]string):= bufio.NewScanner()for .Scan() {:= .Text()if skip() {continue}, , := parse()if {[] =}}return}// skip reports whether the line is blank or starts with a '#' character, and// therefore should be skipped from processing.func skip( string) bool {= strings.TrimSpace()return == "" || strings.HasPrefix(, "#")}// parse attempts to split the provided line on the first '=' character, and then// sanitize each side of the split before returning them as a key-value pair.func parse( string) (string, string, bool) {, , := strings.Cut(, "=")if ! || == "" {return "", "", false}:= strings.TrimSpace():= unescape(unquote(strings.TrimSpace()))return , , true}// unquote checks whether the string `s` is quoted with double or single quotes// and, if so, returns a version of the string without them. Otherwise it returns// the provided string unchanged.func unquote( string) string {if len() < 2 {return}if ([0] == '"' || [0] == '\'') && [0] == [len()-1] {return [1 : len()-1]}return}// unescape removes the `\` prefix from some characters that are expected// to have it added in front of them for escaping purposes.func unescape( string) string {return strings.NewReplacer(`\$`, `$`,`\"`, `"`,`\'`, `'`,`\\`, `\`,"\\`", "`",).Replace()}// buildOSRelease builds a string describing the OS release based on the properties// available on the provided map. It favors a combination of the `NAME` and `VERSION`// properties as first option (falling back to `VERSION_ID` if `VERSION` isn't// found), and using `PRETTY_NAME` alone if some of the previous are not present. If// none of these properties are found, it returns an empty string.//// The rationale behind not using `PRETTY_NAME` as first choice was that, for some// Linux distributions, it doesn't include the same detail that can be found on the// individual `NAME` and `VERSION` properties, and combining `PRETTY_NAME` with// other properties can produce "pretty" redundant strings in some cases.func buildOSRelease( map[string]string) string {var string:= ["NAME"]:= ["VERSION"]if == "" {= ["VERSION_ID"]}if != "" && != "" {= fmt.Sprintf("%s %s", , )} else {= ["PRETTY_NAME"]}return}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |