github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/cmd/bytomcli/commands/mining.go (about)

     1  package commands
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/spf13/cobra"
     8  	jww "github.com/spf13/jwalterweatherman"
     9  
    10  	"github.com/bytom/bytom/util"
    11  )
    12  
    13  var isMiningCmd = &cobra.Command{
    14  	Use:   "is-mining",
    15  	Short: "If client is actively mining new blocks",
    16  	Args:  cobra.NoArgs,
    17  	Run: func(cmd *cobra.Command, args []string) {
    18  		data, exitCode := util.ClientCall("/is-mining")
    19  		if exitCode != util.Success {
    20  			os.Exit(exitCode)
    21  		}
    22  		printJSON(data)
    23  	},
    24  }
    25  
    26  var setMiningCmd = &cobra.Command{
    27  	Use:   "set-mining <true or false>",
    28  	Short: "start or stop mining",
    29  	Args:  cobra.ExactArgs(1),
    30  	Run: func(cmd *cobra.Command, args []string) {
    31  		param := strings.ToLower(args[0])
    32  		isMining := false
    33  		switch param {
    34  		case "true":
    35  			isMining = true
    36  		default:
    37  			isMining = false
    38  		}
    39  
    40  		miningInfo := &struct {
    41  			IsMining bool `json:"is_mining"`
    42  		}{IsMining: isMining}
    43  
    44  		if _, exitCode := util.ClientCall("/set-mining", miningInfo); exitCode != util.Success {
    45  			os.Exit(exitCode)
    46  		}
    47  
    48  		if isMining {
    49  			jww.FEEDBACK.Println("start mining success")
    50  		} else {
    51  			jww.FEEDBACK.Println("stop mining success")
    52  		}
    53  	},
    54  }