Skip to content

Commit 0940cdd

Browse files
authored
format output with hujsonfmt (#32)
Updates #29
1 parent 8ea8a2d commit 0940cdd

File tree

5 files changed

+243
-229
lines changed

5 files changed

+243
-229
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ testdata:
44
-v \
55
-f testdata/input-parent.hujson \
66
-d testdata/departments/ \
7-
-o testdata/output-file-to-compare-to.hujson \
8-
-allow=acls,autoApprovers,grants,groups,ipsets,ssh,tests,sshTests
7+
-allow=acls,autoApprovers,grants,groups,ipsets,ssh,tests,sshTests \
8+
-o testdata/output-file-to-compare-to.hujson

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
module github.com/tailscale-dev/tailscale-acl-combiner
22

3-
go 1.21
3+
go 1.23
4+
5+
toolchain go1.23.10
46

57
require github.com/creachadair/jtree v0.0.0-20231211041502-6ba355703cad
68

9+
require github.com/tailscale/hujson v0.0.0-20250605163823-992244df8c5a
10+
711
require (
812
go4.org/mem v0.0.0-20220726221520-4f986261bf13 // indirect
913
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/creachadair/jtree v0.0.0-20231211041502-6ba355703cad h1:2MMMgC7ORRjJH
22
github.com/creachadair/jtree v0.0.0-20231211041502-6ba355703cad/go.mod h1:WP8iLZIRvdwzYE3ahHTQVJa3AvAjQLnSUHl/IfXPzeQ=
33
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
44
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
5-
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a h1:SJy1Pu0eH1C29XwJucQo73FrleVK6t4kYz4NVhp34Yw=
6-
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8=
5+
github.com/tailscale/hujson v0.0.0-20250605163823-992244df8c5a h1:a6TNDN9CgG+cYjaeN8l2mc4kSz2iMiCDQxPEyltUV/I=
6+
github.com/tailscale/hujson v0.0.0-20250605163823-992244df8c5a/go.mod h1:EbW0wDK/qEUYI0A5bqq0C2kF8JTQwWONmGDBbzsxxHo=
77
go4.org/mem v0.0.0-20220726221520-4f986261bf13 h1:CbZeCBZ0aZj8EfVgnqQcYZgf0lpZ3H9rmp5nkDTAst8=
88
go4.org/mem v0.0.0-20220726221520-4f986261bf13/go.mod h1:reUoABIJ9ikfM5sgtSF3Wushcza7+WeD01VB9Lirh3g=
99
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 h1:/yRP+0AN7mf5DkD3BAI6TOFnd51gEoDEb8o35jIFtgw=

main.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/creachadair/jtree/ast"
1515
"github.com/creachadair/jtree/jwcc"
16+
"github.com/tailscale/hujson"
1617
)
1718

1819
var (
@@ -304,6 +305,17 @@ func gatherChildren(path string) ([]*ParsedDocument, error) {
304305
}
305306

306307
func outputFile(doc *jwcc.Object) error {
308+
var sb strings.Builder
309+
err := jwcc.Format(&sb, doc)
310+
if err != nil {
311+
return err
312+
}
313+
314+
formatted, err := hujson.Format([]byte(sb.String()))
315+
if err != nil {
316+
return err
317+
}
318+
307319
if *outFile != "" {
308320
f, err := os.Create(*outFile)
309321
if err != nil {
@@ -312,18 +324,10 @@ func outputFile(doc *jwcc.Object) error {
312324
defer f.Close()
313325

314326
w := bufio.NewWriter(f)
315-
err = jwcc.Format(w, doc)
316-
if err != nil {
317-
return err
318-
}
319-
w.WriteString("\n")
327+
w.Write(formatted)
320328
w.Flush()
321329
} else {
322-
err := jwcc.Format(os.Stdout, doc)
323-
if err != nil {
324-
return err
325-
}
326-
fmt.Printf("\n")
330+
fmt.Print(string(formatted))
327331
}
328332
return nil
329333
}

0 commit comments

Comments
 (0)