Skip to content

Commit

Permalink
add os version template function
Browse files Browse the repository at this point in the history
  • Loading branch information
femnad committed Dec 17, 2023
1 parent 55d8c78 commit 3966538
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
22 changes: 16 additions & 6 deletions precheck/fact.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,21 @@ func isOs(osId string) (bool, error) {
return foundOsId == osId, nil
}

func isOsVersion(version string) (bool, error) {
osVersion, err := GetOSVersion()
if err != nil {
return false, err
}

return osVersion == version, err
}

var FactFns = template.FuncMap{
"env": hasEnv,
"is": isA,
"ok": isOk,
"os": isOs,
"output": hasOutput,
"pkg": hasPkgMgr,
"env": hasEnv,
"is": isA,
"ok": isOk,
"os": isOs,
"output": hasOutput,
"pkg": hasPkgMgr,
"version": isOsVersion,
}
19 changes: 18 additions & 1 deletion precheck/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ import (
)

const (
quote = `"`
osReleaseFile = "/etc/os-release"
osIdField = "ID"
osVersionField = "VERSION_ID"
versionCodenameField = "VERSION_CODENAME"
)

func removeLeadingTrailingQuotes(s string) string {
if strings.HasPrefix(s, quote) {
s = strings.TrimPrefix(s, quote)
}
if strings.HasSuffix(s, quote) {
s = strings.TrimSuffix(s, quote)
}
return s
}

func getOSReleaseField(f string) (string, error) {
file, err := os.Open(osReleaseFile)
if err != nil {
Expand Down Expand Up @@ -41,7 +53,8 @@ func getOSReleaseField(f string) (string, error) {
if field != f {
continue
}
return value, nil

return removeLeadingTrailingQuotes(value), nil
}

return "", fmt.Errorf("unable to locate OS ID line in %s", osReleaseFile)
Expand All @@ -54,3 +67,7 @@ func GetOSVersionCodename() (string, error) {
func GetOSId() (string, error) {
return getOSReleaseField(osIdField)
}

func GetOSVersion() (string, error) {
return getOSReleaseField(osVersionField)
}
2 changes: 1 addition & 1 deletion provision/osrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func addRepos(config base.Config) error {
continue
}

internal.Log.Noticef("Adding repo %s", repo.Name())
internal.Log.Infof("Adding repo %s", repo.Name())

err := repo.Install()
if err == nil && repo.UpdateCmd() != "" {
Expand Down

0 comments on commit 3966538

Please sign in to comment.