Skip to content

Commit 967ba19

Browse files
committed
move helpers round
1 parent ea86ea8 commit 967ba19

3 files changed

Lines changed: 65 additions & 66 deletions

File tree

src/helpers.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,48 @@
44
package main
55

66
import (
7+
"bufio"
78
"crypto/tls"
89
"errors"
910
"fmt"
1011
"io"
1112
"net"
1213
"path/filepath"
1314
"os"
15+
"runtime"
1416
"strconv"
1517
"strings"
1618
"syscall"
1719
"time"
1820
)
1921

2022

23+
func showRuntimeInfo() {
24+
25+
logSpace()
26+
logMsg("Runtime")
27+
logMsg("-------------------------")
28+
logSpace()
29+
30+
info, err := readOSRelease()
31+
if err == nil {
32+
showInfo("Name", info["PRETTY_NAME"])
33+
showInfo("ID", info["ID"])
34+
showInfo("Version", info["VERSION_ID"])
35+
}
36+
37+
showInfo("Go version", runtime.Version())
38+
showInfo("OS", runtime.GOOS)
39+
showInfo("Arch", runtime.GOARCH)
40+
showInfo("Platform", gBuildPlatform)
41+
42+
showInfo("CPUs", runtime.NumCPU())
43+
showInfo("PID", os.Getpid())
44+
45+
showInfo("UID/GID", strconv.Itoa(os.Getuid()) + ":" + strconv.Itoa(os.Getgid()))
46+
showInfo("EUID/EGID", strconv.Itoa(os.Geteuid()) + ":" + strconv.Itoa(os.Getegid()))
47+
}
48+
2149
func formatStr(s string) string {
2250
if s == "" {
2351
return "[]"
@@ -338,3 +366,31 @@ func parseCIDRs(list []string) ([]*net.IPNet, error) {
338366

339367
return nets, nil
340368
}
369+
370+
func readOSRelease() (map[string]string, error) {
371+
372+
file, err := os.Open("/etc/os-release")
373+
if err != nil {
374+
return nil, err
375+
}
376+
defer file.Close()
377+
378+
data := make(map[string]string)
379+
scanner := bufio.NewScanner(file)
380+
381+
for scanner.Scan() {
382+
line := scanner.Text()
383+
384+
if strings.HasPrefix(line, "#") || !strings.Contains(line, "=") {
385+
continue
386+
}
387+
388+
parts := strings.SplitN(line, "=", 2)
389+
key := parts[0]
390+
val := strings.Trim(parts[1], `"`)
391+
392+
data[key] = val
393+
}
394+
395+
return data, scanner.Err()
396+
}

src/log.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import (
1111
"time"
1212
)
1313

14+
func showCfg(description, variableName, defaultValue, currentValue any) {
15+
logMsg("%-34s %-34s %-30v %v", variableName, description, defaultValue, currentValue)
16+
}
17+
18+
func showInfo(description, currentValue any) {
19+
logMsg("%-15s: %v", description, currentValue)
20+
}
21+
1422
func (l LogLevel) LowerCaseStr() string {
1523

1624
switch l {

src/main.go

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"os/signal"
1818
"path/filepath"
1919
"runtime"
20-
"strconv"
2120
"strings"
2221
"sync"
2322
"sync/atomic"
@@ -140,10 +139,7 @@ type Stats struct
140139

141140
var stats Stats
142141

143-
144-
// Global counters
145-
146-
142+
// Declared to overwrite by build
147143
var gBuildPlatform = "unknown"
148144

149145
// Global variables
@@ -400,40 +396,6 @@ var (
400396
smtpResponse_BlockedIpByRBL = []byte("530 Connection rejected by policy\r\n")
401397
)
402398

403-
func showCfg(description, variableName, defaultValue, currentValue any) {
404-
logMsg("%-34s %-34s %-30v %v", variableName, description, defaultValue, currentValue)
405-
}
406-
407-
func showInfo(description, currentValue any) {
408-
logMsg("%-15s: %v", description, currentValue)
409-
}
410-
411-
func showRuntimeInfo() {
412-
413-
logSpace()
414-
logMsg("Runtime")
415-
logMsg("-------------------------")
416-
logSpace()
417-
418-
info, err := readOSRelease()
419-
if err == nil {
420-
showInfo("Name", info["PRETTY_NAME"])
421-
showInfo("ID", info["ID"])
422-
showInfo("Version", info["VERSION_ID"])
423-
}
424-
425-
showInfo("Go version", runtime.Version())
426-
showInfo("OS", runtime.GOOS)
427-
showInfo("Arch", runtime.GOARCH)
428-
showInfo("Platform", gBuildPlatform)
429-
430-
showInfo("CPUs", runtime.NumCPU())
431-
showInfo("PID", os.Getpid())
432-
433-
showInfo("UID/GID", strconv.Itoa(os.Getuid()) + ":" + strconv.Itoa(os.Getgid()))
434-
showInfo("EUID/EGID", strconv.Itoa(os.Geteuid()) + ":" + strconv.Itoa(os.Getegid()))
435-
}
436-
437399
func shutdown() {
438400

439401
logLine("Shutting down ...")
@@ -1770,30 +1732,3 @@ func watchCertificateFiles() {
17701732
}
17711733
}
17721734

1773-
func readOSRelease() (map[string]string, error) {
1774-
1775-
file, err := os.Open("/etc/os-release")
1776-
if err != nil {
1777-
return nil, err
1778-
}
1779-
defer file.Close()
1780-
1781-
data := make(map[string]string)
1782-
scanner := bufio.NewScanner(file)
1783-
1784-
for scanner.Scan() {
1785-
line := scanner.Text()
1786-
1787-
if strings.HasPrefix(line, "#") || !strings.Contains(line, "=") {
1788-
continue
1789-
}
1790-
1791-
parts := strings.SplitN(line, "=", 2)
1792-
key := parts[0]
1793-
val := strings.Trim(parts[1], `"`)
1794-
1795-
data[key] = val
1796-
}
1797-
1798-
return data, scanner.Err()
1799-
}

0 commit comments

Comments
 (0)