go scratch code for atproto
at main 1.7 kB view raw
1package main 2 3import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "reflect" 8 9 "github.com/bluesky-social/indigo/atproto/atdata" 10 "github.com/bluesky-social/indigo/atproto/syntax" 11 12 "github.com/urfave/cli/v3" 13) 14 15var cmdLexStatus = &cli.Command{ 16 Name: "status", 17 Usage: "check if local lexicons are in-sync with live network", 18 Description: "Enumerates all local lexicons (JSON files), and checks for changes against the live network\nWill detect new published lexicons under a known lexicon group, but will not discover new groups under the same domain prefix.\nOperates on entire ./lexicons/ directory unless specific files or directories are provided.", 19 ArgsUsage: `<file-or-dir>*`, 20 Flags: []cli.Flag{ 21 &cli.StringFlag{ 22 Name: "lexicons-dir", 23 Value: "lexicons/", 24 Usage: "base directory for project Lexicon files", 25 Sources: cli.EnvVars("LEXICONS_DIR"), 26 }, 27 }, 28 Action: runLexStatus, 29} 30 31func runLexStatus(ctx context.Context, cmd *cli.Command) error { 32 return runComparisons(ctx, cmd, compareStatus) 33} 34 35func compareStatus(ctx context.Context, cmd *cli.Command, nsid syntax.NSID, localJSON, remoteJSON json.RawMessage) error { 36 37 // new remote schema (missing local) 38 if localJSON == nil { 39 fmt.Printf(" ⭕ %s\n", nsid) 40 return nil 41 } 42 43 // new unpublished local schema 44 if remoteJSON == nil { 45 fmt.Printf(" 🟠 %s\n", nsid) 46 return nil 47 } 48 49 local, err := atdata.UnmarshalJSON(localJSON) 50 if err != nil { 51 return err 52 } 53 remote, err := atdata.UnmarshalJSON(remoteJSON) 54 if err != nil { 55 return err 56 } 57 delete(local, "$type") 58 delete(remote, "$type") 59 if reflect.DeepEqual(local, remote) { 60 fmt.Printf(" 🟢 %s\n", nsid) 61 } else { 62 fmt.Printf(" 🟣 %s\n", nsid) 63 } 64 return nil 65}