Skip to content

Commit

Permalink
Merge pull request #622 from kmuto/dashboard-bom
Browse files Browse the repository at this point in the history
accept UTF8-BOM when reading dashboard or monitor from JSON file
  • Loading branch information
kmuto authored Jan 22, 2024
2 parents 317d6b3 + c401adc commit ceaac83
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
7 changes: 6 additions & 1 deletion dashboards/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"fmt"
"os"

"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"

"github.com/mackerelio/mackerel-client-go"
"github.com/mackerelio/mkr/format"
"github.com/mackerelio/mkr/logger"
Expand Down Expand Up @@ -92,8 +95,10 @@ func doPushDashboard(c *cli.Context) error {
f := c.String("file-path")
src, err := os.Open(f)
logger.DieIf(err)
fallback := unicode.UTF8.NewDecoder()
r := transform.NewReader(src, unicode.BOMOverride(fallback))

dec := json.NewDecoder(src)
dec := json.NewDecoder(r)
var dashboard mackerel.Dashboard
err = dec.Decode(&dashboard)
logger.DieIf(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/yudai/gojsondiff v1.0.0
golang.org/x/oauth2 v0.15.0
golang.org/x/sync v0.5.0
golang.org/x/text v0.14.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down Expand Up @@ -62,7 +63,6 @@ require (
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
Expand Down
6 changes: 5 additions & 1 deletion monitors/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/urfave/cli"
"github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)

var Command = cli.Command{
Expand Down Expand Up @@ -102,7 +104,9 @@ func monitorLoadRules(optFilePath string) ([]mackerel.Monitor, error) {
if err != nil {
return nil, err
}
return decodeMonitors(f)
fallback := unicode.UTF8.NewDecoder()
r := transform.NewReader(f, unicode.BOMOverride(fallback))
return decodeMonitors(r)
}

// decodeMonitors decodes monitors JSON.
Expand Down
34 changes: 34 additions & 0 deletions monitors/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,37 @@ func TestDiffMonitorsWithScopes(t *testing.T) {
t.Errorf("expected:\n%s\n, output:\n%s\n", expected, diff)
}
}

func TestMonitorLoadRulesWithBOM(t *testing.T) {
// XXX: t.TempDir is better, but it will cause "TempDir RemoveAll cleanup: remove C:\...\monitors.json: The process cannot access the file because it is being used by another process." error on Windows
tmpFile, err := os.CreateTemp("", "")
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())

json := `{"monitors": []}`

_, err = tmpFile.WriteString(json)
if err != nil {
t.Errorf("should not raise error: %v", err)
}
_, err = monitorLoadRules(tmpFile.Name())
if err != nil {
t.Error("should accept JSON content no BOM")
}

utf8bom := "\xef\xbb\xbf"
_, err = tmpFile.Seek(0, 0)
if err != nil {
t.Errorf("should not raise error: %v", err)
}
_, err = tmpFile.WriteString(utf8bom + json)
if err != nil {
t.Errorf("should not raise error: %v", err)
}
_, err = monitorLoadRules(tmpFile.Name())
if err != nil {
t.Error("should accept JSON content with BOM")
}
}

0 comments on commit ceaac83

Please sign in to comment.