github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/siac/minercmd.go (about)

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