-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpost.go
317 lines (274 loc) · 6.56 KB
/
post.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"context"
"flag"
"fmt"
"io"
"log/slog"
"os"
"strings"
"time"
"github.com/adrg/frontmatter"
"github.com/gnolang/gno/gno.land/pkg/gnoclient"
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/tm2/pkg/commands"
)
type cliCfg struct {
Publish bool
Edit bool
GasWanted int64
GasFee string
ChainId string
BlogRealmPath string
KeyName string
GnoHome string
Remote string
Quiet bool
InsecurePasswordStdIn bool
ConfirmInput bool
}
func newPostCommand(io commands.IO) *commands.Command {
var (
fs = flag.NewFlagSet("Post", flag.ExitOnError)
cfg = &cliCfg{}
)
// Register flagset
cfg.RegisterFlags(fs)
// Make Post command
return commands.NewCommand(
commands.Metadata{
Name: "post",
ShortUsage: "post <FILE OR FILES_DIR> [flags]",
LongHelp: `Post one or more files. Passing in a file will post that single file, while passing in a directory will search for all README.md files and batch post them.`,
},
cfg,
func(_ context.Context, args []string) error {
return execPost(io, args, cfg)
},
)
}
// RegisterFlags registers flags for cliCfg
func (cfg *cliCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(&cfg.KeyName,
"key",
"",
"name of keypair to use for deployment",
)
fs.BoolVar(&cfg.Publish,
"publish",
false,
"publish blogpost",
)
fs.BoolVar(&cfg.Edit,
"edit",
false,
"edit mode",
)
fs.Int64Var(&cfg.GasWanted,
"gas-wanted",
5000000,
"gas requested for tx",
)
fs.StringVar(&cfg.GasFee,
"gas-fee",
"1000000ugnot",
"gas payment fee",
)
fs.StringVar(&cfg.ChainId,
"chainid",
"dev",
"chain ID",
)
fs.StringVar(&cfg.BlogRealmPath,
"pkgpath",
"gno.land/r/gnoland/blog",
"blog realm path",
)
fs.StringVar(&cfg.GnoHome,
"home",
gnoenv.HomeDir(),
"home directory",
)
fs.StringVar(&cfg.Remote,
"remote",
"localhost:26657",
"remote node URL",
)
fs.BoolVar(&cfg.InsecurePasswordStdIn,
"insecure-password-stdin",
false,
"WARNING! take password from stdin",
)
fs.BoolVar(&cfg.ConfirmInput,
"confirm-input",
false,
"ask user to confirm input",
)
}
func execPost(io commands.IO, args []string, cfg *cliCfg) error {
if len(args) != 1 {
return ErrInvalidNumberOfArgs
}
if cfg.KeyName == "" {
return ErrEmptyKeyName
}
// Ask user for confirming the ChainID
if cfg.ConfirmInput && !askForConfirmation(cfg.ChainId, cfg.KeyName) {
fmt.Println("operation canceled by the user, exiting")
return nil
}
// Stat passed in arg
fileInfo, err := os.Stat(args[0])
if err != nil {
return fmt.Errorf("unable to stat %q: %w", args[0], err)
}
var pass string
if cfg.Quiet {
pass, err = io.GetPassword("", cfg.InsecurePasswordStdIn)
} else {
pass, err = io.GetPassword("Enter password:", cfg.InsecurePasswordStdIn)
}
if err != nil {
return err
}
// Initialize signer
signer, err := initSigner(cfg, pass)
if err != nil {
return err
}
// Initialize Gnoclient
rpc, err := initRPCClient(cfg)
if err != nil {
return err
}
client := gnoclient.Client{
Signer: signer,
RPCClient: rpc,
}
// Batch Post request passed in with root argument
if fileInfo.IsDir() {
// Find file paths
files, err := findFilePaths(args[0])
if err != nil {
return err
}
return post(client, cfg, files...)
}
// Single Post request passed in an argument
return post(client, cfg, args[0])
}
func post(c gnoclient.Client, cfg *cliCfg, paths ...string) error {
msgs := make([]vm.MsgCall, 0, len(paths))
// Get account info
account, err := c.Signer.Info()
if err != nil {
return fmt.Errorf("getting signer info failed: %w", err)
}
address := account.GetAddress()
signingAcc, _, err := c.QueryAccount(address)
if err != nil {
return fmt.Errorf("query account %q failed: %w", account, err)
}
nonce := signingAcc.GetSequence()
accNumber := signingAcc.GetAccountNumber()
// Set up base config
baseTxCfg := gnoclient.BaseTxCfg{
GasFee: cfg.GasFee,
GasWanted: cfg.GasWanted,
AccountNumber: accNumber,
SequenceNumber: nonce,
Memo: "Posted from gnoblog-cli",
}
// Save title for printing to cli
var postTitle string
// Go through Post(s)
for _, postPath := range paths {
// Open file
postFile, err := os.Open(postPath)
if err != nil {
return fmt.Errorf("cannot open file %q: %w", postPath, err)
}
// Parse Post
post, err := parsePost(postFile)
if err != nil {
fmt.Printf("skipping post at %q, cannot parse: %v\n", postPath, err)
continue
}
// Define function to call on the blog realm
verb := "ModAddPost"
// Check if Post already exists on chain
existsExpr := "PostExists(\"" + post.Slug + "\")"
exists, _, err := c.QEval(cfg.BlogRealmPath, existsExpr)
if err != nil {
slog.Error("error while checking if Post exists", "error", err, "slug", post.Slug)
}
bExists := strings.Contains(exists, "true")
if cfg.Edit {
if !bExists {
return fmt.Errorf("%s is not on chain yet - disable the edit flag\n", post.Title)
}
// If Post exists, and user wants to edit it, use ModEditPost
verb = "ModEditPost"
} else if bExists {
// if a post is already on chain, and we are not editing it, just skip it
// otherwise, batch transactions will fail if a single MsgCall fails
continue
}
callMsg := vm.MsgCall{
Caller: address,
Send: nil,
PkgPath: cfg.BlogRealmPath,
Func: verb,
Args: []string{
post.Slug,
post.Title,
post.Body,
post.PublicationDate.Format(time.RFC3339),
strings.Join(post.Authors, ","),
strings.Join(post.Tags, ","),
},
}
msgs = append(msgs, callMsg)
postTitle = post.Title
}
if len(msgs) == 0 {
return fmt.Errorf("%w, exiting", ErrNoNewOrChangedPosts)
}
_, err = c.Call(baseTxCfg, msgs...)
if err != nil {
return err
}
// Print success messages
action := "posted"
if cfg.Edit {
action = "edited"
}
if len(msgs) == 1 {
fmt.Printf("Successfully %s \"%s\"!\n", action, postTitle)
} else {
fmt.Printf("Successfully %s %d posts!\n", action, len(msgs))
}
return nil
}
func parsePost(reader io.Reader) (*Post, error) {
var p Post
rest, err := frontmatter.MustParse(reader, &p)
if err != nil {
return nil, fmt.Errorf("invalid Post frontmatter: %w", err)
}
body := string(rest)
p.Title, err = extractTitle(body)
if err != nil {
return nil, err
}
p.Body = removeTitle(body, p.Title)
if len(p.Tags) != 0 {
p.Tags = removeWhitespace(p.Tags)
}
if p.PublicationDate == nil {
now := time.Now()
p.PublicationDate = &now
}
return &p, nil
}