go scratch code for atproto
1package main
2
3import (
4 "context"
5 "fmt"
6 "sort"
7
8 comatproto "github.com/bluesky-social/indigo/api/atproto"
9 "github.com/bluesky-social/indigo/atproto/atclient"
10 "github.com/bluesky-social/indigo/atproto/identity"
11 "github.com/bluesky-social/indigo/atproto/syntax"
12
13 "github.com/urfave/cli/v3"
14)
15
16var cmdLexUnpublish = &cli.Command{
17 Name: "unpublish",
18 Usage: "delete lexicon schema records from current account",
19 Description: "Deletes published schema records from current AT account repository.\nDoes not delete local schema JSON files.",
20 ArgsUsage: `<nsid>+`,
21 Flags: []cli.Flag{
22 &cli.StringFlag{
23 Name: "username",
24 Aliases: []string{"u"},
25 Usage: "account identifier (handle or DID) for login",
26 Sources: cli.EnvVars("GLOT_USERNAME", "ATP_USERNAME"),
27 },
28 &cli.StringFlag{
29 Name: "password",
30 Aliases: []string{"p"},
31 Usage: "account password (app password) for login",
32 Sources: cli.EnvVars("GLOT_PASSWORD", "ATP_PASSWORD", "PASSWORD"),
33 },
34 },
35 Action: runLexUnpublish,
36}
37
38func runLexUnpublish(ctx context.Context, cmd *cli.Command) error {
39
40 if cmd.Args().Len() == 0 {
41 cli.ShowSubcommandHelpAndExit(cmd, 1)
42 }
43
44 user := cmd.String("username")
45 pass := cmd.String("password")
46 if user == "" || pass == "" {
47 return fmt.Errorf("requires account credentials")
48 }
49 atid, err := syntax.ParseAtIdentifier(user)
50 if err != nil {
51 return fmt.Errorf("invalid AT account identifier %s: %w", user, err)
52 }
53
54 cdir := identity.DefaultDirectory()
55 c, err := atclient.LoginWithPassword(ctx, cdir, atid, pass, "", nil)
56 if err != nil {
57 return nil
58 }
59 if c.AccountDID == nil {
60 return fmt.Errorf("require API client to have DID configured")
61 }
62
63 nsids := []string{}
64 for _, arg := range cmd.Args().Slice() {
65 n, err := syntax.ParseNSID(arg)
66 if err != nil {
67 return err
68 }
69 nsids = append(nsids, n.String())
70 }
71 sort.Strings(nsids)
72
73 for _, nsid := range nsids {
74 if err := unpublishSchema(ctx, c, syntax.NSID(nsid)); err != nil {
75 fmt.Printf(" 🟠 %s\n", nsid)
76 fmt.Printf(" record deletion failed: %s\n", err.Error())
77 continue
78 }
79 fmt.Printf(" 🟢 %s\n", nsid)
80 }
81
82 return nil
83}
84
85func unpublishSchema(ctx context.Context, c *atclient.APIClient, nsid syntax.NSID) error {
86
87 resp, err := comatproto.RepoDeleteRecord(ctx, c, &comatproto.RepoDeleteRecord_Input{
88 Collection: schemaNSID.String(),
89 Repo: c.AccountDID.String(),
90 Rkey: nsid.String(),
91 })
92 if err != nil {
93 return err
94 }
95
96 if resp.Commit == nil {
97 return fmt.Errorf("schema record did not exist")
98 }
99
100 return nil
101}