AT Protocol IPLD-CAR Repository toolkit (CLI)
1package cmd
2
3import (
4 "context"
5 "encoding/hex"
6 "fmt"
7 "os"
8 "sort"
9 "strings"
10
11 "github.com/atscan/atr/cli"
12 "github.com/atscan/atr/engine"
13 "github.com/atscan/atr/repo"
14 "github.com/dustin/go-humanize"
15 "github.com/fatih/color"
16 "github.com/spf13/cobra"
17)
18
19func init() {
20 rootCmd.AddCommand(LsCmd)
21}
22
23var LsCmd = &cobra.Command{
24 Use: "inspect",
25 Aliases: []string{"i"},
26 Short: "Inspect repo(s)",
27 Long: ``,
28 Run: func(cmd *cobra.Command, args []string) {
29 ctx := cli.Context{
30 WorkingDir: workingDir,
31 Args: args,
32 }
33 fmt.Println("")
34 walk := func(ss repo.RepoSnapshot, err error) {
35 if ss.Root.String() == "b" {
36 return
37 }
38 if ss.File == "" {
39 ss.File = "(pipe)"
40 }
41 yellow := color.New(color.FgYellow).SprintFunc()
42 cyan := color.New(color.FgCyan).SprintFunc()
43 boldCyan := color.New(color.FgCyan, color.Bold).SprintFunc()
44 green := color.New(color.FgGreen).SprintFunc()
45
46 fmt.Printf("%v:\n", yellow(ss.File))
47 fmt.Printf(" DID: %s Repo Version: %v\n", boldCyan(ss.Repo.SignedCommit().Did), cyan(ss.Repo.SignedCommit().Version))
48 fmt.Printf(" Head: %s\n", cyan(ss.Root.String()))
49 fmt.Printf(" Sig: %s\n", cyan(hex.EncodeToString(ss.Repo.SignedCommit().Sig)))
50
51 stats, _ := ss.GetCollectionStats("")
52 keys := make([]string, 0, len(stats))
53 total := 0
54 for k := range stats {
55 keys = append(keys, k)
56 total += stats[k]
57 }
58 sort.Strings(keys)
59 cp, _ := ss.Repo.GetCommitsPath(-1)
60 _, v, _ := ss.Repo.GetRecord(context.TODO(), "app.bsky.actor.profile/self")
61
62 dn := v["displayName"]
63 if dn != nil {
64 dn = boldCyan(dn)
65 } else {
66 dn = "(empty)"
67 }
68 desc := v["description"]
69 if desc != nil {
70 desc = cyan(strings.Replace(desc.(string), "\n", "\n ", -1))
71 } else {
72 desc = "(empty)"
73 }
74
75 fmt.Printf(" Size: %s Blocks: %v Commits: %v Objects: %v\n", cyan(humanize.Bytes(uint64(ss.Size))), cyan(humanize.Comma(int64(ss.Repo.Blocks))), cyan(humanize.Comma(int64(len(cp)))), cyan(humanize.Comma(int64(total))))
76 fmt.Printf(" Profile:\n")
77 fmt.Printf(" Display Name: %v\n", dn)
78 fmt.Printf(" Description: %v\n", desc)
79
80 fmt.Printf(" Collections:\n")
81 for _, k := range keys {
82 fmt.Printf(" %s: %v\n", green(k), cyan(humanize.Comma(int64(stats[k]))))
83 }
84 fmt.Printf(" Last 5 commits:\n")
85 for i, cid := range cp {
86 fmt.Printf(" %v\n", cyan(cid.String()))
87 if i >= 5 {
88 break
89 }
90 }
91 if len(cp) > 5 {
92 fmt.Printf(" %s\n", cyan("..."))
93 }
94
95 fmt.Println("")
96 }
97
98 stat, _ := os.Stdin.Stat()
99 if (stat.Mode() & os.ModeCharDevice) == 0 {
100 // data is being piped to stdin
101 engine.WalkStream(&ctx, os.Stdin, walk)
102 } else {
103 //stdin is from a terminal
104 engine.WalkFiles(&ctx, walk)
105 }
106 },
107}