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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/repo"
"github.com/bluesky-social/indigo/xrpc"
"github.com/ipfs/go-cid"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
flag.Parse()
ctx := context.Background()
atid, err := syntax.ParseAtIdentifier(flag.Arg(0))
if err != nil {
return err
}
dir := identity.DefaultDirectory()
ident, err := dir.Lookup(ctx, *atid)
if err != nil {
return err
}
if ident.PDSEndpoint() == "" {
return fmt.Errorf("no PDS endpoint for identity")
}
fmt.Println(ident.PDSEndpoint())
carPath := ident.DID.String() + ".car"
xrpcc := xrpc.Client{
Host: ident.PDSEndpoint(),
}
repoBytes, err := atproto.SyncGetRepo(ctx, &xrpcc, ident.DID.String(), "")
if err != nil {
return err
}
err = os.WriteFile(carPath, repoBytes, 0666)
if err != nil {
return err
}
if err := carList(carPath); err != nil {
return err
}
return nil
}
func carList(carPath string) error {
ctx := context.Background()
fi, err := os.Open(carPath)
if err != nil {
return fmt.Errorf("failed to open car file: %w", err)
}
defer fi.Close()
// read repository tree into memory
r, err := repo.ReadRepoFromCar(ctx, fi)
if err != nil {
return fmt.Errorf("failed to read repository from car file: %w", err)
}
// extract DID from repo commit
sc := r.SignedCommit()
did, err := syntax.ParseDID(sc.Did)
if err != nil {
return fmt.Errorf("failed to parse DID from signed commit: %w", err)
}
topDir := did.String()
// iterate over all of the records by key and CID
err = r.ForEach(ctx, "", func(k string, v cid.Cid) error {
fmt.Printf("%s\t%s\n", k, v.String())
recPath := topDir + "/" + k
if err := os.MkdirAll(filepath.Dir(recPath), os.ModePerm); err != nil {
return fmt.Errorf("failed to create directories for record path: %w", err)
}
// fetch the record CBOR and convert to a golang struct
_, rec, err := r.GetRecord(ctx, k)
if err != nil {
return fmt.Errorf("failed to get record for key %s: %w", k, err)
}
// serialize as JSON
recJson, err := json.MarshalIndent(rec, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal record to JSON for key %s: %w", k, err)
}
if err := os.WriteFile(recPath+".json", recJson, 0666); err != nil {
return fmt.Errorf("failed to write JSON file for key %s: %w", k, err)
}
return nil
})
if err != nil {
return fmt.Errorf("failed to iterate over records: %w", err)
}
return nil
}
|