OSDN Git Service

Dev (#148)
[bytom/bytom.git] / cmd / cobra / commands / mining.go
1 package commands
2
3 import (
4         "context"
5         "encoding/json"
6         "strconv"
7
8         "github.com/spf13/cobra"
9         jww "github.com/spf13/jwalterweatherman"
10
11         "github.com/bytom/blockchain"
12 )
13
14 var isMiningCmd = &cobra.Command{
15         Use:   "is-mining",
16         Short: "If client is actively mining new blocks",
17         Run: func(cmd *cobra.Command, args []string) {
18                 var rawResponse []byte
19                 var response blockchain.Response
20                 client := mustRPCClient()
21                 client.Call(context.Background(), "/is-mining", nil, &rawResponse)
22
23                 if err := json.Unmarshal(rawResponse, &response); err != nil {
24                         jww.ERROR.Println(err)
25                         return
26                 }
27
28                 if response.Status == blockchain.SUCCESS {
29                         data := response.Data
30                         res, err := strconv.ParseBool(data[0])
31                         if err != nil {
32                                 jww.ERROR.Println("Fail to parse response data")
33                                 return
34                         }
35                         jww.FEEDBACK.Printf("is mining: %v\n", res)
36                         return
37                 }
38                 jww.ERROR.Println(response.Msg)
39         },
40 }