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

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