Skip to content

Commit e91e8d5

Browse files
committed
Cleanup, improved error handling and slight refactor
1 parent 5cdb767 commit e91e8d5

5 files changed

Lines changed: 58 additions & 22 deletions

File tree

builder/checks/netCheck.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package checks
22

33
import (
4+
"fmt"
45
"net"
56
)
67

7-
// NetCheck Attempts to make a http request with a 10 second timeout limit, if it fails return a DNS error (default behaviour)
8-
// might change it to return something else if needed, but this should suffice
8+
// NetCheck Attempts to make a http request to the go.dev site to check for network connectivity
99
//
1010
// PENDING CONCURRENCY IMPLEMENTATION
11-
func NetCheck() (passed bool) {
12-
conn, err := net.DialTimeout("tcp", "go.dev:http", 10)
11+
func NetCheck() (passed bool, err error) {
12+
conn, err := net.Dial("tcp", "go.dev:http")
1313
if err != nil {
14-
return false
14+
return false, fmt.Errorf("connectivity error %v", err)
1515
}
1616

1717
defer conn.Close()
18-
return true
18+
return true, nil
1919
}

builder/checks/osCheck.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package checks
22

33
import (
44
"bytes"
5+
"fmt"
56
"log"
67
"os/exec"
78
)
89

910
// OsCheck PENDING CONCURRENCY IMPLEMENTATION
10-
func OsCheck() (passed bool) {
11+
func OsCheck() (passed bool, err error) {
1112

1213
// cmd sends this command to the OS shell
1314
cmd := exec.Command("cat", "/etc/os-release")
@@ -39,18 +40,18 @@ func OsCheck() (passed bool) {
3940
but I am unsure if that value can be changed when changing hostname (pending test), will re-implement based on results
4041
until then, we Will use the Distro code name to ensure a reliable answer
4142
42-
This version is more reliable and preferred so I'll keep it until any problems arise
43+
This version is more reliable and preferred, so I'll keep it until any problems arise
4344
*/
4445

4546
switch {
4647
case bytes.Contains(out, []byte("Noble Numbat")): // Ubuntu 24.04 LTS
47-
return true
48+
return true, nil
4849
case bytes.Contains(out, []byte("Trixie")): // Debian 13
49-
return true
50+
return true, nil
5051
case bytes.Contains(out, []byte("Bookworm")): // Debian 12
51-
return true
52+
return true, nil
5253
default:
53-
return false // Neither of the Above
54+
return false, fmt.Errorf("builder cannot run on this operating system") // Neither of the Above
5455

5556
}
5657
}

builder/checks/permCheck.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"syscall"
66
)
77

8-
// PrivelageCheck checks the UID if the user running isn't root it returns a fatal error!
8+
// PrivilegeCheck checks the UID if the user running isn't root it returns a fatal error!
99
// should be checked before everything for efficiency
1010
//
1111
// PENDING CONCURRENCY IMPLEMENTATION
12-
func PrivelageCheck() (passed bool) {
13-
if syscall.Getuid() != 0{ // 0 is the root User ID
14-
log.Fatal("Command must be run as Root")
12+
func PrivilegeCheck() (passed bool) {
13+
if syscall.Getuid() != 0 { // 0 is the root User ID
14+
log.Fatal("command must be run as Root")
1515
return false
1616
}
1717
return true

builder/checks/root.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package checks
2+
3+
import (
4+
"log"
5+
)
6+
7+
// AllChecks Handles all checks and their errors,
8+
// terminating the program if any errors occur and prints the error to stdout
9+
func AllChecks() (passed bool) {
10+
11+
privCheck := PrivilegeCheck()
12+
13+
osPassed, osErr := OsCheck()
14+
if osErr != nil {
15+
log.Fatal(osErr)
16+
}
17+
18+
netPassed, netErr := NetCheck()
19+
if netErr != nil {
20+
log.Fatal(netErr)
21+
}
22+
23+
storagePassed, storageErr := StorageCheck()
24+
if storageErr != nil {
25+
log.Fatal(storageErr)
26+
}
27+
28+
if privCheck && osPassed && netPassed && storagePassed == true {
29+
return true
30+
}
31+
return false
32+
33+
}

builder/checks/storageCheck.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package checks
22

33
import (
4+
"fmt"
45
"log"
56
"math"
67

78
"golang.org/x/sys/unix"
89
)
910

11+
const neededSpace = 30 // 30 Gigabytes
12+
1013
// StorageCheck PENDING CONCURRENCY IMPLEMENTATION
11-
func StorageCheck() (passed bool) {
14+
func StorageCheck() (passed bool, err error) {
1215
var stat unix.Statfs_t
13-
err := unix.Statfs("/", &stat)
16+
err = unix.Statfs("/", &stat)
1417

1518
if err != nil {
1619
log.Fatal(err)
@@ -19,10 +22,9 @@ func StorageCheck() (passed bool) {
1922
availableSpace := stat.Bavail * uint64(stat.Bsize) * uint64(math.Pow(10, 9))
2023

2124
switch {
22-
case availableSpace >= uint64(math.Pow(30, 9)):
23-
return true
25+
case availableSpace >= neededSpace:
26+
return true, nil
2427
default:
25-
return false
28+
return false, fmt.Errorf("not enough storage space (needed: %v GB) (have: %v GB)", neededSpace, availableSpace)
2629
}
27-
2830
}

0 commit comments

Comments
 (0)