Skip to content

Commit

Permalink
Make .chezmoi.osRelease 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 9d74f07 commit dcf0ba1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
6 changes: 3 additions & 3 deletions internal/chezmoi/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func KernelInfo(fileSystem vfs.FS) (map[string]string, error) {

// OSRelease returns the operating system identification data as defined by the
// os-release specification.
func OSRelease(fileSystem vfs.FS) (map[string]string, error) {
func OSRelease(fileSystem vfs.FS) (map[string]interface{}, error) {
for _, filename := range []string{
"/usr/lib/os-release",
"/etc/os-release",
Expand Down Expand Up @@ -85,8 +85,8 @@ func maybeUnquote(s string) string {

// parseOSRelease parses operating system identification data from r as defined
// by the os-release specification.
func parseOSRelease(r io.Reader) (map[string]string, error) {
result := make(map[string]string)
func parseOSRelease(r io.Reader) (map[string]interface{}, error) {
result := make(map[string]interface{})
s := bufio.NewScanner(r)
for s.Scan() {
// Trim all leading whitespace, but not necessarily trailing whitespace.
Expand Down
12 changes: 6 additions & 6 deletions internal/chezmoi/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestOSRelease(t *testing.T) {
for _, tc := range []struct {
name string
root map[string]interface{}
expected map[string]string
expected map[string]interface{}
}{
{
name: "fedora",
Expand All @@ -82,7 +82,7 @@ func TestOSRelease(t *testing.T) {
`BUG_REPORT_URL="https://bugzilla.redhat.com/"`,
),
},
expected: map[string]string{
expected: map[string]interface{}{
"NAME": "Fedora",
"VERSION": "17 (Beefy Miracle)",
"ID": "fedora",
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestOSRelease(t *testing.T) {
`UBUNTU_CODENAME=bionic`,
),
},
expected: map[string]string{
expected: map[string]interface{}{
"NAME": "Ubuntu",
"VERSION": "18.04.1 LTS (Bionic Beaver)",
"ID": "ubuntu",
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestParseOSRelease(t *testing.T) {
for _, tc := range []struct {
name string
s string
expected map[string]string
expected map[string]interface{}
}{
{
name: "fedora",
Expand All @@ -157,7 +157,7 @@ func TestParseOSRelease(t *testing.T) {
`HOME_URL="https://fedoraproject.org/"`,
`BUG_REPORT_URL="https://bugzilla.redhat.com/"`,
),
expected: map[string]string{
expected: map[string]interface{}{
"NAME": "Fedora",
"VERSION": "17 (Beefy Miracle)",
"ID": "fedora",
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestParseOSRelease(t *testing.T) {
`VERSION_CODENAME=bionic`,
`UBUNTU_CODENAME=bionic`,
),
expected: map[string]string{
expected: map[string]interface{}{
"NAME": "Ubuntu",
"VERSION": "18.04.1 LTS (Bionic Beaver)",
"ID": "ubuntu",
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/doctorcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ func (osArchCheck) Name() string {
func (osArchCheck) Run() (checkResult, string) {
fields := []string{runtime.GOOS + "/" + runtime.GOARCH}
if osRelease, err := chezmoi.OSRelease(vfs.OSFS); err == nil {
fields = append(fields, "("+osRelease["NAME"]+"/"+osRelease["VERSION"]+")")
if name, ok := osRelease["NAME"].(string); ok {
if version, ok := osRelease["VERSION"].(string); ok {
fields = append(fields, "("+name+"/"+version+")")
}
}
}
return checkOK, strings.Join(fields, " ")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/upgradecmd_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,12 @@ func getPackageType(fileSystem vfs.FS) (string, error) {
if err != nil {
return packageTypeNone, err
}
if id, ok := osRelease["ID"]; ok {
if id, ok := osRelease["ID"].(string); ok {
if packageType, ok := packageTypeByID[id]; ok {
return packageType, nil
}
}
if idLikes, ok := osRelease["ID_LIKE"]; ok {
if idLikes, ok := osRelease["ID_LIKE"].(string); ok {
for _, id := range strings.Split(idLikes, " ") {
if packageType, ok := packageTypeByID[id]; ok {
return packageType, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func uniqueAbbreviations(values []string) map[string]string {

// upperSnakeCaseToCamelCaseKeys returns m with all keys converted from
// UPPER_SNAKE_CASE to camelCase.
func upperSnakeCaseToCamelCaseMap(m map[string]string) map[string]string {
result := make(map[string]string)
func upperSnakeCaseToCamelCaseMap(m map[string]interface{}) map[string]interface{} {
result := make(map[string]interface{})
for k, v := range m {
result[upperSnakeCaseToCamelCase(k)] = v
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ func TestUniqueAbbreviations(t *testing.T) {
}

func TestUpperSnakeCaseToCamelCaseMap(t *testing.T) {
actual := upperSnakeCaseToCamelCaseMap(map[string]string{
actual := upperSnakeCaseToCamelCaseMap(map[string]interface{}{
"BUG_REPORT_URL": "",
"ID": "",
})
assert.Equal(t, map[string]string{
assert.Equal(t, map[string]interface{}{
"bugReportURL": "",
"id": "",
}, actual)
Expand Down

0 comments on commit dcf0ba1

Please sign in to comment.