Skip to content

Commit 0bc27e5

Browse files
authored
logging: fix file mode configuration parsing (#6383)
Commit 101d3e7 introduced file mode setting, but was missing a JSON Marshaller so that CaddyFile can be converted to JSON safely.
1 parent 9be4f19 commit 0bc27e5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

modules/logging/filewriter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (m *fileMode) UnmarshalJSON(b []byte) error {
5959
return err
6060
}
6161

62+
// MarshalJSON satisfies json.Marshaler.
63+
func (m *fileMode) MarshalJSON() ([]byte, error) {
64+
return []byte(fmt.Sprintf("\"%04o\"", *m)), nil
65+
}
66+
6267
// parseFileMode parses a file mode string,
6368
// adding support for `chmod` unix command like
6469
// 1 to 4 digital octal values.

modules/logging/filewriter_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,42 @@ func TestFileModeJSON(t *testing.T) {
306306
})
307307
}
308308
}
309+
310+
func TestFileModeToJSON(t *testing.T) {
311+
tests := []struct {
312+
name string
313+
mode fileMode
314+
want string
315+
wantErr bool
316+
}{
317+
{
318+
name: "none zero",
319+
mode: 0644,
320+
want: `"0644"`,
321+
wantErr: false,
322+
},
323+
{
324+
name: "zero mode",
325+
mode: 0,
326+
want: `"0000"`,
327+
wantErr: false,
328+
},
329+
}
330+
331+
for _, tt := range tests {
332+
t.Run(tt.name, func(t *testing.T) {
333+
var b []byte
334+
var err error
335+
336+
if b, err = json.Marshal(&tt.mode); (err != nil) != tt.wantErr {
337+
t.Fatalf("MarshalJSON() error = %v, want %v", err, tt.wantErr)
338+
}
339+
340+
got := string(b[:])
341+
342+
if got != tt.want {
343+
t.Errorf("got mode %v, want %v", got, tt.want)
344+
}
345+
})
346+
}
347+
}

0 commit comments

Comments
 (0)