Skip to content

Commit 30420e0

Browse files
committed
refactor: move GetOsInfo to installer package
1 parent 2e2fcdf commit 30420e0

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

features/src/nginx/installer.go

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

33
import (
4-
"bufio"
54
"builder/installer"
65
"flag"
76
"fmt"
87
"os"
98
"regexp"
10-
"strings"
119

1210
"github.com/roemer/gover"
1311
)
@@ -45,7 +43,7 @@ func runMain() error {
4543
installer.HandleOverride(downloadUrl, "https://nginx.org", "nginx-download-url")
4644

4745
// Fetch the os info
48-
osInfo, err := getOsInfo()
46+
osInfo, err := installer.Tools.System.GetOsInfo()
4947
if err != nil {
5048
return fmt.Errorf("failed getting OS info: %w", err)
5149
}
@@ -66,33 +64,10 @@ func runMain() error {
6664
// Implementation
6765
//////////
6866

69-
type osInfo struct {
70-
vendor string
71-
codename string
72-
}
73-
74-
func getOsInfo() (*osInfo, error) {
75-
f, err := os.Open("/etc/os-release")
76-
if err != nil {
77-
return nil, err
78-
}
79-
defer f.Close()
80-
s := bufio.NewScanner(f)
81-
infoMap := map[string]string{}
82-
for s.Scan() {
83-
parts := strings.SplitN(s.Text(), "=", 2)
84-
infoMap[parts[0]] = parts[1]
85-
}
86-
return &osInfo{
87-
vendor: infoMap["ID"],
88-
codename: infoMap["VERSION_CODENAME"],
89-
}, nil
90-
}
91-
9267
type nginxComponent struct {
9368
*installer.ComponentBase
9469
stableOnly bool
95-
osInfo *osInfo
70+
osInfo *installer.OsInfo
9671
downloadUrl string
9772
}
9873

@@ -129,7 +104,7 @@ func (c *nginxComponent) InstallVersion(version *gover.Version) error {
129104
if err != nil {
130105
return err
131106
}
132-
debName := fmt.Sprintf("nginx_%s~%s_%s.deb", version.Raw, c.osInfo.codename, archPart)
107+
debName := fmt.Sprintf("nginx_%s~%s_%s.deb", version.Raw, c.osInfo.Codename, archPart)
133108
urls = append(urls, c.getStableUrl()+debName)
134109
if !c.stableOnly {
135110
urls = append(urls, c.getMainlineUrl()+debName)
@@ -159,18 +134,18 @@ func (c *nginxComponent) InstallVersion(version *gover.Version) error {
159134
}
160135

161136
func (c *nginxComponent) getStableUrl() string {
162-
return fmt.Sprintf("%s/packages/%s/pool/nginx/n/nginx/", c.downloadUrl, c.osInfo.vendor)
137+
return fmt.Sprintf("%s/packages/%s/pool/nginx/n/nginx/", c.downloadUrl, c.osInfo.Vendor)
163138
}
164139

165140
func (c *nginxComponent) getMainlineUrl() string {
166-
return fmt.Sprintf("%s/packages/mainline/%s/pool/nginx/n/nginx/", c.downloadUrl, c.osInfo.vendor)
141+
return fmt.Sprintf("%s/packages/mainline/%s/pool/nginx/n/nginx/", c.downloadUrl, c.osInfo.Vendor)
167142
}
168143

169144
func (c *nginxComponent) lineExtractFunc(url, line string) (*gover.Version, error) {
170145
if match := indexLineRegexp.FindStringSubmatch(line); match != nil {
171146
versionString := match[2]
172147
codename := match[3]
173-
if codename == c.osInfo.codename {
148+
if codename == c.osInfo.Codename {
174149
version := gover.MustParseVersionFromRegex(versionString, versionRegexp)
175150
return version, nil
176151
}

installer/system.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package installer
22

33
import (
4+
"bufio"
45
"fmt"
6+
"os"
57
"runtime"
8+
"strings"
69
)
710

811
const (
@@ -19,3 +22,26 @@ func (s *system) MapArchitecture(mapping map[string]string) (string, error) {
1922
}
2023
return mappedValue, nil
2124
}
25+
26+
type OsInfo struct {
27+
Vendor string
28+
Codename string
29+
}
30+
31+
func (s *system) GetOsInfo() (*OsInfo, error) {
32+
f, err := os.Open("/etc/os-release")
33+
if err != nil {
34+
return nil, err
35+
}
36+
defer f.Close()
37+
scanner := bufio.NewScanner(f)
38+
infoMap := map[string]string{}
39+
for scanner.Scan() {
40+
parts := strings.SplitN(scanner.Text(), "=", 2)
41+
infoMap[parts[0]] = parts[1]
42+
}
43+
return &OsInfo{
44+
Vendor: infoMap["ID"],
45+
Codename: infoMap["VERSION_CODENAME"],
46+
}, nil
47+
}

0 commit comments

Comments
 (0)