OSDN Git Service

Hulk did something
[bytom/vapor.git] / cmd / bytomd / commands / init.go
diff --git a/cmd/bytomd/commands/init.go b/cmd/bytomd/commands/init.go
new file mode 100644 (file)
index 0000000..33e6c00
--- /dev/null
@@ -0,0 +1,40 @@
+package commands
+
+import (
+       "os"
+       "path"
+
+       log "github.com/sirupsen/logrus"
+       "github.com/spf13/cobra"
+
+       cfg "github.com/vapor/config"
+)
+
+var initFilesCmd = &cobra.Command{
+       Use:   "init",
+       Short: "Initialize blockchain",
+       Run:   initFiles,
+}
+
+func init() {
+       initFilesCmd.Flags().String("chain_id", config.ChainID, "Select [mainnet] or [testnet] or [solonet]")
+
+       RootCmd.AddCommand(initFilesCmd)
+}
+
+func initFiles(cmd *cobra.Command, args []string) {
+       configFilePath := path.Join(config.RootDir, "config.toml")
+       if _, err := os.Stat(configFilePath); !os.IsNotExist(err) {
+               log.WithFields(log.Fields{"module": logModule, "config": configFilePath}).Info("Already exists config file.")
+               return
+       }
+
+       switch config.ChainID {
+       case "mainnet", "testnet":
+               cfg.EnsureRoot(config.RootDir, config.ChainID)
+       default:
+               cfg.EnsureRoot(config.RootDir, "solonet")
+       }
+
+       log.WithFields(log.Fields{"module": logModule, "config": configFilePath}).Info("Initialized bytom")
+}