-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlex_diff.go
More file actions
95 lines (82 loc) · 2 KB
/
lex_diff.go
File metadata and controls
95 lines (82 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"context"
"encoding/json"
"fmt"
"reflect"
"github.com/bluesky-social/indigo/atproto/atdata"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/urfave/cli/v3"
"github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
)
var cmdLexDiff = &cli.Command{
Name: "diff",
Usage: "print differences for any updated lexicon schemas",
ArgsUsage: `<file-or-dir>*`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "lexicons-dir",
Value: "lexicons/",
Usage: "base directory for project Lexicon files",
Sources: cli.EnvVars("LEXICONS_DIR"),
},
},
Action: runLexDiff,
}
func runLexDiff(ctx context.Context, cmd *cli.Command) error {
return runComparisons(ctx, cmd, compareDiff)
}
func compareDiff(ctx context.Context, cmd *cli.Command, nsid syntax.NSID, localJSON, remoteJSON json.RawMessage) error {
// skip schemas which aren't in both locations
if localJSON == nil || remoteJSON == nil {
return nil
}
local, err := atdata.UnmarshalJSON(localJSON)
if err != nil {
return err
}
remote, err := atdata.UnmarshalJSON(remoteJSON)
if err != nil {
return err
}
delete(local, "$type")
delete(remote, "$type")
// skip if rqual
if reflect.DeepEqual(local, remote) {
return nil
}
// re-marshal with type removed
localJSON, err = json.Marshal(local)
if err != nil {
return err
}
remoteJSON, err = json.Marshal(remote)
if err != nil {
return err
}
// compute and print diff
var diffString string
var outJSON map[string]interface{}
differ := gojsondiff.New()
d, err := differ.Compare(localJSON, remoteJSON)
if err != nil {
return nil
}
json.Unmarshal(localJSON, &outJSON)
config := formatter.AsciiFormatterConfig{
//ShowArrayIndex: true,
Coloring: true,
}
formatter := formatter.NewAsciiFormatter(outJSON, config)
diffString, err = formatter.Format(d)
if err != nil {
return err
}
fmt.Printf("diff %s\n", nsid)
fmt.Println("--- local")
fmt.Println("+++ remote")
fmt.Print(diffString)
fmt.Println()
return nil
}