Skip to content

Commit

Permalink
Make .chezmoi.kernel template variable more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jul 13, 2021
1 parent dcf0ba1 commit 7c688ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions internal/chezmoi/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/twpayne/go-vfs/v3"
)

// KernelInfo returns the kernel information parsed from /proc/sys/kernel.
func KernelInfo(fileSystem vfs.FS) (map[string]string, error) {
// Kernel returns the kernel information parsed from /proc/sys/kernel.
func Kernel(fileSystem vfs.FS) (map[string]interface{}, error) {
const procSysKernel = "/proc/sys/kernel"

info, err := fileSystem.Stat(procSysKernel)
Expand All @@ -31,7 +31,7 @@ func KernelInfo(fileSystem vfs.FS) (map[string]string, error) {
return nil, nil
}

kernelInfo := make(map[string]string)
kernel := make(map[string]interface{})
for _, filename := range []string{
"osrelease",
"ostype",
Expand All @@ -46,9 +46,9 @@ func KernelInfo(fileSystem vfs.FS) (map[string]string, error) {
case err != nil:
return nil, err
}
kernelInfo[filename] = string(bytes.TrimSpace(data))
kernel[filename] = string(bytes.TrimSpace(data))
}
return kernelInfo, nil
return kernel, nil
}

// OSRelease returns the operating system identification data as defined by the
Expand Down
18 changes: 9 additions & 9 deletions internal/chezmoi/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"github.com/twpayne/chezmoi/v2/internal/chezmoitest"
)

func TestKernelInfo(t *testing.T) {
func TestKernel(t *testing.T) {
for _, tc := range []struct {
name string
root interface{}
expectedKernelInfo map[string]string
name string
root interface{}
expectedKernel map[string]interface{}
}{
{
name: "windows_services_for_linux",
Expand All @@ -26,7 +26,7 @@ func TestKernelInfo(t *testing.T) {
"version": "#1 SMP Debian 5.2.9-2 (2019-08-21)\n",
},
},
expectedKernelInfo: map[string]string{
expectedKernel: map[string]interface{}{
"osrelease": "4.19.81-microsoft-standard",
"ostype": "Linux",
"version": "#1 SMP Debian 5.2.9-2 (2019-08-21)",
Expand All @@ -39,7 +39,7 @@ func TestKernelInfo(t *testing.T) {
"version": "#1 SMP Debian 5.2.9-2 (2019-08-21)\n",
},
},
expectedKernelInfo: map[string]string{
expectedKernel: map[string]interface{}{
"version": "#1 SMP Debian 5.2.9-2 (2019-08-21)",
},
},
Expand All @@ -48,14 +48,14 @@ func TestKernelInfo(t *testing.T) {
root: map[string]interface{}{
"/proc/sys": &vfst.Dir{Perm: 0o755},
},
expectedKernelInfo: nil,
expectedKernel: nil,
},
} {
t.Run(tc.name, func(t *testing.T) {
chezmoitest.WithTestFS(t, tc.root, func(fileSystem vfs.FS) {
actual, err := KernelInfo(fileSystem)
actual, err := Kernel(fileSystem)
assert.NoError(t, err)
assert.Equal(t, tc.expectedKernelInfo, actual)
assert.Equal(t, tc.expectedKernel, actual)
})
})
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,12 @@ func (c *Config) defaultTemplateData() map[string]interface{} {
Msg("os.Hostname")
}

if kernelInfo, err := chezmoi.KernelInfo(c.fileSystem); err == nil {
data["kernel"] = kernelInfo
if kernel, err := chezmoi.Kernel(c.fileSystem); err == nil {
data["kernel"] = kernel
} else {
log.Debug().
Err(err).
Msg("chezmoi.KernelInfo")
Msg("chezmoi.Kernel")
}

if osRelease, err := chezmoi.OSRelease(c.fileSystem); err == nil {
Expand Down

0 comments on commit 7c688ef

Please sign in to comment.