github.com/Synthesix/Sia@v1.3.3-0.20180413141344-f863baeed3ca/cmd/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/Synthesix/Sia/node/api"
    11  )
    12  
    13  var (
    14  	gatewayAddressCmd = &cobra.Command{
    15  		Use:   "address",
    16  		Short: "Print the gateway address",
    17  		Long:  "Print the network address of the gateway.",
    18  		Run:   wrap(gatewayaddresscmd),
    19  	}
    20  
    21  	gatewayCmd = &cobra.Command{
    22  		Use:   "gateway",
    23  		Short: "Perform gateway actions",
    24  		Long:  "View and manage the gateway's connected peers.",
    25  		Run:   wrap(gatewaycmd),
    26  	}
    27  
    28  	gatewayConnectCmd = &cobra.Command{
    29  		Use:   "connect [address]",
    30  		Short: "Connect to a peer",
    31  		Long:  "Connect to a peer and add it to the node list.",
    32  		Run:   wrap(gatewayconnectcmd),
    33  	}
    34  
    35  	gatewayDisconnectCmd = &cobra.Command{
    36  		Use:   "disconnect [address]",
    37  		Short: "Disconnect from a peer",
    38  		Long:  "Disconnect from a peer. Does not remove the peer from the node list.",
    39  		Run:   wrap(gatewaydisconnectcmd),
    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  // gatewayconnectcmd is the handler for the command `siac gateway add [address]`.
    51  // Adds a new peer to the peer list.
    52  func gatewayconnectcmd(addr string) {
    53  	err := post("/gateway/connect/"+addr, "")
    54  	if err != nil {
    55  		die("Could not add peer:", err)
    56  	}
    57  	fmt.Println("Added", addr, "to peer list.")
    58  }
    59  
    60  // gatewaydisconnectcmd is the handler for the command `siac gateway remove [address]`.
    61  // Removes a peer from the peer list.
    62  func gatewaydisconnectcmd(addr string) {
    63  	err := post("/gateway/disconnect/"+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.GatewayGET
    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.GatewayGET
    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.GatewayGET
    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  }