added init option flag and tests

This commit is contained in:
Rigel Rozanski 2017-08-30 19:25:35 -04:00 committed by rigelrozanski
parent 080d9a1e31
commit bfd4ce96bb
2 changed files with 47 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -25,10 +26,12 @@ var InitCmd = &cobra.Command{
//nolint - flags //nolint - flags
var ( var (
FlagChainID = "chain-id" //TODO group with other flags or remove? is this already a flag here? FlagChainID = "chain-id" //TODO group with other flags or remove? is this already a flag here?
FlagOption = "option"
) )
func init() { func init() {
InitCmd.Flags().String(FlagChainID, "test_chain_id", "Chain ID") InitCmd.Flags().String(FlagChainID, "test_chain_id", "Chain ID")
InitCmd.Flags().StringSlice(FlagOption, []string{}, "Genesis option in the format <app>/<option>/<value>")
} }
// returns 1 iff it set a file, otherwise 0 (so we can add them) // returns 1 iff it set a file, otherwise 0 (so we can add them)
@ -64,7 +67,24 @@ func initCmd(cmd *cobra.Command, args []string) error {
return errors.New("Address must be 20-bytes in hex") return errors.New("Address must be 20-bytes in hex")
} }
genesis := GetGenesisJSON(viper.GetString(FlagChainID), userAddr) var options []string
var optionsStr string
sep := ",\n "
optionsRaw := viper.GetStringSlice(FlagOption)
if len(optionsRaw) > 0 {
optionsStr = sep
for i := 0; i < len(optionsRaw); i++ {
s := strings.SplitN(optionsRaw[i], "/", 3)
if len(s) != 3 {
return errors.New("Genesis option must be in the format <app>/<option>/<value>")
}
option := `"` + s[0] + `/` + s[1] + `", "` + s[2] + `"`
options = append(options, option)
}
}
optionsStr += strings.Join(options[:], ",\n ")
genesis := GetGenesisJSON(viper.GetString(FlagChainID), userAddr, optionsStr)
return CreateGenesisValidatorFiles(cfg, genesis, cmd.Root().Name()) return CreateGenesisValidatorFiles(cfg, genesis, cmd.Root().Name())
} }
@ -114,7 +134,7 @@ var PrivValJSON = `{
// GetGenesisJSON returns a new tendermint genesis with Basecoin app_options // GetGenesisJSON returns a new tendermint genesis with Basecoin app_options
// that grant a large amount of "mycoin" to a single address // that grant a large amount of "mycoin" to a single address
// TODO: A better UX for generating genesis files // TODO: A better UX for generating genesis files
func GetGenesisJSON(chainID, addr string) string { func GetGenesisJSON(chainID, addr string, options string) string {
return fmt.Sprintf(`{ return fmt.Sprintf(`{
"app_hash": "", "app_hash": "",
"chain_id": "%s", "chain_id": "%s",
@ -140,8 +160,8 @@ func GetGenesisJSON(chainID, addr string) string {
] ]
}], }],
"plugin_options": [ "plugin_options": [
"coin/issuer", {"app": "sigs", "addr": "%s"} "coin/issuer", {"app": "sigs", "addr": "%s"}%s
] ]
} }
}`, chainID, addr, addr) }`, chainID, addr, addr, options)
} }

23
tests/cli/init-server.sh Normal file
View File

@ -0,0 +1,23 @@
" Press ? for help
.. (up a dir)
/
▸ Applications/
▸ bin/
▸ cores/
▸ dev/
▸ etc/ -> /private/etc/
▸ home/
▸ Library/
▸ net/
▸ Network/
▸ opt/
▸ private/
▸ sbin/
▸ System/
▸ tmp/ -> /private/tmp/
▸ Users/
▸ usr/
▸ var/ -> /private/var/
▸ Volumes/
installer.failurerequests [RO]