github.com/Synthesix/Sia@v1.3.3-0.20180413141344-f863baeed3ca/cmd/siac/daemoncmd.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Synthesix/Sia/build"
     7  	"github.com/Synthesix/Sia/node/api"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var (
    12  	stopCmd = &cobra.Command{
    13  		Use:   "stop",
    14  		Short: "Stop the Sia daemon",
    15  		Long:  "Stop the Sia daemon.",
    16  		Run:   wrap(stopcmd),
    17  	}
    18  
    19  	updateCheckCmd = &cobra.Command{
    20  		Use:   "check",
    21  		Short: "Check for available updates",
    22  		Long:  "Check for available updates.",
    23  		Run:   wrap(updatecheckcmd),
    24  	}
    25  
    26  	updateCmd = &cobra.Command{
    27  		Use:   "update",
    28  		Short: "Update Sia",
    29  		Long:  "Check for (and/or download) available updates for Sia.",
    30  		Run:   wrap(updatecmd),
    31  	}
    32  
    33  	versionCmd = &cobra.Command{
    34  		Use:   "version",
    35  		Short: "Print version information",
    36  		Long:  "Print version information.",
    37  		Run:   wrap(versioncmd),
    38  	}
    39  )
    40  
    41  type updateInfo struct {
    42  	Available bool   `json:"available"`
    43  	Version   string `json:"version"`
    44  }
    45  
    46  // version prints the version of siac and siad.
    47  func versioncmd() {
    48  	fmt.Println("Sia Client")
    49  	fmt.Println("\tVersion " + build.Version)
    50  	if build.GitRevision != "" {
    51  		fmt.Println("\tGit Revision " + build.GitRevision)
    52  		fmt.Println("\tBuild Time   " + build.BuildTime)
    53  	}
    54  	var dvg api.DaemonVersionGet
    55  	err := getAPI("/daemon/version", &dvg)
    56  	if err != nil {
    57  		fmt.Println("Could not get daemon version:", err)
    58  		return
    59  	}
    60  	fmt.Println("Sia Daemon")
    61  	fmt.Println("\tVersion " + dvg.Version)
    62  	if build.GitRevision != "" {
    63  		fmt.Println("\tGit Revision " + dvg.GitRevision)
    64  		fmt.Println("\tBuild Time   " + dvg.BuildTime)
    65  	}
    66  }
    67  
    68  // stopcmd is the handler for the command `siac stop`.
    69  // Stops the daemon.
    70  func stopcmd() {
    71  	err := get("/daemon/stop")
    72  	if err != nil {
    73  		die("Could not stop daemon:", err)
    74  	}
    75  	fmt.Println("Sia daemon stopped.")
    76  }
    77  
    78  func updatecmd() {
    79  	var update updateInfo
    80  	err := getAPI("/daemon/update", &update)
    81  	if err != nil {
    82  		fmt.Println("Could not check for update:", err)
    83  		return
    84  	}
    85  	if !update.Available {
    86  		fmt.Println("Already up to date.")
    87  		return
    88  	}
    89  
    90  	err = post("/daemon/update", "")
    91  	if err != nil {
    92  		fmt.Println("Could not apply update:", err)
    93  		return
    94  	}
    95  	fmt.Printf("Updated to version %s! Restart siad now.\n", update.Version)
    96  }
    97  
    98  func updatecheckcmd() {
    99  	var update updateInfo
   100  	err := getAPI("/daemon/update", &update)
   101  	if err != nil {
   102  		fmt.Println("Could not check for update:", err)
   103  		return
   104  	}
   105  	if update.Available {
   106  		fmt.Printf("A new release (v%s) is available! Run 'siac update' to install it.\n", update.Version)
   107  	} else {
   108  		fmt.Println("Up to date.")
   109  	}
   110  }