Skip to content

Commit

Permalink
wip save, move things around
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Jan 20, 2024
1 parent 0e46514 commit 6b9f1a2
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 89 deletions.
Binary file modified cmd
Binary file not shown.
12 changes: 6 additions & 6 deletions gnoblog-cli/cmd/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
)

func initSigner(cfg *postCfg, password string) gnoclient.SignerFromKeybase {
kb, _ := keys.NewKeyBaseFromDir(cfg.gnoHome)
func initSigner(cfg *cliCfg, password string) gnoclient.SignerFromKeybase {
kb, _ := keys.NewKeyBaseFromDir(cfg.GnoHome)
return gnoclient.SignerFromKeybase{
Keybase: kb,
Account: cfg.keyName,
Account: cfg.KeyName,
Password: password,
ChainID: cfg.chainId,
ChainID: cfg.ChainId,
}
}

func initRPCClient(cfg *postCfg) rpcclient.Client {
remote := cfg.remote // todo: validate if chainID & remote match?
func initRPCClient(cfg *cliCfg) rpcclient.Client {
remote := cfg.Remote // todo: validate if chainID & remote match?
return rpcclient.NewHTTP(remote, "/websocket")
}
44 changes: 3 additions & 41 deletions gnoblog-cli/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,30 @@ import (
"context"
"flag"
"fmt"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/peterbourgon/ff/v3/ffcli"
"os"
)

type baseCliCfg struct {
debug bool
publish bool
edit bool
gasWanted int64
gasFee string
chainId string
blogPath string

keyName string
gnoHome string
remote string
quiet bool
insecurePasswordStdIn bool
}

func main() {
var (
baseCfg = &baseCliCfg{}
fs = flag.NewFlagSet("root", flag.ExitOnError)
)

// Register the flags
baseCfg.registerFlags(fs)

// Create the root command
cmd := &ffcli.Command{
ShortUsage: "<subcommand> [flags] [<arg>...]",
LongHelp: "The CLI for easy use of the r/blog realm",
FlagSet: fs,
FlagSet: nil,
Exec: func(_ context.Context, _ []string) error {
return flag.ErrHelp
},
}

// Add the subcommands
cmd.Subcommands = []*ffcli.Command{
newPostCommand(baseCfg),
newPostCommand(),
}

// Run root command
if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v", err)

os.Exit(1)
}
}

func (cfg *baseCliCfg) 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", 2000000, "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.blogPath, "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")
}
162 changes: 120 additions & 42 deletions gnoblog-cli/cmd/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,144 @@ import (
"flag"
"fmt"
"github.com/gnolang/gno/gno.land/pkg/gnoclient"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/peterbourgon/ff/v3/ffcli"
"os"
"strings"
)

type postCfg struct {
*baseCliCfg
path string
type cliCfg struct {
Publish bool
Edit bool
GasWanted int64
GasFee string
ChainId string
BlogRealmPath string
Multiple string

KeyName string
GnoHome string
Remote string
Quiet bool
InsecurePasswordStdIn bool
}

func newPostCommand(base *baseCliCfg) *ffcli.Command {
cfg := &postCfg{
baseCliCfg: base,
}
fs := flag.NewFlagSet("post", flag.ExitOnError)
func newPostCommand() *ffcli.Command {
var (
fs = flag.NewFlagSet("post", flag.ExitOnError)
cfg = &cliCfg{}
)

// Register flagset
cfg.registerFlags(fs)

// Make Post command
return &ffcli.Command{
Name: "post",
ShortUsage: "post [flags]",
LongHelp: "post a single post",
LongHelp: "Post one or more files. To post one file, simply pass it as an argument to the command. To post a batch of files, use --multiple.",
FlagSet: fs,
Exec: cfg.execPost,
}
}

func (cfg *postCfg) registerFlags(fs *flag.FlagSet) {
fs.StringVar(&cfg.path,
"path",
// Register flags
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",
2000000,
"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.StringVar(&cfg.Multiple,
"multiple",
"",
"path to post README. if a dir is passed in, a batch post happens",
"walk root dir & batch post found .md files",
)
}

func (cfg *postCfg) execPost(_ context.Context, _ []string) error {
if cfg.path == "" {
return errors.New("post path cannot be empty")
func (cfg *cliCfg) execPost(_ context.Context, args []string) error {
// Check for --root-dir flag with arguments
if cfg.Multiple != "" && len(args) > 0 {
return fmt.Errorf("no arguments expected when using --multiple")
}

// Check for no files provided
if len(args) == 0 {
return fmt.Errorf("no readme files provided")
}

fmt.Println(cfg.KeyName)

// Check for multiple files without --multiple flag
//if len(args) > 1 {
// return fmt.Errorf("use the --multiple flag to post more than 1 file")
//}

// Init IO for password input
io := commands.NewDefaultIO()

var pass string
var err error
if cfg.quiet {
pass, err = io.GetPassword("", cfg.insecurePasswordStdIn)
if cfg.Quiet {
pass, err = io.GetPassword("", cfg.InsecurePasswordStdIn)
} else {
pass, err = io.GetPassword("Enter password:", cfg.insecurePasswordStdIn)
pass, err = io.GetPassword("Enter password:", cfg.InsecurePasswordStdIn)
}

if err != nil {
return err
}

// Validate chainID that was passed in
if err := validateChainID(cfg.chainId); err != nil {
if err = validateChainID(cfg.ChainId); err != nil {
return err
}

Expand All @@ -71,64 +151,62 @@ func (cfg *postCfg) execPost(_ context.Context, _ []string) error {
RPCClient: initRPCClient(cfg),
}

//// Batch post request passed in
//if cfg.batchRoot != "" {
// return cfg.batchPost()
//}
// Batch post request passed in
if cfg.Multiple != "" {
return cfg.batchPost()
}

// Single post request passed in
return cfg.singlePost(client)
// Single post request passed in an argument
return cfg.singlePost(client, args[0])
}

func (cfg *postCfg) singlePost(c gnoclient.Client) error {
postFile, err := os.Open(cfg.path)
func (cfg *cliCfg) singlePost(c gnoclient.Client, postPath string) error {
postFile, err := os.Open(postPath)
if err != nil {
return fmt.Errorf("cannot open file %q: %w", cfg.path, err)
return fmt.Errorf("cannot open file %q: %w", postPath, err)
}

post, err := parsePost(postFile)
if err != nil {
return fmt.Errorf("cannot parse post %q: %w", cfg.path, err)
return fmt.Errorf("cannot parse post %q: %w", postPath, err)
}

signingAcc, _, err := c.QueryAccount(c.Signer.Info().GetAddress())

if err != nil {
return err
}

nonce := signingAcc.GetSequence()
accNumber := signingAcc.GetAccountNumber()

// add check if post exists to call edit

// todo add check if post exists to call edit
callCfg := gnoclient.CallCfg{
PkgPath: cfg.blogPath,
PkgPath: cfg.BlogRealmPath,
FuncName: "ModAddPost",
Args: []string{
post.Slug,
post.Title,
post.Body,
"tags",
strings.Join(post.Tags, ","),
},
GasFee: cfg.gasFee,
GasWanted: cfg.gasWanted,
GasFee: cfg.GasFee,
GasWanted: cfg.GasWanted,
Send: "",
AccountNumber: accNumber,
SequenceNumber: nonce,
Memo: "Posted from gnoblog-cli",
}

call, err := c.Call(callCfg)
_, err = c.Call(callCfg)
if err != nil {
return err
}

fmt.Printf("Success! %s", call.DeliverTx.Info)
fmt.Printf("Success!")

return nil
}

func (cfg *postCfg) batchPost() error {
return nil
func (cfg *cliCfg) batchPost() error {
return errors.New("not implemented")
}

0 comments on commit 6b9f1a2

Please sign in to comment.