Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): exit with error code and use regen env prefix #285

Merged
merged 6 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ import (

const (
appName = "regen"

// EnvPrefix environment variable prefix used to map environment
// variables to command flags
EnvPrefix = "REGEN"
)

var _ simapp.App = &RegenApp{}
Expand All @@ -112,13 +116,11 @@ var (
// DefaultNodeHome default home directories for regen
DefaultNodeHome = os.ExpandEnv("$HOME/.regen")

// The ModuleBasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
// and genesis verification.
// ModuleBasics is in charge of setting up basic, non-dependant module
// elements, such as codec registration and genesis verification.
ModuleBasics = module.NewBasicManager(
append([]module.AppModuleBasic{
auth.AppModuleBasic{},
// genaccounts.AppModuleBasic{},
genutil.AppModuleBasic{},
bank.AppModuleBasic{},
capability.AppModuleBasic{},
Expand Down
12 changes: 9 additions & 3 deletions app/regen/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ package regen_test

import (
"fmt"
"io/ioutil"
"testing"

"github.com/cosmos/cosmos-sdk/client/flags"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/regen-network/regen-ledger/v4/app"
cmd "github.com/regen-network/regen-ledger/v4/app/regen/cmd"
)

func TestInitCmd(t *testing.T) {
nodeHome, err := ioutil.TempDir(t.TempDir(), ".regen")
require.NoError(t, err)

rootCmd, _ := cmd.NewRootCmd()
rootCmd.SetArgs([]string{
"init", // Test the init cmd
"regenapp-test", // Moniker
fmt.Sprintf("--%s=%s", cli.FlagOverwrite, "true"), // Overwrite genesis.json, in case it already exists
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped this as it seemed unnecessary. If folks think there is a good reason to keep this flag, I can add it back in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I don't think it hurts. There could be edge cases where is needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What edge cases are you thinking of? Ideally this should be run as a self contained test with full teardown of any artifacts right? The home directory is created from scratch on each test run, so I don't see how we could expect there to already be a genesis.json here.

Previously this line was there because it always looked at your default home directory for running (or created one if it didnt exist). This PR changes that so it creates a temp home dir, which should render the --overwrite=true flag unnecessary. Lmk if i'm missing something

fmt.Sprintf("--%s=%s", flags.FlagHome, nodeHome), // Set home flag
})

err := cmd.Execute(rootCmd)
err = svrcmd.Execute(rootCmd, app.EnvPrefix, app.DefaultNodeHome)
require.NoError(t, err)
}
3 changes: 2 additions & 1 deletion app/regen/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"

tmcfg "github.com/tendermint/tendermint/config"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -148,7 +149,7 @@ func Execute(rootCmd *cobra.Command) error {
rootCmd.PersistentFlags().String(flags.FlagLogLevel, zerolog.InfoLevel.String(), "The logging level (trace|debug|info|warn|error|fatal|panic)")
rootCmd.PersistentFlags().String(flags.FlagLogFormat, tmcfg.LogFormatPlain, "The logging format (json|plain)")

executor := tmcli.PrepareBaseCmd(rootCmd, "REGEN", app.DefaultNodeHome)
executor := tmcli.PrepareBaseCmd(rootCmd, app.EnvPrefix, app.DefaultNodeHome)
return executor.ExecuteContext(ctx)
}

Expand Down
13 changes: 11 additions & 2 deletions app/regen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ package main
import (
"os"

"github.com/cosmos/cosmos-sdk/server"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"

"github.com/regen-network/regen-ledger/v4/app"
cmd "github.com/regen-network/regen-ledger/v4/app/regen/cmd"
)

// In main we call the rootCmd
func main() {
rootCmd, _ := cmd.NewRootCmd()
if err := cmd.Execute(rootCmd); err != nil {
os.Exit(1)
if err := svrcmd.Execute(rootCmd, app.EnvPrefix, app.DefaultNodeHome); err != nil {
switch e := err.(type) {
case server.ErrorCode:
os.Exit(e.Code)
default:
os.Exit(1)
}
}
}