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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"text/tabwriter"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/NebulousLabs/Sia/api"
    11  )
    12  
    13  var (
    14  	gatewayCmd = &cobra.Command{
    15  		Use:   "gateway",
    16  		Short: "Perform gateway actions",
    17  		Long:  "Add or remove a peer, view the current peer list, or synchronize to the network.",
    18  		Run:   wrap(gatewaycmd),
    19  	}
    20  
    21  	gatewayAddCmd = &cobra.Command{
    22  		Use:   "add [address]",
    23  		Short: "Add a peer",
    24  		Long:  "Add a new peer to the peer list.",
    25  		Run:   wrap(gatewayaddcmd),
    26  	}
    27  
    28  	gatewayRemoveCmd = &cobra.Command{
    29  		Use:   "remove [address]",
    30  		Short: "Remove a peer",
    31  		Long:  "Remove a peer from the peer list.",
    32  		Run:   wrap(gatewayremovecmd),
    33  	}
    34  
    35  	gatewayAddressCmd = &cobra.Command{
    36  		Use:   "address",
    37  		Short: "Print the gateway address",
    38  		Long:  "Print the network address of the gateway.",
    39  		Run:   wrap(gatewayaddresscmd),
    40  	}
    41  
    42  	gatewayListCmd = &cobra.Command{
    43  		Use:   "list",
    44  		Short: "View a list of peers",
    45  		Long:  "View the current peer list.",
    46  		Run:   wrap(gatewaylistcmd),
    47  	}
    48  )
    49  
    50  // gatewayaddcmd is the handler for the command `siac gateway add [address]`.
    51  // Adds a new peer to the peer list.
    52  func gatewayaddcmd(addr string) {
    53  	err := post("/gateway/add/"+addr, "")
    54  	if err != nil {
    55  		die("Could not add peer:", err)
    56  	}
    57  	fmt.Println("Added", addr, "to peer list.")
    58  }
    59  
    60  // gatewayremovecmd is the handler for the command `siac gateway remove [address]`.
    61  // Removes a peer from the peer list.
    62  func gatewayremovecmd(addr string) {
    63  	err := post("/gateway/remove/"+addr, "")
    64  	if err != nil {
    65  		die("Could not remove peer:", err)
    66  	}
    67  	fmt.Println("Removed", addr, "from peer list.")
    68  }
    69  
    70  // gatewayaddresscmd is the handler for the command `siac gateway address`.
    71  // Prints the gateway's network address.
    72  func gatewayaddresscmd() {
    73  	var info api.GatewayInfo
    74  	err := getAPI("/gateway", &info)
    75  	if err != nil {
    76  		die("Could not get gateway address:", err)
    77  	}
    78  	fmt.Println("Address:", info.NetAddress)
    79  }
    80  
    81  // gatewaycmd is the handler for the command `siac gateway`.
    82  // Prints the gateway's network address and number of peers.
    83  func gatewaycmd() {
    84  	var info api.GatewayInfo
    85  	err := getAPI("/gateway", &info)
    86  	if err != nil {
    87  		die("Could not get gateway address:", err)
    88  	}
    89  	fmt.Println("Address:", info.NetAddress)
    90  	fmt.Println("Active peers:", len(info.Peers))
    91  }
    92  
    93  // gatewaylistcmd is the handler for the command `siac gateway list`.
    94  // Prints a list of all peers.
    95  func gatewaylistcmd() {
    96  	var info api.GatewayInfo
    97  	err := getAPI("/gateway", &info)
    98  	if err != nil {
    99  		die("Could not get peer list:", err)
   100  	}
   101  	if len(info.Peers) == 0 {
   102  		fmt.Println("No peers to show.")
   103  		return
   104  	}
   105  	fmt.Println(len(info.Peers), "active peers:")
   106  	w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
   107  	fmt.Fprintln(w, "Version\tOutbound\tAddress")
   108  	for _, peer := range info.Peers {
   109  		fmt.Fprintf(w, "%v\t%v\t%v\n", peer.Version, yesNo(!peer.Inbound), peer.NetAddress)
   110  	}
   111  	w.Flush()
   112  }