Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept UTF8-BOM when reading dashboard or monitor from JSON file #622

Merged
merged 9 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Comment on lines +98 to +99
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like it a lot!


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())
Comment on lines +206 to +210
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testing.T.TempDir is useful.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had tried it on 8df6538 , 3295eed , and db949f8 but all of them went failed on Windows (I saw golang/go#50510 )
It would be better to use it but I keep CreateTemp code at this time for Windows.


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")
}
}
Loading