rootcmd.go 2.95 KB
Newer Older
duanjinfei's avatar
duanjinfei committed
1 2 3
package main

import (
duanjinfei's avatar
duanjinfei committed
4 5
	"encoding/json"
	"example.com/m/conf"
duanjinfei's avatar
duanjinfei committed
6 7
	"example.com/m/log"
	"example.com/m/nm"
duanjinfei's avatar
duanjinfei committed
8 9
	"example.com/m/utils"
	"fmt"
duanjinfei's avatar
duanjinfei committed
10
	"github.com/astaxie/beego"
duanjinfei's avatar
duanjinfei committed
11 12
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto"
duanjinfei's avatar
duanjinfei committed
13
	"github.com/spf13/cobra"
duanjinfei's avatar
duanjinfei committed
14 15
	"github.com/spf13/viper"
	"io/ioutil"
duanjinfei's avatar
duanjinfei committed
16 17 18 19
	"os"
)

var (
duanjinfei's avatar
duanjinfei committed
20
	rewardAddr, dockerServer string
duanjinfei's avatar
duanjinfei committed
21 22
)

duanjinfei's avatar
duanjinfei committed
23
func init() {
24
	RootCmd.PersistentFlags().StringVarP(&rewardAddr, "reward", "r", "", "please enter a reward address")
duanjinfei's avatar
duanjinfei committed
25
	RootCmd.PersistentFlags().StringVarP(&dockerServer, "docker_server", "d", "", "please enter docker server address")
duanjinfei's avatar
duanjinfei committed
26 27 28
	cobra.OnInitialize(initConfig)
}

duanjinfei's avatar
duanjinfei committed
29 30
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
duanjinfei's avatar
duanjinfei committed
31
	Use:   "miner",
duanjinfei's avatar
duanjinfei committed
32 33 34
	Short: "The miner command-line interface",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
duanjinfei's avatar
duanjinfei committed
35 36 37
		if rewardAddr == "" {
			log.Error("Enter reward address is not nil")
			return
duanjinfei's avatar
duanjinfei committed
38
		}
duanjinfei's avatar
duanjinfei committed
39 40 41 42
		isSetDockerServer := conf.GetConfig().SetDockerServerUrl(dockerServer)
		if !isSetDockerServer {
			log.Error("Enter right docker server address:", dockerServer)
			return
duanjinfei's avatar
duanjinfei committed
43
		}
duanjinfei's avatar
duanjinfei committed
44 45 46 47 48 49 50 51 52 53
		log.Info("Enter docker server url:", dockerServer)
		isSetReward := conf.GetConfig().SetRewardAddress(rewardAddr)
		if !isSetReward {
			log.Error("Please set right reward address")
			return
		}
		log.Info("Enter reward address:", rewardAddr)
		log.InitLog(log.LogConfig{Path: "logs", Level: "debug", Save: 3})
		go nm.StartMonitor()
		beego.Run()
duanjinfei's avatar
duanjinfei committed
54 55 56
	},
}

duanjinfei's avatar
duanjinfei committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
func initConfig() {
	// 设置配置文件的名称(不包含扩展名)
	viper.SetConfigName("config")

	// 设置配置文件的类型
	viper.SetConfigType("json")

	// 设置配置文件所在的目录
	viper.AddConfigPath(".")

	viper.AutomaticEnv()

	// 读取配置文件
	if err := viper.ReadInConfig(); err != nil {
		fmt.Println("Error reading config file:", err)
		return
	}

	configFilePath := viper.ConfigFileUsed()
	if configFilePath == "" {
		// handle error
		log.Error("config file path is empty")
		panic("config file path is empty")
	}

	data, err := ioutil.ReadFile(configFilePath)
	if err != nil {
		// handle error
		log.Error("Read cfg file error:", err)
		panic("Read cfg file error")
	}
	err = json.Unmarshal(data, conf.GetConfig())
	if err != nil {
		// handle error
		log.Error("Json unmarshal cfg error:", err)
		panic("Json unmarshal cfg error")
	}
	conf.GetConfig().HeartRespTimeMillis = conf.GetConfig().HeartRespTimeSecond * 60 * 60 * 1000
	prvKey, err := utils.GetPrv()
	if err != nil {
		panic("get prv error or delete keystore after restart")
	}
	conf.GetConfig().SignPrivateKey = prvKey
	ecdsaPub := prvKey.PublicKey
	conf.GetConfig().SignPub = common.Bytes2Hex(crypto.FromECDSAPub(&ecdsaPub))
	log.Info("PublicKey", conf.GetConfig().SignPub)
	publicAddr := crypto.PubkeyToAddress(ecdsaPub)
	log.Info("publicAddr:", publicAddr)
	conf.GetConfig().SignPublicAddress = publicAddr
duanjinfei's avatar
duanjinfei committed
106 107 108 109 110 111 112 113
}

func Execute() {
	if err := RootCmd.Execute(); err != nil {
		log.Error("root cmd execute failed", err)
		os.Exit(-1)
	}
}