diff --git a/commands/server/init.go b/commands/server/init.go index 8d8087af..2b397a59 100644 --- a/commands/server/init.go +++ b/commands/server/init.go @@ -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" ) @@ -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, @@ -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 } @@ -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.") } @@ -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 @@ -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() @@ -133,7 +139,7 @@ var ( // indexer = "kv" // index_all_tags = // index_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) diff --git a/examples/mycoind/commands/init_test.go b/examples/mycoind/commands/init_test.go index 3b8c7692..1329beb5 100644 --- a/examples/mycoind/commands/init_test.go +++ b/examples/mycoind/commands/init_test.go @@ -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,