Only write settings if no error when reading

This commit also switches from yosuke-furukawa/json5 to flynn/json5
because yosuke-furukawa/json5 does not provide a license.

Ref zyedidia/json5#1

Closes #775
This commit is contained in:
Zachary Yedidia
2017-08-06 12:02:17 -04:00
parent 5c785ab1ac
commit 202cfb574c
8 changed files with 84 additions and 10 deletions

View File

@@ -5,7 +5,7 @@ import (
"os"
"strings"
"github.com/zyedidia/json5/encoding/json5"
"github.com/flynn/json5"
"github.com/zyedidia/tcell"
)

View File

@@ -14,8 +14,8 @@ import (
"sync"
"github.com/blang/semver"
"github.com/flynn/json5"
"github.com/yuin/gopher-lua"
"github.com/zyedidia/json5/encoding/json5"
)
var (

View File

@@ -1,10 +1,11 @@
package main
import (
"github.com/blang/semver"
"testing"
"github.com/zyedidia/json5/encoding/json5"
"github.com/blang/semver"
"github.com/flynn/json5"
)
func TestDependencyResolving(t *testing.T) {

View File

@@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
@@ -8,8 +9,8 @@ import (
"strconv"
"strings"
"github.com/flynn/json5"
"github.com/zyedidia/glob"
"github.com/zyedidia/json5/encoding/json5"
)
type optionValidator func(string, interface{}) error
@@ -17,6 +18,8 @@ type optionValidator func(string, interface{}) error
// The options that the user can set
var globalSettings map[string]interface{}
var invalidSettings bool
// Options with validators
var optionValidators = map[string]optionValidator{
"tabsize": validatePositiveValue,
@@ -28,6 +31,7 @@ var optionValidators = map[string]optionValidator{
// InitGlobalSettings initializes the options map and sets all options to their default values
func InitGlobalSettings() {
invalidSettings = false
defaults := DefaultGlobalSettings()
var parsed map[string]interface{}
@@ -38,12 +42,14 @@ func InitGlobalSettings() {
if !strings.HasPrefix(string(input), "null") {
if err != nil {
TermMessage("Error reading settings.json file: " + err.Error())
invalidSettings = true
return
}
err = json5.Unmarshal(input, &parsed)
if err != nil {
TermMessage("Error reading settings.json:", err.Error())
invalidSettings = true
}
} else {
writeSettings = true
@@ -71,6 +77,7 @@ func InitGlobalSettings() {
// InitLocalSettings scans the json in settings.json and sets the options locally based
// on whether the buffer matches the glob
func InitLocalSettings(buf *Buffer) {
invalidSettings = false
var parsed map[string]interface{}
filename := configDir + "/settings.json"
@@ -78,12 +85,14 @@ func InitLocalSettings(buf *Buffer) {
input, err := ioutil.ReadFile(filename)
if err != nil {
TermMessage("Error reading settings.json file: " + err.Error())
invalidSettings = true
return
}
err = json5.Unmarshal(input, &parsed)
if err != nil {
TermMessage("Error reading settings.json:", err.Error())
invalidSettings = true
}
}
@@ -106,6 +115,11 @@ func InitLocalSettings(buf *Buffer) {
// WriteSettings writes the settings to the specified filename as JSON
func WriteSettings(filename string) error {
if invalidSettings {
// Do not write the settings if there was an error when reading them
return nil
}
var err error
if _, e := os.Stat(configDir); e == nil {
parsed := make(map[string]interface{})
@@ -124,6 +138,7 @@ func WriteSettings(filename string) error {
err = json5.Unmarshal(input, &parsed)
if err != nil {
TermMessage("Error reading settings.json:", err.Error())
invalidSettings = true
}
for k, v := range parsed {
@@ -136,7 +151,7 @@ func WriteSettings(filename string) error {
}
}
txt, _ := json5.MarshalIndent(parsed, "", " ")
txt, _ := json.MarshalIndent(parsed, "", " ")
err = ioutil.WriteFile(filename, append(txt, '\n'), 0644)
}
return err