Skip to content

Commit

Permalink
Fixed bug with bools, ints, ... values
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongXiLu committed Aug 21, 2021
1 parent 5a1b583 commit bbaf274
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 16 additions & 1 deletion internal/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -91,7 +92,21 @@ func diffPlistFile(oldPlist string, newPlist string, newSettings map[string]stri
if key != "" && value != "" {
plistFileName := oldPlist[len(viper.GetString("TmpPreferencesDir"))+1:]
originalPlist := fmt.Sprintf("%s/%s", viper.GetString("PreferencesDir"), plistFileName)
command := fmt.Sprintf("defaults write %s \"%s\" \"%s\"", originalPlist, key, value)

typeOfValue := "" // default is string
if _, err := strconv.ParseBool(value); err == nil {
typeOfValue = " -bool"
} else if _, err := strconv.Atoi(value); err == nil {
if strings.Contains(value, ".") {
typeOfValue = " -float"
} else {
typeOfValue = " -int"
}
}

command := fmt.Sprintf(
"defaults write %s \"%s\"%s \"%s\"", originalPlist, key, typeOfValue, value,
)

log.Debug("")
log.Debug(originalPlist)
Expand Down
4 changes: 2 additions & 2 deletions internal/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Test_diffPlistFile_valueChanged(t *testing.T) {
)
assert.Contains(t, settingsMap, "_HIHideMenuBar")
assert.Equal(t,
"defaults write $HOME/Library/Preferences/.GlobalPreferences.plist.valueChanged \"_HIHideMenuBar\" \"false\"",
"defaults write $HOME/Library/Preferences/.GlobalPreferences.plist.valueChanged \"_HIHideMenuBar\" -bool \"false\"",
settingsMap["_HIHideMenuBar"],
)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestDiffPreferences_valueChanged(t *testing.T) {
DiffPreferences("testdata/old_tmp/", "testdata/new_tmp/", settingsMap)
assert.Contains(t, settingsMap, "_HIHideMenuBar")
assert.Equal(t,
"defaults write $HOME/Library/Preferences/.GlobalPreferences.plist.valueChanged \"_HIHideMenuBar\" \"false\"",
"defaults write $HOME/Library/Preferences/.GlobalPreferences.plist.valueChanged \"_HIHideMenuBar\" -bool \"false\"",
settingsMap["_HIHideMenuBar"],
)
assert.Contains(t, settingsMap, "AppleFontSmoothing")
Expand Down

0 comments on commit bbaf274

Please sign in to comment.