github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/cmd/spc/minercmd.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  )
     8  
     9  var (
    10  	minerCmd = &cobra.Command{
    11  		Use:   "miner",
    12  		Short: "Perform miner actions",
    13  		Long:  "Perform miner actions and view miner status.",
    14  		Run:   wrap(minercmd),
    15  	}
    16  
    17  	minerStartCmd = &cobra.Command{
    18  		Use:   "start",
    19  		Short: "Start cpu mining",
    20  		Long:  "Start cpu mining, if the miner is already running, this command does nothing",
    21  		Run:   wrap(minerstartcmd),
    22  	}
    23  
    24  	minerStopCmd = &cobra.Command{
    25  		Use:   "stop",
    26  		Short: "Stop mining",
    27  		Long:  "Stop mining (this may take a few moments).",
    28  		Run:   wrap(minerstopcmd),
    29  	}
    30  )
    31  
    32  // minerstartcmd is the handler for the command `siac miner start`.
    33  // Starts the CPU miner.
    34  func minerstartcmd() {
    35  	err := httpClient.MinerStartGet()
    36  	if err != nil {
    37  		die("Could not start miner:", err)
    38  	}
    39  	fmt.Println("CPU Miner is now running.")
    40  }
    41  
    42  // minercmd is the handler for the command `siac miner`.
    43  // Prints the status of the miner.
    44  func minercmd() {
    45  	status, err := httpClient.MinerGet()
    46  	if err != nil {
    47  		die("Could not get miner status:", err)
    48  	}
    49  
    50  	miningStr := "off"
    51  	if status.CPUMining {
    52  		miningStr = "on"
    53  	}
    54  	fmt.Printf(`Miner status:
    55  CPU Mining:   %s
    56  CPU Hashrate: %v KH/s
    57  Blocks Mined: %d (%d stale)
    58  `, miningStr, status.CPUHashrate/1000, status.BlocksMined, status.StaleBlocksMined)
    59  }
    60  
    61  // minerstopcmd is the handler for the command `siac miner stop`.
    62  // Stops the CPU miner.
    63  func minerstopcmd() {
    64  	err := httpClient.MinerStopGet()
    65  	if err != nil {
    66  		die("Could not stop miner:", err)
    67  	}
    68  	fmt.Println("Stopped mining.")
    69  }