Skip to content

Commit

Permalink
Merge pull request #80 from confio/feature/ignore-reinit
Browse files Browse the repository at this point in the history
add ignore flag
  • Loading branch information
ethanfrey authored Jun 27, 2018
2 parents b0d1bc2 + 4e1c1a6 commit af40199
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 15 additions & 9 deletions commands/server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ const (
AppStateKey = "app_state"
DirConfig = "config"
GenesisTimeKey = "genesis_time"
ErrorAlreadyInitialised = "the application has already been initialised, use %s flag to override"
ErrorAlreadyInitialised = "the application has already been initialised, use %s flag to override or %s to ignore"
FlagForce = "f"
FlagIgnore = "i"
flagIndexAll = "all"
flagIndexTags = "tags"
)
Expand All @@ -33,14 +34,16 @@ Usage:
xxx init -all=f // no index
xxx init -tags=foo,bar // index only foo and bar
*/
func parseIndex(args []string) (bool, bool, string, []string, error) {
func parseIndex(args []string) (bool, bool, string, bool, []string, error) {
// parse flagIndexAll, flagIndexTags and return the result
indexFlags := flag.NewFlagSet("init", flag.ExitOnError)
tags := indexFlags.String(flagIndexTags, "", "comma-separated list of tags to index")
all := indexFlags.Bool(flagIndexAll, true, "")
force := indexFlags.Bool(FlagForce, false, "")
ignore := indexFlags.Bool(FlagIgnore, false, "")

err := indexFlags.Parse(args)
return *all, *force, *tags, indexFlags.Args(), err
return *all, *force, *tags, *ignore, indexFlags.Args(), err
}

// InitCmd will initialize all files for tendermint,
Expand All @@ -52,11 +55,11 @@ func InitCmd(gen GenOptions, logger log.Logger, home string, args []string) erro
genFile := filepath.Join(home, DirConfig, "genesis.json")
confFile := filepath.Join(home, DirConfig, "config.toml")

all, force, tags, args, err := parseIndex(args)
all, force, tags, ignore, args, err := parseIndex(args)
if err != nil {
return err
}
err = setTxIndex(confFile, all, tags, force)
err = setTxIndex(confFile, all, tags, force, ignore)
if err != nil {
return err
}
Expand All @@ -73,7 +76,7 @@ func InitCmd(gen GenOptions, logger log.Logger, home string, args []string) erro
}

// And add them to the genesis file
err = addGenesisOptions(genFile, options, force)
err = addGenesisOptions(genFile, options, force, ignore)
if err == nil {
fmt.Println("The application has been succesfully initialised.")
}
Expand All @@ -91,7 +94,7 @@ type GenOptions func(args []string) (json.RawMessage, error)
// so we can add one line.
type GenesisDoc map[string]json.RawMessage

func addGenesisOptions(filename string, options json.RawMessage, force bool) error {
func addGenesisOptions(filename string, options json.RawMessage, force, ignore bool) error {
bz, err := ioutil.ReadFile(filename)
if err != nil {
return err
Expand All @@ -105,7 +108,10 @@ func addGenesisOptions(filename string, options json.RawMessage, force bool) err

v, ok := doc[AppStateKey]
if !force && ok && len(v) > 0 {
return fmt.Errorf(ErrorAlreadyInitialised, FlagForce)
if ignore {
return nil
}
return fmt.Errorf(ErrorAlreadyInitialised, FlagForce, FlagIgnore)
}

timeJson, _ := time.Now().MarshalJSON()
Expand Down Expand Up @@ -133,7 +139,7 @@ var (
// indexer = "kv"
// index_all_tags = <all>
// index_tags = <tags>
func setTxIndex(config string, all bool, tags string, force bool) error {
func setTxIndex(config string, all bool, tags string, force, ignore bool) error {
f, err := os.Open(config)
if err != nil {
return errors.WithStack(err)
Expand Down
2 changes: 1 addition & 1 deletion examples/mycoind/commands/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestInit(t *testing.T) {
assert.Contains(t, string(doc[server.AppStateKey]), `"ticker": "ETH"`)

err = server.InitCmd(app.GenInitOptions, logger, home, args)
assert.EqualValues(t, fmt.Errorf(server.ErrorAlreadyInitialised, server.FlagForce), err)
assert.EqualValues(t, fmt.Errorf(server.ErrorAlreadyInitialised, server.FlagForce, server.FlagIgnore), err)
}

// setupConfig creates a homedir to run inside,
Expand Down

0 comments on commit af40199

Please sign in to comment.