tangled
alpha
login
or
join now
bnewbold.net
/
cobalt
13
fork
atom
go scratch code for atproto
13
fork
atom
overview
issues
pulls
pipelines
basic diff support
bnewbold.net
5 months ago
55c649ce
09b353d9
+97
2 changed files
expand all
collapse all
unified
split
cmd
glot
diff.go
main.go
+96
cmd/glot/diff.go
···
1
1
+
package main
2
2
+
3
3
+
import (
4
4
+
"context"
5
5
+
"encoding/json"
6
6
+
"fmt"
7
7
+
"reflect"
8
8
+
9
9
+
"github.com/bluesky-social/indigo/atproto/data"
10
10
+
"github.com/bluesky-social/indigo/atproto/syntax"
11
11
+
12
12
+
"github.com/urfave/cli/v3"
13
13
+
"github.com/yudai/gojsondiff"
14
14
+
"github.com/yudai/gojsondiff/formatter"
15
15
+
)
16
16
+
17
17
+
var cmdDiff = &cli.Command{
18
18
+
Name: "diff",
19
19
+
Usage: "print differences for any updated lexicon schemas",
20
20
+
ArgsUsage: `<file-or-dir>*`,
21
21
+
Flags: []cli.Flag{
22
22
+
&cli.StringFlag{
23
23
+
Name: "lexicons-dir",
24
24
+
Value: "./lexicons/",
25
25
+
Usage: "base directory for project Lexicon files",
26
26
+
Sources: cli.EnvVars("LEXICONS_DIR"),
27
27
+
},
28
28
+
},
29
29
+
Action: runDiff,
30
30
+
}
31
31
+
32
32
+
func runDiff(ctx context.Context, cmd *cli.Command) error {
33
33
+
return compareSchemas(ctx, cmd, diffCompare)
34
34
+
}
35
35
+
36
36
+
func diffCompare(ctx context.Context, cmd *cli.Command, nsid syntax.NSID, localJSON, remoteJSON json.RawMessage) error {
37
37
+
38
38
+
// skip schemas which aren't in both locations
39
39
+
if localJSON == nil || remoteJSON == nil {
40
40
+
return nil
41
41
+
}
42
42
+
43
43
+
local, err := data.UnmarshalJSON(localJSON)
44
44
+
if err != nil {
45
45
+
return err
46
46
+
}
47
47
+
remote, err := data.UnmarshalJSON(remoteJSON)
48
48
+
if err != nil {
49
49
+
return err
50
50
+
}
51
51
+
delete(local, "$type")
52
52
+
delete(remote, "$type")
53
53
+
54
54
+
// skip if rqual
55
55
+
if reflect.DeepEqual(local, remote) {
56
56
+
return nil
57
57
+
}
58
58
+
59
59
+
// re-marshal with type removed
60
60
+
localJSON, err = json.Marshal(local)
61
61
+
if err != nil {
62
62
+
return err
63
63
+
}
64
64
+
remoteJSON, err = json.Marshal(remote)
65
65
+
if err != nil {
66
66
+
return err
67
67
+
}
68
68
+
69
69
+
// compute and print diff
70
70
+
var diffString string
71
71
+
var outJSON map[string]interface{}
72
72
+
differ := gojsondiff.New()
73
73
+
d, err := differ.Compare(localJSON, remoteJSON)
74
74
+
if err != nil {
75
75
+
return nil
76
76
+
}
77
77
+
json.Unmarshal(localJSON, &outJSON)
78
78
+
config := formatter.AsciiFormatterConfig{
79
79
+
//ShowArrayIndex: true,
80
80
+
// TODO: Coloring: c.Bool("coloring"),
81
81
+
Coloring: true,
82
82
+
}
83
83
+
formatter := formatter.NewAsciiFormatter(outJSON, config)
84
84
+
diffString, err = formatter.Format(d)
85
85
+
if err != nil {
86
86
+
return err
87
87
+
}
88
88
+
89
89
+
fmt.Printf("diff %s\n", nsid)
90
90
+
fmt.Println("--- local")
91
91
+
fmt.Println("+++ remote")
92
92
+
fmt.Print(diffString)
93
93
+
fmt.Println()
94
94
+
95
95
+
return nil
96
96
+
}
+1
cmd/glot/main.go
···
39
39
cmdLint,
40
40
cmdPull,
41
41
cmdStatus,
42
42
+
cmdDiff,
42
43
}
43
44
return app.Run(context.Background(), args)
44
45
}