func readReleaseInfo()

in sharedlibraries/osinfo/osinfo.go [74:107]


func readReleaseInfo(ctx context.Context, cfr FileReadCloser, filePath string) (osVendorID, osVersion string, err error) {
	if cfr == nil || filePath == "" {
		return "", "", fmt.Errorf("both ConfigFileReader and OSReleaseFilePath must be set")
	}

	file, err := cfr(filePath)
	if err != nil {
		return "", "", err
	}
	defer file.Close()

	ini := goini.New()
	if err := ini.ParseFrom(file, "\n", "="); err != nil {
		return "", "", fmt.Errorf("failed to parse %s: %v", filePath, err)
	}

	id, ok := ini.Get("ID")
	if !ok {
		log.CtxLogger(ctx).Warn(fmt.Sprintf("Could not read ID from %s", filePath))
		id = ""
	}
	osVendorID = strings.ReplaceAll(strings.TrimSpace(id), `"`, "")

	version, ok := ini.Get("VERSION")
	if !ok {
		log.CtxLogger(ctx).Warn(fmt.Sprintf("Could not read VERSION from %s", filePath))
		version = ""
	}
	if vf := strings.Fields(version); len(vf) > 0 {
		osVersion = strings.ReplaceAll(strings.TrimSpace(vf[0]), `"`, "")
	}

	return osVendorID, osVersion, nil
}