Skip to content

Commit 4721c69

Browse files
authored
feat(pkgmgr): expose os facts to packages (#359)
Signed-off-by: Akhil Repala <[email protected]>
1 parent 6d93e42 commit 4721c69

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

pkgmgr/package.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"os/exec"
2727
"path/filepath"
2828
"regexp"
29+
"runtime"
2930
"strings"
3031

3132
"github.com/hashicorp/go-version"
@@ -142,6 +143,10 @@ func (p Package) install(
142143
"ContextDir": pkgContextDir,
143144
"DataDir": pkgDataDir,
144145
},
146+
"System": map[string]string{
147+
"OS": runtime.GOOS,
148+
"ARCH": runtime.GOARCH,
149+
},
145150
},
146151
)
147152
// Run pre-flight checks

pkgmgr/package_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ package pkgmgr
1616

1717
import (
1818
"reflect"
19+
"runtime"
1920
"strings"
2021
"testing"
22+
"text/template"
2123

2224
"gopkg.in/yaml.v3"
2325
)
@@ -68,3 +70,64 @@ func TestPackageToYaml(t *testing.T) {
6870
}
6971
}
7072
}
73+
74+
func TestOSAndARCH(t *testing.T) {
75+
expectOS := runtime.GOOS
76+
expectARCH := runtime.GOARCH
77+
78+
// Initialized a config object
79+
tempCacheDir := t.TempDir()
80+
tempDataDir := t.TempDir()
81+
cfg := Config{
82+
CacheDir: tempCacheDir,
83+
DataDir: tempDataDir,
84+
BinDir: "/usr/local/bin",
85+
Template: &Template{
86+
tmpl: template.New("test"),
87+
baseVars: make(map[string]any),
88+
},
89+
}
90+
91+
cfg.Template = cfg.Template.WithVars(
92+
map[string]any{
93+
"System": map[string]string{
94+
"OS": runtime.GOOS,
95+
"ARCH": runtime.GOARCH,
96+
},
97+
},
98+
)
99+
100+
// Defined a test package
101+
pkg := Package{}
102+
pkg.Name = "test-package"
103+
pkg.Version = "1.0.0"
104+
opts := make(map[string]bool)
105+
106+
_, _, err := pkg.install(cfg, "test", opts, false)
107+
if err != nil {
108+
t.Errorf("Installation failed: %v", err)
109+
}
110+
111+
// Verify if OS and ARCH are injected into the config template
112+
actualOS, err := cfg.Template.Render("{{ .System.OS }}", nil)
113+
if err != nil {
114+
t.Errorf("Template rendering for OS failed: %v", err)
115+
} else if actualOS != expectOS {
116+
t.Errorf("Expected OS: %s and rendered OS: %s are not same", expectOS, actualOS)
117+
} else {
118+
t.Logf("Expected OS matched with rendered OS")
119+
}
120+
121+
actualARCH, err := cfg.Template.Render("{{ .System.ARCH }}", nil)
122+
if err != nil {
123+
t.Errorf("Template rendering for ARCH failed: %v", err)
124+
} else if actualARCH != expectARCH {
125+
t.Errorf("Expected ARCH: %s and rendered ARCH: %s are not same", expectARCH, actualARCH)
126+
} else {
127+
t.Logf("Expected ARCH matched with rendered ARCH")
128+
}
129+
130+
if actualOS == expectOS && actualARCH == expectARCH {
131+
t.Logf("Test is successful and OS, ARCH values are correctly injected to config template")
132+
}
133+
}

0 commit comments

Comments
 (0)