Skip to content

Commit fc0e5bc

Browse files
leohhhnmoul
andauthored
fix: refactor gnoblog-cli (#63)
* start refac * add cmds * wip setup post command * wip save, move things around * finished singlepost * added comments * extract errors * support multicall * update gno version * update titles & remove title frontmatter * add title & disclaimer to poc2 for consistency * update cfg caller * remove validate-chain func * fix post title parsing * fix * chore: better error output Signed-off-by: moul <[email protected]> * chore: fmt + tidy Signed-off-by: moul <[email protected]> * chore: fix frontmatter parsing Signed-off-by: moul <[email protected]> * chore: add missing errcheck Signed-off-by: moul <[email protected]> * chore: fixup Signed-off-by: moul <[email protected]> * move folders * add gitignore, fix package names * unify post function, minor modifications * fix md text in posts * move err check * move IO init to main * add comments, fix removeWhitespace * clarify error * add key check * move parsePost, change err check * change post func signature * add initial test & mocks * integration test setup * remove ./build * fix leftover titles * add output confirmation * wip integ tests * remove integration test setup * use tm2/pkg/commands instead of ffcli * update err message * updat err msg again * add condition * make if more readable, add exit condition * go mod tidy * add readme * update readme * update makefile, chagne error * update main readme --------- Signed-off-by: moul <[email protected]> Co-authored-by: moul <[email protected]>
1 parent c16c172 commit fc0e5bc

File tree

41 files changed

+847
-452
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+847
-452
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ reporting/reporting
22
reporting/output
33
.idea
44
.DS_Store
5+
6+
./build

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
install:
2-
go install ./gnoblog-cli
2+
go install ./cmd/gnoblog-cli
33

44
test:
55
go test -v ./...

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The blog posts are written in markdown format, and include a frontmatter prefix
1515

1616
See [CONTRIBUTING.md](./CONTRIBUTING.md).
1717

18-
## CLI ([`./gnoblog-cli`](./gnoblog-cli))
18+
## CLI ([`./gnoblog-cli`](./cmd/gnoblog-cli))
1919

2020
_See [#1](https://github.com/gnolang/blog/issues/1)_
2121

cmd/gnoblog-cli/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# `gnoblog-cli`
2+
3+
`gnoblog-cli` is a configurable tool that allows for easy deployment of blog posts to the
4+
`r/gnoland/blog` realm.
5+
6+
All blog posts posted with `gnoblog-cli` need to be in the correct format:
7+
- Filename: `README.md`,
8+
- Frontmatter must be of a specific format,
9+
- There must be an H1 in below the frontmatter, which will be treated as the post title.
10+
11+
For reference, see existing posts, like [Peace!](../../posts/2022-05-02_peace)
12+
13+
`gnoblog-cli` utilizes keys from the local Gno keybase to deploy to
14+
the blog realm, whose path is configurable via the `--pkgpath` flag.
15+
16+
`gnoblog-cli` can has the ability to:
17+
- Post a single blog post `README.md` file to the chain, in which case
18+
it takes the direct path to the file as an argument,
19+
- Batch post multiple `README.md` files by recursive looking through directories,
20+
finding `README.md`s, and pack all of them to a single transaction to be
21+
broadcast to the chain.
22+
23+
24+
25+
26+
27+

cmd/gnoblog-cli/clients.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"github.com/gnolang/gno/gno.land/pkg/gnoclient"
5+
rpcclient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
6+
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
7+
)
8+
9+
func initSigner(cfg *cliCfg, password string) (gnoclient.SignerFromKeybase, error) {
10+
kb, err := keys.NewKeyBaseFromDir(cfg.GnoHome)
11+
if err != nil {
12+
return gnoclient.SignerFromKeybase{}, err
13+
}
14+
15+
return gnoclient.SignerFromKeybase{
16+
Keybase: kb,
17+
Account: cfg.KeyName,
18+
Password: password,
19+
ChainID: cfg.ChainId,
20+
}, nil
21+
}
22+
23+
func initRPCClient(cfg *cliCfg) rpcclient.Client {
24+
remote := cfg.Remote // todo: validate if chainID & remote match?
25+
return rpcclient.NewHTTP(remote, "/websocket")
26+
}

cmd/gnoblog-cli/errors.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import "errors"
4+
5+
var (
6+
ErrInvalidNumberOfArgs = errors.New("invalid number of args; please input exactly 1 argument")
7+
ErrEmptyKeyName = errors.New("key name cannot be empty")
8+
ErrNoNewOrChangedPosts = errors.New("no new posts have been added, nor have any existing posts been changed")
9+
)

cmd/gnoblog-cli/main.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"github.com/gnolang/gno/tm2/pkg/commands"
6+
"os"
7+
)
8+
9+
func main() {
10+
io := commands.NewDefaultIO()
11+
cmd := newRootCmd(io)
12+
13+
cmd.Execute(context.Background(), os.Args[1:])
14+
}
15+
16+
func newRootCmd(io commands.IO) *commands.Command {
17+
cmd := commands.NewCommand(
18+
commands.Metadata{
19+
ShortUsage: "<subcommand> [flags] [<arg>...]",
20+
LongHelp: "The CLI for easy use of the r/blog realm",
21+
},
22+
commands.NewEmptyConfig(),
23+
commands.HelpExec,
24+
)
25+
26+
cmd.AddSubCommands(
27+
newPostCommand(io),
28+
)
29+
30+
return cmd
31+
}

cmd/gnoblog-cli/mock_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
"io"
5+
)
6+
7+
type (
8+
mockGetPassword func() (string, error)
9+
)
10+
11+
type mockIO struct {
12+
getPassword mockGetPassword
13+
}
14+
15+
func (m mockIO) In() io.Reader {
16+
//TODO implement me
17+
panic("implement me")
18+
}
19+
20+
func (m mockIO) Out() io.WriteCloser {
21+
//TODO implement me
22+
panic("implement me")
23+
}
24+
25+
func (m mockIO) Err() io.WriteCloser {
26+
//TODO implement me
27+
panic("implement me")
28+
}
29+
30+
func (m mockIO) SetIn(in io.Reader) {
31+
//TODO implement me
32+
panic("implement me")
33+
}
34+
35+
func (m mockIO) SetOut(out io.WriteCloser) {
36+
//TODO implement me
37+
panic("implement me")
38+
}
39+
40+
func (m mockIO) SetErr(err io.WriteCloser) {
41+
//TODO implement me
42+
panic("implement me")
43+
}
44+
45+
func (m mockIO) Println(args ...interface{}) {
46+
//TODO implement me
47+
panic("implement me")
48+
}
49+
50+
func (m mockIO) Printf(format string, args ...interface{}) {
51+
//TODO implement me
52+
panic("implement me")
53+
}
54+
55+
func (m mockIO) Printfln(format string, args ...interface{}) {
56+
//TODO implement me
57+
panic("implement me")
58+
}
59+
60+
func (m mockIO) ErrPrintln(args ...interface{}) {
61+
//TODO implement me
62+
panic("implement me")
63+
}
64+
65+
func (m mockIO) ErrPrintfln(format string, args ...interface{}) {
66+
//TODO implement me
67+
panic("implement me")
68+
}
69+
70+
func (m mockIO) GetCheckPassword(prompts [2]string, insecure bool) (string, error) {
71+
//TODO implement me
72+
panic("implement me")
73+
}
74+
75+
func (m mockIO) GetConfirmation(prompt string) (bool, error) {
76+
//TODO implement me
77+
panic("implement me")
78+
}
79+
80+
func (m mockIO) GetPassword(prompt string, insecure bool) (string, error) {
81+
if m.getPassword != nil {
82+
return m.getPassword()
83+
}
84+
return "", nil
85+
}
86+
87+
func (m mockIO) GetString(prompt string) (string, error) {
88+
//TODO implement me
89+
panic("implement me")
90+
}

0 commit comments

Comments
 (0)