Skip to content

Commit 6fde624

Browse files
author
Ricky Cousins
committed
Combine rhel like distros logic
1 parent e9ede8b commit 6fde624

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
utils "github.com/Puppet-Finland/updates-exporter/distros"
88
)
99

10-
type Rocky struct{}
10+
type Rhel struct{}
1111

12-
func (Rocky) GetSecurityUpdates() int {
12+
func (Rhel) GetSecurityUpdates() int {
1313
cmd := exec.Command("sh", "-c", "dnf updateinfo list --sec-severity=Critical --sec-severity=Important --all | wc -l")
1414
out, err := cmd.Output()
1515
if err != nil {
@@ -19,7 +19,7 @@ func (Rocky) GetSecurityUpdates() int {
1919
return utils.ParseUpdateCount(string(out))
2020
}
2121

22-
func (Rocky) GetTotalUpdates() int {
22+
func (Rhel) GetTotalUpdates() int {
2323
cmd := exec.Command("sh", "-c", "dnf updateinfo list --all | wc -l")
2424
out, err := cmd.Output()
2525
if err != nil {
@@ -29,8 +29,11 @@ func (Rocky) GetTotalUpdates() int {
2929
return utils.ParseUpdateCount(string(out))
3030
}
3131

32-
func (Rocky) GetRebootRequired() bool {
32+
func (Rhel) GetRebootRequired() bool {
3333
cmd := exec.Command("needs-restarting", "-r")
34-
err := cmd.Run()
35-
return err != nil
34+
if err := cmd.Run(); err != nil {
35+
log.Printf("Error %v", err)
36+
return false
37+
}
38+
return true
3639
}

distros/utils.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import (
99
"strings"
1010
)
1111

12-
var rockyReleaseFile = "/etc/rocky-release"
12+
var rhelReleases = []string{
13+
"/etc/rocky-release",
14+
"/etc/almalinux-release",
15+
"/etc/redhat-release",
16+
}
17+
1318
var osReleaseFile = "/etc/os-release"
1419

1520
func ParseUpdateCount(out string) int {
@@ -22,8 +27,10 @@ func GetLinuxDistro() string {
2227
return "unknown"
2328
}
2429

25-
if _, err := os.Stat(rockyReleaseFile); err == nil {
26-
return "rocky"
30+
for _, releaseFile := range rhelReleases {
31+
if _, err := os.Stat(releaseFile); err == nil {
32+
return "rhel"
33+
}
2734
}
2835

2936
out, err := exec.Command("sh", "-c", fmt.Sprintf("cat %s", osReleaseFile)).Output()
@@ -35,8 +42,6 @@ func GetLinuxDistro() string {
3542
switch {
3643
case strings.Contains(s, "ubuntu"):
3744
return "ubuntu"
38-
case strings.Contains(s, "alma"):
39-
return "alma"
4045
case strings.Contains(s, "rhel"), strings.Contains(s, "centos"), strings.Contains(s, "fedora"):
4146
return "rhel"
4247
default:

distros/utils_test.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ import (
66
)
77

88
func getRelease(r string, t *testing.T) {
9-
osReleaseFile = "/tmp/os-release"
109

11-
os.WriteFile(osReleaseFile, []byte(r), 0644)
12-
got := GetLinuxDistro()
13-
expected := r
14-
if got != expected {
15-
t.Errorf("Expected %s, got %s", expected, got)
16-
}
1710
}
1811

1912
func TestParseUpdateCount(t *testing.T) {
@@ -26,17 +19,31 @@ func TestParseUpdateCount(t *testing.T) {
2619
}
2720

2821
func TestGetDistros(t *testing.T) {
29-
getRelease("alma", t)
30-
getRelease("rhel", t)
31-
getRelease("ubuntu", t)
32-
}
33-
34-
func TestGetDistrosRocky(t *testing.T) {
35-
rockyReleaseFile = "/tmp/rocky-release"
22+
osReleaseFile = "/tmp/os-release"
23+
rhelReleases = []string{
24+
"/tmp/rocky-release",
25+
}
26+
os.Remove(rhelReleases[0])
3627

37-
os.WriteFile(rockyReleaseFile, []byte("rocky"), 0644)
28+
os.WriteFile(osReleaseFile, []byte("ubuntu"), 0644)
3829
got := GetLinuxDistro()
39-
expected := "rocky"
30+
expected := "ubuntu"
31+
if got != expected {
32+
t.Errorf("Expected %s, got %s", expected, got)
33+
}
34+
35+
os.WriteFile(osReleaseFile, []byte("fedora"), 0644)
36+
got = GetLinuxDistro()
37+
expected = "rhel"
38+
if got != expected {
39+
t.Errorf("Expected %s, got %s", expected, got)
40+
}
41+
42+
if err := os.WriteFile(rhelReleases[0], []byte("test"), 0644); err != nil {
43+
t.Errorf("Error creating %s", rhelReleases[0])
44+
}
45+
46+
got = GetLinuxDistro()
4047
if got != expected {
4148
t.Errorf("Expected %s, got %s", expected, got)
4249
}

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func getDistro() distros.Distro {
3939
switch distros.GetLinuxDistro() {
4040
case "ubuntu":
4141
return ubuntu.Ubuntu{}
42-
case "rocky", "alma":
43-
return rhel.Rocky{}
42+
case "rhel":
43+
return rhel.Rhel{}
4444
default:
4545
return nil
4646
}

0 commit comments

Comments
 (0)