forked from textileio/go-textile
/
blocks.go
150 lines (124 loc) · 3.13 KB
/
blocks.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cmd
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"github.com/textileio/textile-go/core"
)
var errMissingBlockId = errors.New("missing block ID")
func init() {
register(&blocksCmd{})
}
type blocksCmd struct {
List lsBlocksCmd `command:"ls" description:"Paginate thread blocks"`
Get getBlocksCmd `command:"get" description:"Get a thread block"`
}
func (x *blocksCmd) Name() string {
return "blocks"
}
func (x *blocksCmd) Short() string {
return "View thread blocks"
}
func (x *blocksCmd) Long() string {
return `
Blocks are the raw components in a thread. Think of them as an
append-only log of thread updates where each update is hash-linked
to its parent(s). New / recovering peers can sync history by simply
traversing the hash tree.
There are several block types:
- JOIN: Peer joined.
- ANNOUNCE: Peer set username / inbox address
- LEAVE: Peer left.
- FILES: File(s) added.
- MESSAGE: Text message added.
- COMMENT: Comment added to another block.
- LIKE: Like added to another block.
- MERGE: 3-way merge added.
- IGNORE: Another block was ignored.
- FLAG: A flag was added to another block.
Use this command to get and list blocks in a thread.
`
}
type lsBlocksCmd struct {
Client ClientOptions `group:"Client Options"`
Thread string `short:"t" long:"thread" description:"Thread ID. Omit for default."`
Offset string `short:"o" long:"offset" description:"Offset ID to start listing from."`
Limit int `short:"l" long:"limit" description:"List page size." default:"5"`
}
func (x *lsBlocksCmd) Usage() string {
return `
Paginates blocks in a thread.
`
}
func (x *lsBlocksCmd) Execute(args []string) error {
setApi(x.Client)
opts := map[string]string{
"thread": x.Thread,
"offset": x.Offset,
"limit": strconv.Itoa(x.Limit),
}
return callLsBlocks(opts)
}
func callLsBlocks(opts map[string]string) error {
if opts["thread"] == "" {
opts["thread"] = "default"
}
var list []core.BlockInfo
res, err := executeJsonCmd(GET, "blocks", params{opts: opts}, &list)
if err != nil {
return err
}
output(res)
limit, err := strconv.Atoi(opts["limit"])
if err != nil {
return err
}
if len(list) < limit {
return nil
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("next page...")
if _, err := reader.ReadString('\n'); err != nil {
return err
}
return callLsBlocks(map[string]string{
"thread": opts["thread"],
"offset": list[len(list)-1].Id,
"limit": opts["limit"],
})
}
type getBlocksCmd struct {
Client ClientOptions `group:"Client Options"`
}
func (x *getBlocksCmd) Usage() string {
return `
Gets a thread block by ID.
`
}
func (x *getBlocksCmd) Execute(args []string) error {
setApi(x.Client)
if len(args) == 0 {
return errMissingBlockId
}
var info *core.BlockInfo
res, err := executeJsonCmd(GET, "blocks/"+args[0], params{}, &info)
if err != nil {
return err
}
output(res)
return nil
}
func callRmBlocks(args []string) error {
if len(args) == 0 {
return errMissingBlockId
}
var info *core.BlockInfo
res, err := executeJsonCmd(DEL, "blocks/"+args[0], params{}, &info)
if err != nil {
return err
}
output(res)
return nil
}