/
query.go
165 lines (145 loc) · 4.75 KB
/
query.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/stevedomin/termtable"
"github.com/tent/tent-client-go"
"strconv"
"strings"
"time"
)
func CmdQuery(c *Config) *cobra.Command {
var limit int
var since string // 1234567890,version
var before string // 1234567890,version
var until string // 1234567890,version
var entities string // entityone,entitytwo
var types string // typeone,typetwo
var maxRefs int
var sortBy string // r,p,vr,vp
// var mentions string // mentionone,mentiontwo
var profile string
cmd := &cobra.Command{
Use: "query",
Short: "Query the posts feed.",
Long: `
Query the posts feed of the default profile.
Find more information about the available parameters here: https://tent.io/docs/api#postsfeed
Join multiple values with commata, i.e. when using --entities or --types.
A note about --types and fragments:
- "--types=https://tent.io/types/status/v0" matches all fragments
- "--types=https://tent.io/types/status/v0#" just matches "https://tent.io/types/status/v0#"
- "--types=https://tent.io/types/status/v0#reply" just matches "https://tent.io/types/status/v0#reply"
`,
Run: func(cmd *cobra.Command, args []string) {
p, err := c.Profile(profile)
maybeExit(err)
client := p.Client()
q := tent.NewPostsFeedQuery()
q.Limit(limit)
q.MaxRefs(maxRefs)
timeSetter := map[string]func(time.Time, string) *tent.PostsFeedQuery{
since: q.Since,
before: q.Before,
until: q.Until,
}
for arg, setter := range timeSetter {
err = splitAndSetTimeValue(arg, setter)
maybeExit(err)
}
if entities != "" {
entitiesStr, err := splitAndMaybeReplace(entities, func(name string) (string, error) {
if i, p := c.ProfileByName(name); i > -1 {
return p.Entity, nil
}
if !isURL(name) {
return "", fmt.Errorf(`Profile "%v" not found.`, name)
}
return name, nil
})
maybeExit(err)
q.Set("entities", entitiesStr)
}
if types != "" {
typesStr, err := splitAndMaybeReplace(types, func(name string) (string, error) {
if i, s := c.SchemaByName(name); i > -1 {
return s.MergeFragment(name), nil
}
if !isURL(name) {
return "", fmt.Errorf(`Schema "%v" not found.`, name)
}
return name, nil
})
maybeExit(err)
q.Set("types", typesStr)
}
if sortBy != "" {
order, err := matchSortOrder(sortBy)
maybeExit(err)
q.SortBy(order)
}
res, err := client.GetFeed(q, nil)
maybeExit(err)
t := termtable.NewTable(nil, nil)
t.SetHeader([]string{"ID", "ENTITY", "TYPE", "PUBLISHED_AT"})
for _, p := range res.Posts {
// layout := "2006-01-02 15:04" // p.PublishedAt.Format(layout)
pubAt := strconv.FormatInt(p.PublishedAt.UnixMillis(), 10)
t.AddRow([]string{p.ID, p.Entity, p.Type, pubAt})
}
fmt.Println(t.Render())
},
}
cmd.Flags().IntVarP(&limit, "limit", "l", 25, "Cap number of returned posts.")
cmd.Flags().StringVarP(&since, "since", "s", "", "Only posts since timestamp and/or version. Format: `123456789,versionid`")
cmd.Flags().StringVarP(&before, "before", "b", "", "Only posts before timestamp and/or version. Format: `123456789,versionid`")
cmd.Flags().StringVarP(&until, "until", "u", "", "Only posts until timestamp and/or version. Format: `123456789,versionid`")
cmd.Flags().StringVarP(&entities, "entities", "e", "", "Only posts from specific entities.")
cmd.Flags().StringVarP(&types, "types", "t", "", "Only posts with specific types.")
cmd.Flags().IntVarP(&maxRefs, "maxrefs", "r", 5, "Cap number of inlined refs per post.")
cmd.Flags().StringVarP(&sortBy, "sortBy", "o", "", `Define sort order. Possible values: "p" published_at, "r" received_at, "vp" version.published_at, "vr" version.received_at`)
// cmd.Flags().StringVarP(&mentions, "mentions", "m", "", "Only posts which mention specific entities.")
setUseFlag(&profile, cmd)
return cmd
}
func splitAndSetTimeValue(arg string, setter func(time.Time, string) *tent.PostsFeedQuery) error {
if arg != "" {
s := strings.Split(arg, ",")
t := tent.UnixTime{}
err := t.UnmarshalJSON([]byte(s[0]))
if err != nil {
return err
}
if len(s) == 1 {
setter(t.Time, "")
} else {
setter(t.Time, s[1])
}
}
return nil
}
func splitAndMaybeReplace(s string, get func(string) (string, error)) (string, error) {
res := []string{}
for _, part := range strings.Split(s, ",") {
p, err := get(part)
if err != nil {
return "", err
}
res = append(res, p)
}
return strings.Join(res, ","), nil
}
func matchSortOrder(s string) (tent.SortOrder, error) {
switch s {
case "p":
return tent.PublishedAt, nil
case "r":
return tent.ReceivedAt, nil
case "vp":
return tent.VersionPublishedAt, nil
case "vr":
return tent.VersionReceivedAt, nil
default:
return -1, fmt.Errorf("No match for %s", s)
}
}