Skip to content

Commit

Permalink
version: Remove Viper (#6628)
Browse files Browse the repository at this point in the history
* version: remove viper

* Remove home flag gets

* fix: tests
  • Loading branch information
alexanderbez authored Jul 7, 2020
1 parent 57ffe14 commit bc261d9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 56 deletions.
2 changes: 1 addition & 1 deletion server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func AddCommands(rootCmd *cobra.Command, appCreator AppCreator, appExport AppExp
tendermintCmd,
ExportCmd(appExport),
flags.LineBreak,
version.Cmd,
version.NewVersionCommand(),
)
}

Expand Down
4 changes: 1 addition & 3 deletions simapp/simd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -47,8 +46,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config

homeDir, _ := cmd.Flags().GetString(cli.HomeFlag)
config.SetRoot(homeDir)
config.SetRoot(clientCtx.HomeDir)

keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
clientHome, _ := cmd.Flags().GetString(flagClientHome)
Expand Down
71 changes: 36 additions & 35 deletions version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,50 @@ package version

import (
"encoding/json"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"

"github.com/tendermint/tendermint/libs/cli"
yaml "gopkg.in/yaml.v2"
)

const flagLong = "long"

func init() {
Cmd.Flags().Bool(flagLong, false, "Print long version information")
}

// Cmd prints out the application's version information passed via build flags.
var Cmd = &cobra.Command{
Use: "version",
Short: "Print the app version",
RunE: runVersionCmd,
}

func runVersionCmd(cmd *cobra.Command, args []string) error {
verInfo := NewInfo()

if !viper.GetBool(flagLong) {
cmd.Println(verInfo.Version)
return nil
}

var bz []byte
var err error

switch viper.GetString(cli.OutputFlag) {
case "json":
bz, err = json.Marshal(verInfo)
default:
bz, err = yaml.Marshal(&verInfo)
func NewVersionCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the application binary version information",
RunE: func(cmd *cobra.Command, _ []string) error {
verInfo := NewInfo()

if long, _ := cmd.Flags().GetBool(flagLong); !long {
cmd.Println(verInfo.Version)
return nil
}

var bz []byte
var err error

output, _ := cmd.Flags().GetString(cli.OutputFlag)
switch strings.ToLower(output) {
case "json":
bz, err = json.Marshal(verInfo)

default:
bz, err = yaml.Marshal(&verInfo)
}

if err != nil {
return err
}

cmd.Println(string(bz))
return nil
},
}

if err != nil {
return err
}
cmd.Flags().Bool(flagLong, false, "Print long version information")
cmd.Flags().StringP(cli.OutputFlag, "o", "text", "Output format (text|json)")

cmd.Println(string(bz))
return nil
return cmd
}
23 changes: 14 additions & 9 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"runtime"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/cli"
Expand Down Expand Up @@ -40,20 +39,26 @@ go version go1.14 linux/amd64`
}

func Test_runVersionCmd(t *testing.T) {
require.NotNil(t, Cmd)
_, mockOut, _ := tests.ApplyMockIO(Cmd)
cmd := NewVersionCommand()
_, mockOut, _ := tests.ApplyMockIO(cmd)

viper.Set(cli.OutputFlag, "")
viper.Set(flagLong, false)
require.NoError(t, runVersionCmd(Cmd, nil))
cmd.SetArgs([]string{
fmt.Sprintf("--%s=''", cli.OutputFlag),
fmt.Sprintf("--%s=false", flagLong),
})

require.NoError(t, cmd.Execute())
assert.Equal(t, "\n", mockOut.String())
mockOut.Reset()

viper.Set(cli.OutputFlag, "json")
viper.Set(flagLong, true)
cmd.SetArgs([]string{
fmt.Sprintf("--%s=json", cli.OutputFlag),
fmt.Sprintf("--%s=true", flagLong),
})

info := NewInfo()
stringInfo, err := json.Marshal(info)
require.NoError(t, err)
require.NoError(t, runVersionCmd(Cmd, nil))
require.NoError(t, cmd.Execute())
assert.Equal(t, string(stringInfo)+"\n", mockOut.String())
}
3 changes: 1 addition & 2 deletions x/genutil/client/cli/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator, defaultNodeH
clientCtx := client.GetClientContextFromCmd(cmd)
cdc := clientCtx.JSONMarshaler

home, _ := cmd.Flags().GetString(cli.HomeFlag)
config.SetRoot(home)
config.SetRoot(clientCtx.HomeDir)

nodeID, valPubKey, err := genutil.InitializeNodeValidatorFiles(config)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions x/genutil/client/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config

home, _ := cmd.Flags().GetString(cli.HomeFlag)
config.SetRoot(home)
config.SetRoot(clientCtx.HomeDir)

chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
if chainID == "" {
Expand Down
8 changes: 4 additions & 4 deletions x/genutil/client/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestInitCmd(t *testing.T) {
require.NoError(t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithJSONMarshaler(makeCodec())
clientCtx := client.Context{}.WithJSONMarshaler(makeCodec()).WithHomeDir(home)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestEmptyState(t *testing.T) {
require.NoError(t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithJSONMarshaler(makeCodec())
clientCtx := client.Context{}.WithJSONMarshaler(makeCodec()).WithHomeDir(home)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
Expand Down Expand Up @@ -128,14 +128,14 @@ func TestStartStandAlone(t *testing.T) {
require.NoError(t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithJSONMarshaler(makeCodec())
clientCtx := client.Context{}.WithJSONMarshaler(makeCodec()).WithHomeDir(home)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)

cmd := InitCmd(testMbm, home)
cmd.SetArgs([]string{"appnode-test", fmt.Sprintf("--%s=%s", cli.HomeFlag, home)})
cmd.SetArgs([]string{"appnode-test"})

require.NoError(t, cmd.ExecuteContext(ctx))

Expand Down

0 comments on commit bc261d9

Please sign in to comment.