Skip to content

Commit

Permalink
refactor(gnoland): add subcommands support (#937)
Browse files Browse the repository at this point in the history
Co-authored-by: Hariom Verma <[email protected]>
  • Loading branch information
moul and harry-hov authored Jul 10, 2023
1 parent f50f33d commit 8c8923e
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/gnoland.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ jobs:
# TODO: setup docker caching
- run: make test.docker
- run: docker logs int_gnoland || true

# TODO: docker-less integration test?
2 changes: 1 addition & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tasks:

- name: Gnoland Node
init: go install ./gno.land/cmd/gnoland
command: gnoland
command: gnoland start

ports:
- port: 8888
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/r/demo/boards/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Use this mneonic:
### Start `gnoland` node.

```bash
./build/gnoland
./build/gnoland start
```

NOTE: This can be reset with `make reset`
Expand Down
2 changes: 1 addition & 1 deletion gno.land/cmd/gnofaucet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Copy and paste the following mnemonic.

Make sure you have started gnoland

./build/gnoland
./build/gnoland start

## Step3:

Expand Down
4 changes: 4 additions & 0 deletions gno.land/cmd/gnoland/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
$> cd ./gno/gno.land
$> make install.gnoland

## Run `gnoland` full node

$> gnoland start

Afterward, you can interact with [`gnokey`](../gnokey) or launch a [`gnoweb`](../gnoweb) interface.
42 changes: 42 additions & 0 deletions gno.land/cmd/gnoland/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/fftoml"
)

func main() {
io := commands.NewDefaultIO()
cmd := newRootCmd(io)

if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}

func newRootCmd(io *commands.IO) *commands.Command {
cmd := commands.NewCommand(
commands.Metadata{
ShortUsage: "<subcommand> [flags] [<arg>...]",
ShortHelp: "Starts the gnoland blockchain node",
Options: []ff.Option{
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(fftoml.Parser),
},
},
commands.NewEmptyConfig(),
commands.HelpExec,
)

cmd.AddSubCommands(
newStartCmd(io),
)

return cmd
}
41 changes: 14 additions & 27 deletions gno.land/cmd/gnoland/main.go → gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/fftoml"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
Expand All @@ -29,7 +25,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/std"
)

type gnolandCfg struct {
type startCfg struct {
skipFailingGenesisTxs bool
skipStart bool
genesisBalancesFile string
Expand All @@ -41,32 +37,23 @@ type gnolandCfg struct {
config string
}

func main() {
cfg := &gnolandCfg{}
func newStartCmd(io *commands.IO) *commands.Command {
cfg := &startCfg{}

cmd := commands.NewCommand(
return commands.NewCommand(
commands.Metadata{
ShortUsage: "[flags] [<arg>...]",
LongHelp: "Starts the gnoland blockchain node",
Options: []ff.Option{
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(fftoml.Parser),
},
Name: "start",
ShortUsage: "start [flags]",
ShortHelp: "Run the full node",
},
cfg,
func(_ context.Context, _ []string) error {
return exec(cfg)
func(_ context.Context, args []string) error {
return execStart(cfg, args, io)
},
)

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

os.Exit(1)
}
}

func (c *gnolandCfg) RegisterFlags(fs *flag.FlagSet) {
func (c *startCfg) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(
&c.skipFailingGenesisTxs,
"skip-failing-genesis-txs",
Expand Down Expand Up @@ -131,8 +118,8 @@ func (c *gnolandCfg) RegisterFlags(fs *flag.FlagSet) {
)
}

func exec(c *gnolandCfg) error {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
func execStart(c *startCfg, args []string, io *commands.IO) error {
logger := log.NewTMLogger(log.NewSyncWriter(io.Out))
rootDir := c.rootDir

cfg := config.LoadOrMakeConfigWithOptions(rootDir, func(cfg *config.Config) {
Expand Down Expand Up @@ -171,10 +158,10 @@ func exec(c *gnolandCfg) error {
return fmt.Errorf("error in creating node: %w", err)
}

fmt.Fprintln(os.Stderr, "Node created.")
fmt.Fprintln(io.Err, "Node created.")

if c.skipStart {
fmt.Fprintln(os.Stderr, "'--skip-start' is set. Exiting.")
fmt.Fprintln(io.Err, "'--skip-start' is set. Exiting.")

return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package main

import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"testing"

"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/testutils"
"github.com/stretchr/testify/require"
)

func TestInitialize(t *testing.T) {
func TestStartInitialize(t *testing.T) {
cases := []struct {
args []string
}{
{[]string{"--skip-start", "--skip-failing-genesis-txs"}},
{[]string{"start", "--skip-start", "--skip-failing-genesis-txs"}},
// {[]string{"--skip-start"}},
// FIXME: test seems flappy as soon as we have multiple cases.
}
Expand All @@ -25,28 +25,23 @@ func TestInitialize(t *testing.T) {
for _, tc := range cases {
name := strings.Join(tc.args, " ")
t.Run(name, func(t *testing.T) {
closer := testutils.CaptureStdoutAndStderr()

cfg := &gnolandCfg{}
cmd := commands.NewCommand(
commands.Metadata{},
cfg,
func(_ context.Context, _ []string) error {
return exec(cfg)
},
)
mockOut := bytes.NewBufferString("")
mockErr := bytes.NewBufferString("")
io := commands.NewTestIO()
io.SetOut(commands.WriteNopCloser(mockOut))
io.SetErr(commands.WriteNopCloser(mockErr))
cmd := newRootCmd(io)

t.Logf(`Running "gnoland %s"`, strings.Join(tc.args, " "))
err := cmd.ParseAndRun(context.Background(), tc.args)
require.NoError(t, err)

stdouterr, bufErr := closer()
require.NoError(t, bufErr)
require.NoError(t, err)
stdout := mockOut.String()
stderr := mockErr.String()

require.Contains(t, stdouterr, "Node created.", "failed to create node")
require.Contains(t, stdouterr, "'--skip-start' is set. Exiting.", "not exited with skip-start")
require.NotContains(t, stdouterr, "panic:")
require.Contains(t, stderr, "Node created.", "failed to create node")
require.Contains(t, stderr, "'--skip-start' is set. Exiting.", "not exited with skip-start")
require.NotContains(t, stdout, "panic:")
})
}
}
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ require (
)

require (
github.com/awalterschulze/gographviz v2.0.3+incompatible // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
Expand All @@ -56,12 +58,16 @@ require (
github.com/google/flatbuffers v1.12.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/uber/go-torch v0.0.0-20181107071353-86f327cc820e // indirect
go.opencensus.io v0.22.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/sys v0.9.0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions misc/deployments/staging.gno.land/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
working_dir: /opt/gno/src/gno.land
command:
- gnoland
- start
- --skip-failing-genesis-txs
- --chainid=staging
- --genesis-remote=staging.gno.land:36657
Expand Down
1 change: 1 addition & 0 deletions misc/deployments/test2.gno.land/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
- LETSENCRYPT_HOST=rpc.test2.gno.land
command:
- gnoland
- start
- --skip-failing-genesis-txs
- --chainid
- test2
Expand Down
1 change: 1 addition & 0 deletions misc/deployments/test3.gno.land/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
- LETSENCRYPT_HOST=rpc.test3.gno.land
command:
- gnoland
- start
- --skip-failing-genesis-txs
- --chainid
- test3
Expand Down
2 changes: 1 addition & 1 deletion misc/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
dockerfile: ../..Dockerfile
environment:
- LOG_LEVEL=4
command: [ "gnoland" ]
command: [ "gnoland", "start" ]
volumes:
- "gnonode:/opt/gno/src/testdir"
networks:
Expand Down
1 change: 1 addition & 0 deletions misc/docker-integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func startGnoland(t *testing.T) {
"-w", "/opt/gno/src/gno.land",
"gno:integration",
"gnoland",
"start",
})
output, err := cmd.CombinedOutput()
require.NoError(t, err)
Expand Down

0 comments on commit 8c8923e

Please sign in to comment.