From c1f80bd88e6adc31f39f5516615751a03f968ebb Mon Sep 17 00:00:00 2001 From: Nickson Date: Tue, 2 Apr 2019 21:23:04 +0300 Subject: [PATCH 01/11] restructured cli commands package --- cmd/skywire-cli/commands/node/node.go | 15 +++++++++++++++ cmd/skywire-cli/commands/{ => node}/pk.go | 8 +++++--- cmd/skywire-cli/commands/root.go | 16 ++++++++++++++++ cmd/skywire-cli/skywire-cli.go | 5 ++++- 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 cmd/skywire-cli/commands/node/node.go rename cmd/skywire-cli/commands/{ => node}/pk.go (70%) diff --git a/cmd/skywire-cli/commands/node/node.go b/cmd/skywire-cli/commands/node/node.go new file mode 100644 index 0000000000..53cffe9705 --- /dev/null +++ b/cmd/skywire-cli/commands/node/node.go @@ -0,0 +1,15 @@ +package node + +import ( + "github.com/skycoin/skywire/cmd/skywire-cli/commands" + "github.com/spf13/cobra" +) + +var nodeCmd = &cobra.Command{ + Use: "node", + Short: "Commands that interact with the skywire-node", +} + +func init() { + commands.AddCommand(nodeCmd) +} diff --git a/cmd/skywire-cli/commands/pk.go b/cmd/skywire-cli/commands/node/pk.go similarity index 70% rename from cmd/skywire-cli/commands/pk.go rename to cmd/skywire-cli/commands/node/pk.go index 91c2e5f346..9ce9dc3789 100644 --- a/cmd/skywire-cli/commands/pk.go +++ b/cmd/skywire-cli/commands/node/pk.go @@ -1,13 +1,15 @@ -package commands +package node import ( "fmt" + "log" + "github.com/skycoin/skywire/cmd/skywire-cli/commands" "github.com/spf13/cobra" ) func init() { - rootCmd.AddCommand(pkCmd) + nodeCmd.AddCommand(pkCmd) } var pkCmd = &cobra.Command{ @@ -15,7 +17,7 @@ var pkCmd = &cobra.Command{ Short: "get public key of node", Run: func(_ *cobra.Command, _ []string) { - client := rpcClient() + client := commands.PrpcClient() summary, err := client.Summary() if err != nil { log.Fatal("Failed to connect:", err) diff --git a/cmd/skywire-cli/commands/root.go b/cmd/skywire-cli/commands/root.go index e184bf05f3..08f8742e35 100644 --- a/cmd/skywire-cli/commands/root.go +++ b/cmd/skywire-cli/commands/root.go @@ -36,6 +36,22 @@ func rpcClient() node.RPCClient { return node.NewRPCClient(client, node.RPCPrefix) } +// PrpcClient exported rpc method that +func PrpcClient() node.RPCClient { + client, err := rpc.Dial("tcp", rpcAddr) + if err != nil { + log.Fatal("RPC connection failed:", err) + } + return node.NewRPCClient(client, node.RPCPrefix) +} + +// AddCommand adds commands to the root command +func AddCommand(command *cobra.Command) { + rootCmd.AddCommand( + command, + ) +} + func catch(err error, msgs ...string) { if err != nil { if len(msgs) > 0 { diff --git a/cmd/skywire-cli/skywire-cli.go b/cmd/skywire-cli/skywire-cli.go index 7a3aa2c1ea..002bd09e3c 100644 --- a/cmd/skywire-cli/skywire-cli.go +++ b/cmd/skywire-cli/skywire-cli.go @@ -3,7 +3,10 @@ CLI for skywire node */ package main -import "github.com/skycoin/skywire/cmd/skywire-cli/commands" +import ( + "github.com/skycoin/skywire/cmd/skywire-cli/commands" + _ "github.com/skycoin/skywire/cmd/skywire-cli/commands/node" +) func main() { commands.Execute() From d7a261a7ca498409617b098b0d4032cec2bb0f18 Mon Sep 17 00:00:00 2001 From: Nickson Date: Thu, 4 Apr 2019 10:04:04 +0300 Subject: [PATCH 02/11] restructured skywire-cli package --- .../commands/{ => mdisc}/messaging.go | 31 +++--- cmd/skywire-cli/commands/{ => node}/app.go | 24 ++--- .../commands/{ => node}/gen-config.go | 4 +- cmd/skywire-cli/commands/node/node.go | 11 +-- cmd/skywire-cli/commands/node/pk.go | 8 +- cmd/skywire-cli/commands/root.go | 94 +++---------------- .../commands/{ => rtfind}/routing.go | 54 +++++++---- .../{transport.go => tpdisc/tpdisc.go} | 57 ++++++----- cmd/skywire-cli/commands/tpdisc/transport.go | 23 +++++ cmd/skywire-cli/internal/internal.go | 51 ++++++++++ cmd/skywire-cli/skywire-cli.go | 1 - 11 files changed, 193 insertions(+), 165 deletions(-) rename cmd/skywire-cli/commands/{ => mdisc}/messaging.go (69%) rename cmd/skywire-cli/commands/{ => node}/app.go (75%) rename cmd/skywire-cli/commands/{ => node}/gen-config.go (98%) rename cmd/skywire-cli/commands/{ => rtfind}/routing.go (79%) rename cmd/skywire-cli/commands/{transport.go => tpdisc/tpdisc.go} (81%) create mode 100644 cmd/skywire-cli/commands/tpdisc/transport.go create mode 100644 cmd/skywire-cli/internal/internal.go diff --git a/cmd/skywire-cli/commands/messaging.go b/cmd/skywire-cli/commands/mdisc/messaging.go similarity index 69% rename from cmd/skywire-cli/commands/messaging.go rename to cmd/skywire-cli/commands/mdisc/messaging.go index 4200029ad6..5c9fd6d04d 100644 --- a/cmd/skywire-cli/commands/messaging.go +++ b/cmd/skywire-cli/commands/mdisc/messaging.go @@ -1,4 +1,4 @@ -package commands +package mdisc import ( "context" @@ -9,24 +9,23 @@ import ( "github.com/spf13/cobra" + "github.com/skycoin/skywire/cmd/skywire-cli/internal" + "github.com/skycoin/skywire/pkg/messaging-discovery/client" ) -func init() { - rootCmd.AddCommand(messagingCmd) +//MessageDiscoveryCmd contains commands that interact with messaging services +var MessageDiscoveryCmd = &cobra.Command{ + Use: "mdisc", + Short: "Commands that interact with messaging-discovery", } var mdAddr string -var messagingCmd = &cobra.Command{ - Use: "messaging", - Short: "manage operations with messaging services", -} - func init() { - messagingCmd.PersistentFlags().StringVar(&mdAddr, "addr", "https://messaging.discovery.skywire.skycoin.net", "address of messaging discovery server") + MessageDiscoveryCmd.PersistentFlags().StringVar(&mdAddr, "addr", "https://messaging.discovery.skywire.skycoin.net", "address of messaging discovery server") - messagingCmd.AddCommand( + MessageDiscoveryCmd.AddCommand( mEntryCmd, mAvailableServersCmd) } @@ -38,9 +37,9 @@ var mEntryCmd = &cobra.Command{ Run: func(_ *cobra.Command, args []string) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() - pk := parsePK("node-public-key", args[0]) + pk := internal.ParsePK("node-public-key", args[0]) entry, err := client.NewHTTP(mdAddr).Entry(ctx, pk) - catch(err) + internal.Catch(err) fmt.Println(entry) }, } @@ -52,7 +51,7 @@ var mAvailableServersCmd = &cobra.Command{ ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() entries, err := client.NewHTTP(mdAddr).AvailableServers(ctx) - catch(err) + internal.Catch(err) printAvailableServers(entries) }, } @@ -60,11 +59,11 @@ var mAvailableServersCmd = &cobra.Command{ func printAvailableServers(entries []*client.Entry) { w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) _, err := fmt.Fprintln(w, "version\tregistered\tpublic-key\taddress\tport\tconns") - catch(err) + internal.Catch(err) for _, entry := range entries { _, err := fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\t%d\n", entry.Version, entry.Timestamp, entry.Static, entry.Server.Address, entry.Server.Port, entry.Server.AvailableConnections) - catch(err) + internal.Catch(err) } - catch(w.Flush()) + internal.Catch(w.Flush()) } diff --git a/cmd/skywire-cli/commands/app.go b/cmd/skywire-cli/commands/node/app.go similarity index 75% rename from cmd/skywire-cli/commands/app.go rename to cmd/skywire-cli/commands/node/app.go index 691a423400..41ce2bdd93 100644 --- a/cmd/skywire-cli/commands/app.go +++ b/cmd/skywire-cli/commands/node/app.go @@ -1,4 +1,4 @@ -package commands +package node import ( "fmt" @@ -8,11 +8,13 @@ import ( "github.com/spf13/cobra" + "github.com/skycoin/skywire/cmd/skywire-cli/internal" + "github.com/skycoin/skywire/pkg/node" ) func init() { - rootCmd.AddCommand( + NodeCmd.AddCommand( appsCmd, startAppCmd, stopAppCmd, @@ -24,12 +26,12 @@ var appsCmd = &cobra.Command{ Use: "apps", Short: "lists apps running on the node", Run: func(_ *cobra.Command, _ []string) { - states, err := rpcClient().Apps() - catch(err) + states, err := internal.RPCClient().Apps() + internal.Catch(err) w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) _, err = fmt.Fprintln(w, "app\tports\tauto_start\tstatus") - catch(err) + internal.Catch(err) for _, state := range states { status := "stopped" @@ -37,9 +39,9 @@ var appsCmd = &cobra.Command{ status = "running" } _, err = fmt.Fprintf(w, "%s\t%s\t%t\t%s\n", state.Name, strconv.Itoa(int(state.Port)), state.AutoStart, status) - catch(err) + internal.Catch(err) } - catch(w.Flush()) + internal.Catch(w.Flush()) }, } @@ -48,7 +50,7 @@ var startAppCmd = &cobra.Command{ Short: "starts an app of given name", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { - catch(rpcClient().StartApp(args[0])) + internal.Catch(internal.RPCClient().StartApp(args[0])) fmt.Println("OK") }, } @@ -58,7 +60,7 @@ var stopAppCmd = &cobra.Command{ Short: "stops an app of given name", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { - catch(rpcClient().StopApp(args[0])) + internal.Catch(internal.RPCClient().StopApp(args[0])) fmt.Println("OK") }, } @@ -75,9 +77,9 @@ var setAppAutostartCmd = &cobra.Command{ case "off": autostart = false default: - catch(fmt.Errorf("invalid args[1] value: %s", args[1])) + internal.Catch(fmt.Errorf("invalid args[1] value: %s", args[1])) } - catch(rpcClient().SetAutoStart(args[0], autostart)) + internal.Catch(internal.RPCClient().SetAutoStart(args[0], autostart)) fmt.Println("OK") }, } diff --git a/cmd/skywire-cli/commands/gen-config.go b/cmd/skywire-cli/commands/node/gen-config.go similarity index 98% rename from cmd/skywire-cli/commands/gen-config.go rename to cmd/skywire-cli/commands/node/gen-config.go index c7e3868c0f..b906ca92a0 100644 --- a/cmd/skywire-cli/commands/gen-config.go +++ b/cmd/skywire-cli/commands/node/gen-config.go @@ -1,4 +1,4 @@ -package commands +package node import ( "encoding/base64" @@ -19,7 +19,7 @@ var ( ) func init() { - rootCmd.AddCommand(genConfigCmd) + NodeCmd.AddCommand(genConfigCmd) genConfigCmd.Flags().StringVarP(&output, "output", "o", "", "path of output config file. Uses default of 'type' flag if unspecified.") genConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, "whether to allow rewrite of a file that already exists.") genConfigCmd.Flags().VarP(&configLocType, "type", "m", fmt.Sprintf("config generation mode. Valid values: %v", pathutil.AllConfigLocationTypes())) diff --git a/cmd/skywire-cli/commands/node/node.go b/cmd/skywire-cli/commands/node/node.go index 53cffe9705..b3c01ae48e 100644 --- a/cmd/skywire-cli/commands/node/node.go +++ b/cmd/skywire-cli/commands/node/node.go @@ -1,15 +1,14 @@ package node import ( - "github.com/skycoin/skywire/cmd/skywire-cli/commands" + "github.com/skycoin/skycoin/src/util/logging" "github.com/spf13/cobra" ) -var nodeCmd = &cobra.Command{ +var log = logging.MustGetLogger("skywire-cli") + +//NodeCmd contains commands that interact with the skywire-node +var NodeCmd = &cobra.Command{ Use: "node", Short: "Commands that interact with the skywire-node", } - -func init() { - commands.AddCommand(nodeCmd) -} diff --git a/cmd/skywire-cli/commands/node/pk.go b/cmd/skywire-cli/commands/node/pk.go index 9ce9dc3789..807f608a3b 100644 --- a/cmd/skywire-cli/commands/node/pk.go +++ b/cmd/skywire-cli/commands/node/pk.go @@ -2,14 +2,14 @@ package node import ( "fmt" - "log" - "github.com/skycoin/skywire/cmd/skywire-cli/commands" + "github.com/skycoin/skywire/cmd/skywire-cli/internal" + "github.com/spf13/cobra" ) func init() { - nodeCmd.AddCommand(pkCmd) + NodeCmd.AddCommand(pkCmd) } var pkCmd = &cobra.Command{ @@ -17,7 +17,7 @@ var pkCmd = &cobra.Command{ Short: "get public key of node", Run: func(_ *cobra.Command, _ []string) { - client := commands.PrpcClient() + client := internal.RPCClient() summary, err := client.Summary() if err != nil { log.Fatal("Failed to connect:", err) diff --git a/cmd/skywire-cli/commands/root.go b/cmd/skywire-cli/commands/root.go index 08f8742e35..abd186a3a8 100644 --- a/cmd/skywire-cli/commands/root.go +++ b/cmd/skywire-cli/commands/root.go @@ -1,20 +1,13 @@ package commands import ( - "fmt" - "net/rpc" - "strconv" - - "github.com/google/uuid" - "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/cmd/skywire-cli/commands/mdisc" + "github.com/skycoin/skywire/cmd/skywire-cli/commands/node" + "github.com/skycoin/skywire/cmd/skywire-cli/commands/rtfind" + "github.com/skycoin/skywire/cmd/skywire-cli/commands/tpdisc" "github.com/spf13/cobra" - - "github.com/skycoin/skywire/pkg/cipher" - "github.com/skycoin/skywire/pkg/node" ) -var log = logging.MustGetLogger("skywire-cli") - var rpcAddr string var rootCmd = &cobra.Command{ @@ -22,78 +15,17 @@ var rootCmd = &cobra.Command{ Short: "Command Line Interface for skywire", } -// Execute executes root CLI command. -func Execute() { - rootCmd.PersistentFlags().StringVarP(&rpcAddr, "rpc", "", "localhost:3435", "RPC server address") - rootCmd.Execute() //nolint:errcheck -} - -func rpcClient() node.RPCClient { - client, err := rpc.Dial("tcp", rpcAddr) - if err != nil { - log.Fatal("RPC connection failed:", err) - } - return node.NewRPCClient(client, node.RPCPrefix) -} - -// PrpcClient exported rpc method that -func PrpcClient() node.RPCClient { - client, err := rpc.Dial("tcp", rpcAddr) - if err != nil { - log.Fatal("RPC connection failed:", err) - } - return node.NewRPCClient(client, node.RPCPrefix) -} - -// AddCommand adds commands to the root command -func AddCommand(command *cobra.Command) { +func init() { rootCmd.AddCommand( - command, + node.NodeCmd, + mdisc.MessageDiscoveryCmd, + rtfind.RtFindCmd, + tpdisc.TransportCmd, ) } -func catch(err error, msgs ...string) { - if err != nil { - if len(msgs) > 0 { - log.Fatalln(append(msgs, err.Error())) - } else { - log.Fatalln(err) - } - } -} - -type transportID uuid.UUID - -// String implements pflag.Value -func (t transportID) String() string { return uuid.UUID(t).String() } - -// Type implements pflag.Value -func (transportID) Type() string { return "transportID" } - -// Set implements pflag.Value -func (t *transportID) Set(s string) error { - tID, err := uuid.Parse(s) - if err != nil { - return err - } - *t = transportID(tID) - return nil -} - -func parsePK(name, v string) cipher.PubKey { - var pk cipher.PubKey - catch(pk.Set(v), fmt.Sprintf("failed to parse <%s>:", name)) - return pk -} - -func parseUUID(name, v string) uuid.UUID { - id, err := uuid.Parse(v) - catch(err, fmt.Sprintf("failed to parse <%s>:", name)) - return id -} - -func parseUint(name, v string, bitSize int) uint64 { - i, err := strconv.ParseUint(v, 10, bitSize) - catch(err, fmt.Sprintf("failed to parse <%s>:", name)) - return i +// Execute executes root CLI command. +func Execute() { + rootCmd.PersistentFlags().StringVarP(&rpcAddr, "rpc", "", "localhost:3435", "RPC server address") + rootCmd.Execute() //nolint:errcheck } diff --git a/cmd/skywire-cli/commands/routing.go b/cmd/skywire-cli/commands/rtfind/routing.go similarity index 79% rename from cmd/skywire-cli/commands/routing.go rename to cmd/skywire-cli/commands/rtfind/routing.go index f6fe3fbba4..919aae941c 100644 --- a/cmd/skywire-cli/commands/routing.go +++ b/cmd/skywire-cli/commands/rtfind/routing.go @@ -1,4 +1,4 @@ -package commands +package rtfind import ( "errors" @@ -11,6 +11,8 @@ import ( "github.com/spf13/cobra" + "github.com/skycoin/skywire/cmd/skywire-cli/internal" + "github.com/skycoin/skywire/pkg/cipher" "github.com/skycoin/skywire/pkg/node" "github.com/skycoin/skywire/pkg/route-finder/client" @@ -18,8 +20,14 @@ import ( "github.com/skycoin/skywire/pkg/routing" ) +// RtFindCmd contains commands that interact with the route finder +var RtFindCmd = &cobra.Command{ + Use: "rtfind", + Short: "Commands that interact with the route-finder", +} + func init() { - rootCmd.AddCommand( + RtFindCmd.AddCommand( listRulesCmd, ruleCmd, rmRuleCmd, @@ -31,8 +39,8 @@ var listRulesCmd = &cobra.Command{ Use: "list-rules", Short: "lists the local node's routing rules", Run: func(_ *cobra.Command, _ []string) { - rules, err := rpcClient().RoutingRules() - catch(err) + rules, err := internal.RPCClient().RoutingRules() + internal.Catch(err) printRoutingRules(rules...) }, @@ -44,10 +52,10 @@ var ruleCmd = &cobra.Command{ Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { id, err := strconv.ParseUint(args[0], 10, 32) - catch(err) + internal.Catch(err) - rule, err := rpcClient().RoutingRule(routing.RouteID(id)) - catch(err) + rule, err := internal.RPCClient().RoutingRule(routing.RouteID(id)) + internal.Catch(err) printRoutingRules(&node.RoutingEntry{Key: rule.RouteID(), Value: rule}) }, @@ -59,8 +67,8 @@ var rmRuleCmd = &cobra.Command{ Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { id, err := strconv.ParseUint(args[0], 10, 32) - catch(err) - catch(rpcClient().RemoveRoutingRule(routing.RouteID(id))) + internal.Catch(err) + internal.Catch(internal.RPCClient().RemoveRoutingRule(routing.RouteID(id))) fmt.Println("OK") }, } @@ -97,7 +105,7 @@ var addRuleCmd = &cobra.Command{ case "app": var ( routeID = routing.RouteID(parseUint("route-id", args[1], 32)) - remotePK = parsePK("remote-pk", args[2]) + remotePK = internal.ParsePK("remote-pk", args[2]) remotePort = uint16(parseUint("remote-port", args[3], 16)) localPort = uint16(parseUint("local-port", args[4], 16)) ) @@ -105,12 +113,12 @@ var addRuleCmd = &cobra.Command{ case "fwd": var ( nextRouteID = routing.RouteID(parseUint("next-route-id", args[1], 32)) - nextTpID = parseUUID("next-transport-id", args[2]) + nextTpID = internal.ParseUUID("next-transport-id", args[2]) ) rule = routing.ForwardRule(time.Now().Add(expire), nextRouteID, nextTpID) } - rIDKey, err := rpcClient().AddRoutingRule(rule) - catch(err) + rIDKey, err := internal.RPCClient().AddRoutingRule(rule) + internal.Catch(err) fmt.Println("Routing Rule Key:", rIDKey) }, } @@ -126,11 +134,11 @@ var findRoutesCmd = &cobra.Command{ rfc := client.NewHTTP(frAddr) var srcPK, dstPK cipher.PubKey - catch(srcPK.Set(args[0])) - catch(dstPK.Set(args[1])) + internal.Catch(srcPK.Set(args[0])) + internal.Catch(dstPK.Set(args[1])) forward, reverse, err := rfc.PairedRoutes(srcPK, dstPK, frMinHops, frMaxHops) - catch(err) + internal.Catch(err) fmt.Println("forward: ", forward) fmt.Println("reverse: ", reverse) @@ -147,16 +155,16 @@ func printRoutingRules(rules ...*node.RoutingEntry) { printAppRule := func(w io.Writer, id routing.RouteID, s *routing.RuleSummary) { _, err := fmt.Fprintf(w, "%d\t%s\t%d\t%d\t%s\t%d\t%s\t%s\t%s\n", id, s.Type, s.AppFields.LocalPort, s.AppFields.RemotePort, s.AppFields.RemotePK, s.AppFields.RespRID, "-", "-", s.ExpireAt) - catch(err) + internal.Catch(err) } printFwdRule := func(w io.Writer, id routing.RouteID, s *routing.RuleSummary) { _, err := fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\n", id, s.Type, "-", "-", "-", "-", s.ForwardFields.NextRID, s.ForwardFields.NextTID, s.ExpireAt) - catch(err) + internal.Catch(err) } w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) _, err := fmt.Fprintln(w, "id\ttype\tlocal-port\tremote-port\tremote-pk\tresp-id\tnext-route-id\tnext-transport-id\texpire-at") - catch(err) + internal.Catch(err) for _, rule := range rules { if rule.Value.Summary().AppFields != nil { printAppRule(w, rule.Key, rule.Value.Summary()) @@ -164,5 +172,11 @@ func printRoutingRules(rules ...*node.RoutingEntry) { printFwdRule(w, rule.Key, rule.Value.Summary()) } } - catch(w.Flush()) + internal.Catch(w.Flush()) +} + +func parseUint(name, v string, bitSize int) uint64 { + i, err := strconv.ParseUint(v, 10, bitSize) + internal.Catch(err, fmt.Sprintf("failed to parse <%s>:", name)) + return i } diff --git a/cmd/skywire-cli/commands/transport.go b/cmd/skywire-cli/commands/tpdisc/tpdisc.go similarity index 81% rename from cmd/skywire-cli/commands/transport.go rename to cmd/skywire-cli/commands/tpdisc/tpdisc.go index ce4311c142..b17d175022 100644 --- a/cmd/skywire-cli/commands/transport.go +++ b/cmd/skywire-cli/commands/tpdisc/tpdisc.go @@ -1,4 +1,4 @@ -package commands +package tpdisc import ( "context" @@ -12,28 +12,37 @@ import ( "github.com/google/uuid" "github.com/spf13/cobra" + "github.com/skycoin/skywire/cmd/skywire-cli/internal" + "github.com/skycoin/skywire/pkg/cipher" "github.com/skycoin/skywire/pkg/node" "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/transport-discovery/client" ) +//TransportCmd contains commands that interact with transport-discovery +var TransportCmd = &cobra.Command{ + Use: "tpdisc", + Short: "Commands that interact with transport-discovery", +} + func init() { - rootCmd.AddCommand( + TransportCmd.AddCommand( transportTypesCmd, listTransportsCmd, transportCmd, addTransportCmd, rmTransportCmd, - findTransport) + findTransport, + ) } var transportTypesCmd = &cobra.Command{ Use: "transport-types", Short: "lists transport types used by the local node", Run: func(_ *cobra.Command, _ []string) { - types, err := rpcClient().TransportTypes() - catch(err) + types, err := internal.RPCClient().TransportTypes() + internal.Catch(err) for _, t := range types { fmt.Println(t) } @@ -50,8 +59,8 @@ var listTransportsCmd = &cobra.Command{ Use: "list-transports", Short: "lists the available transports with optional filter flags", Run: func(_ *cobra.Command, _ []string) { - transports, err := rpcClient().Transports(filterTypes, filterPubKeys, showLogs) - catch(err) + transports, err := internal.RPCClient().Transports(filterTypes, filterPubKeys, showLogs) + internal.Catch(err) printTransports(transports...) }, } @@ -67,9 +76,9 @@ var transportCmd = &cobra.Command{ Short: "returns summary of given transport by id", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { - tpID := parseUUID("transport-id", args[0]) - tp, err := rpcClient().Transport(tpID) - catch(err) + tpID := internal.ParseUUID("transport-id", args[0]) + tp, err := internal.RPCClient().Transport(tpID) + internal.Catch(err) printTransports(tp) }, } @@ -85,9 +94,9 @@ var addTransportCmd = &cobra.Command{ Short: "adds a new transport", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { - pk := parsePK("remote-public-key", args[0]) - tp, err := rpcClient().AddTransport(pk, transportType, public, timeout) - catch(err) + pk := internal.ParsePK("remote-public-key", args[0]) + tp, err := internal.RPCClient().AddTransport(pk, transportType, public, timeout) + internal.Catch(err) printTransports(tp) }, } @@ -103,8 +112,8 @@ var rmTransportCmd = &cobra.Command{ Short: "removes transport with given id", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { - tID := parseUUID("transport-id", args[0]) - catch(rpcClient().RemoveTransport(tID)) + tID := internal.ParseUUID("transport-id", args[0]) + internal.Catch(internal.RPCClient().RemoveTransport(tID)) fmt.Println("OK") }, } @@ -136,14 +145,14 @@ var findTransport = &cobra.Command{ defer cancel() pk, sk := cipher.GenerateKeyPair() c, err := client.NewHTTP(addr, pk, sk) - catch(err) + internal.Catch(err) if tpPK.Null() { entry, err := c.GetTransportByID(ctx, uuid.UUID(tpID)) - catch(err) + internal.Catch(err) printTransportEntries(entry) } else { entries, err := c.GetTransportsByEdge(ctx, pk) - catch(err) + internal.Catch(err) printTransportEntries(entries...) } }, @@ -159,24 +168,24 @@ func printTransports(tps ...*node.TransportSummary) { sortTransports(tps...) w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) _, err := fmt.Fprintln(w, "type\tid\tlocal\tremote") - catch(err) + internal.Catch(err) for _, tp := range tps { _, err = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", tp.Type, tp.ID, tp.Local, tp.Remote) - catch(err) + internal.Catch(err) } - catch(w.Flush()) + internal.Catch(w.Flush()) } func printTransportEntries(entries ...*transport.EntryWithStatus) { w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) _, err := fmt.Fprintln(w, "id\ttype\tpublic\tregistered\tup\tedge1\tedge2\topinion1\topinion2") - catch(err) + internal.Catch(err) for _, e := range entries { _, err := fmt.Fprintf(w, "%s\t%s\t%t\t%d\t%t\t%s\t%s\t%t\t%t\n", e.Entry.ID, e.Entry.Type, e.Entry.Public, e.Registered, e.IsUp, e.Entry.Edges[0], e.Entry.Edges[1], e.Statuses[0], e.Statuses[1]) - catch(err) + internal.Catch(err) } - catch(w.Flush()) + internal.Catch(w.Flush()) } func sortTransports(tps ...*node.TransportSummary) { diff --git a/cmd/skywire-cli/commands/tpdisc/transport.go b/cmd/skywire-cli/commands/tpdisc/transport.go new file mode 100644 index 0000000000..e1570afb81 --- /dev/null +++ b/cmd/skywire-cli/commands/tpdisc/transport.go @@ -0,0 +1,23 @@ +package tpdisc + +import ( + "github.com/google/uuid" +) + +type transportID uuid.UUID + +// String implements pflag.Value +func (t transportID) String() string { return uuid.UUID(t).String() } + +// Type implements pflag.Value +func (transportID) Type() string { return "transportID" } + +// Set implements pflag.Value +func (t *transportID) Set(s string) error { + tID, err := uuid.Parse(s) + if err != nil { + return err + } + *t = transportID(tID) + return nil +} diff --git a/cmd/skywire-cli/internal/internal.go b/cmd/skywire-cli/internal/internal.go new file mode 100644 index 0000000000..a43cddb46d --- /dev/null +++ b/cmd/skywire-cli/internal/internal.go @@ -0,0 +1,51 @@ +package internal + +import ( + "fmt" + "net/rpc" + + "github.com/google/uuid" + + "github.com/skycoin/skywire/pkg/cipher" + + "github.com/skycoin/skycoin/src/util/logging" + "github.com/skycoin/skywire/pkg/node" +) + +var log = logging.MustGetLogger("skywire-cli") + +var rpcAddr string + +//RPCClient connects to the nodes rpc methods +func RPCClient() node.RPCClient { + client, err := rpc.Dial("tcp", rpcAddr) + if err != nil { + log.Fatal("RPC connection failed:", err) + } + return node.NewRPCClient(client, node.RPCPrefix) +} + +//Catch handles errors for skywire-cli commands packages +func Catch(err error, msgs ...string) { + if err != nil { + if len(msgs) > 0 { + log.Fatalln(append(msgs, err.Error())) + } else { + log.Fatalln(err) + } + } +} + +//ParsePK parses a public key +func ParsePK(name, v string) cipher.PubKey { + var pk cipher.PubKey + Catch(pk.Set(v), fmt.Sprintf("failed to parse <%s>:", name)) + return pk +} + +//ParseUUID parses a uuid +func ParseUUID(name, v string) uuid.UUID { + id, err := uuid.Parse(v) + Catch(err, fmt.Sprintf("failed to parse <%s>:", name)) + return id +} diff --git a/cmd/skywire-cli/skywire-cli.go b/cmd/skywire-cli/skywire-cli.go index 002bd09e3c..f5be516a27 100644 --- a/cmd/skywire-cli/skywire-cli.go +++ b/cmd/skywire-cli/skywire-cli.go @@ -5,7 +5,6 @@ package main import ( "github.com/skycoin/skywire/cmd/skywire-cli/commands" - _ "github.com/skycoin/skywire/cmd/skywire-cli/commands/node" ) func main() { From 2873fce2f4130adbcec3b74587dbadf5686da300 Mon Sep 17 00:00:00 2001 From: Nickson Date: Thu, 4 Apr 2019 11:44:26 +0300 Subject: [PATCH 03/11] minor edits to comments --- cmd/skywire-cli/commands/mdisc/messaging.go | 2 +- cmd/skywire-cli/commands/node/node.go | 2 +- cmd/skywire-cli/commands/tpdisc/tpdisc.go | 2 +- cmd/skywire-cli/internal/internal.go | 8 ++++---- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/skywire-cli/commands/mdisc/messaging.go b/cmd/skywire-cli/commands/mdisc/messaging.go index 5c9fd6d04d..f482c9e1e9 100644 --- a/cmd/skywire-cli/commands/mdisc/messaging.go +++ b/cmd/skywire-cli/commands/mdisc/messaging.go @@ -14,7 +14,7 @@ import ( "github.com/skycoin/skywire/pkg/messaging-discovery/client" ) -//MessageDiscoveryCmd contains commands that interact with messaging services +// MessageDiscoveryCmd contains commands that interact with messaging services var MessageDiscoveryCmd = &cobra.Command{ Use: "mdisc", Short: "Commands that interact with messaging-discovery", diff --git a/cmd/skywire-cli/commands/node/node.go b/cmd/skywire-cli/commands/node/node.go index b3c01ae48e..182bf8d643 100644 --- a/cmd/skywire-cli/commands/node/node.go +++ b/cmd/skywire-cli/commands/node/node.go @@ -7,7 +7,7 @@ import ( var log = logging.MustGetLogger("skywire-cli") -//NodeCmd contains commands that interact with the skywire-node +// NodeCmd contains commands that interact with the skywire-node var NodeCmd = &cobra.Command{ Use: "node", Short: "Commands that interact with the skywire-node", diff --git a/cmd/skywire-cli/commands/tpdisc/tpdisc.go b/cmd/skywire-cli/commands/tpdisc/tpdisc.go index b17d175022..2062d3ae21 100644 --- a/cmd/skywire-cli/commands/tpdisc/tpdisc.go +++ b/cmd/skywire-cli/commands/tpdisc/tpdisc.go @@ -20,7 +20,7 @@ import ( "github.com/skycoin/skywire/pkg/transport-discovery/client" ) -//TransportCmd contains commands that interact with transport-discovery +// TransportCmd contains commands that interact with transport-discovery var TransportCmd = &cobra.Command{ Use: "tpdisc", Short: "Commands that interact with transport-discovery", diff --git a/cmd/skywire-cli/internal/internal.go b/cmd/skywire-cli/internal/internal.go index a43cddb46d..9a38d76370 100644 --- a/cmd/skywire-cli/internal/internal.go +++ b/cmd/skywire-cli/internal/internal.go @@ -16,7 +16,7 @@ var log = logging.MustGetLogger("skywire-cli") var rpcAddr string -//RPCClient connects to the nodes rpc methods +// RPCClient connects to the nodes rpc methods func RPCClient() node.RPCClient { client, err := rpc.Dial("tcp", rpcAddr) if err != nil { @@ -25,7 +25,7 @@ func RPCClient() node.RPCClient { return node.NewRPCClient(client, node.RPCPrefix) } -//Catch handles errors for skywire-cli commands packages +// Catch handles errors for skywire-cli commands packages func Catch(err error, msgs ...string) { if err != nil { if len(msgs) > 0 { @@ -36,14 +36,14 @@ func Catch(err error, msgs ...string) { } } -//ParsePK parses a public key +// ParsePK parses a public key func ParsePK(name, v string) cipher.PubKey { var pk cipher.PubKey Catch(pk.Set(v), fmt.Sprintf("failed to parse <%s>:", name)) return pk } -//ParseUUID parses a uuid +// ParseUUID parses a uuid func ParseUUID(name, v string) uuid.UUID { id, err := uuid.Parse(v) Catch(err, fmt.Sprintf("failed to parse <%s>:", name)) From b85aee6f251fa7aa5d8a6641f64b8c4bb11f2ce6 Mon Sep 17 00:00:00 2001 From: ivcosla Date: Thu, 4 Apr 2019 11:40:31 +0200 Subject: [PATCH 04/11] fixed ack issue --- internal/ioutil/ack.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ioutil/ack.go b/internal/ioutil/ack.go index be5bad8ec2..ef737796d8 100644 --- a/internal/ioutil/ack.go +++ b/internal/ioutil/ack.go @@ -142,7 +142,7 @@ func (arw *AckReadWriter) serveLoop() { break } - go arw.confirm(data[1], data[2:34]) + arw.confirm(data[1], data[2:34]) data = data[34:] } From c4cd562a614d2bd03d40ae94fcc05936981a7076 Mon Sep 17 00:00:00 2001 From: Nickson Date: Thu, 4 Apr 2019 13:06:53 +0300 Subject: [PATCH 05/11] moved commands that use rpcClient to node command --- .../commands/mdisc/{messaging.go => mdisc.go} | 17 +-- cmd/skywire-cli/commands/node/app.go | 10 +- cmd/skywire-cli/commands/node/gen-config.go | 2 +- cmd/skywire-cli/commands/node/node.go | 22 ++- cmd/skywire-cli/commands/node/pk.go | 6 +- .../{rtfind/routing.go => node/routes.go} | 50 +------ cmd/skywire-cli/commands/node/transports.go | 125 ++++++++++++++++++ cmd/skywire-cli/commands/root.go | 11 +- cmd/skywire-cli/commands/rtfind/rtfind.go | 52 ++++++++ cmd/skywire-cli/commands/tpdisc/tpdisc.go | 112 +--------------- cmd/skywire-cli/internal/internal.go | 13 -- 11 files changed, 229 insertions(+), 191 deletions(-) rename cmd/skywire-cli/commands/mdisc/{messaging.go => mdisc.go} (78%) rename cmd/skywire-cli/commands/{rtfind/routing.go => node/routes.go} (71%) create mode 100644 cmd/skywire-cli/commands/node/transports.go create mode 100644 cmd/skywire-cli/commands/rtfind/rtfind.go diff --git a/cmd/skywire-cli/commands/mdisc/messaging.go b/cmd/skywire-cli/commands/mdisc/mdisc.go similarity index 78% rename from cmd/skywire-cli/commands/mdisc/messaging.go rename to cmd/skywire-cli/commands/mdisc/mdisc.go index f482c9e1e9..5e62733ee4 100644 --- a/cmd/skywire-cli/commands/mdisc/messaging.go +++ b/cmd/skywire-cli/commands/mdisc/mdisc.go @@ -14,8 +14,8 @@ import ( "github.com/skycoin/skywire/pkg/messaging-discovery/client" ) -// MessageDiscoveryCmd contains commands that interact with messaging services -var MessageDiscoveryCmd = &cobra.Command{ +// RootCmd contains commands that interact with messaging services +var RootCmd = &cobra.Command{ Use: "mdisc", Short: "Commands that interact with messaging-discovery", } @@ -23,14 +23,15 @@ var MessageDiscoveryCmd = &cobra.Command{ var mdAddr string func init() { - MessageDiscoveryCmd.PersistentFlags().StringVar(&mdAddr, "addr", "https://messaging.discovery.skywire.skycoin.net", "address of messaging discovery server") + RootCmd.PersistentFlags().StringVar(&mdAddr, "addr", "https://messaging.discovery.skywire.skycoin.net", "address of messaging discovery server") - MessageDiscoveryCmd.AddCommand( - mEntryCmd, - mAvailableServersCmd) + RootCmd.AddCommand( + entryCmd, + availableServersCmd, + ) } -var mEntryCmd = &cobra.Command{ +var entryCmd = &cobra.Command{ Use: "entry ", Short: "fetch entry from messaging-discovery", Args: cobra.MinimumNArgs(1), @@ -44,7 +45,7 @@ var mEntryCmd = &cobra.Command{ }, } -var mAvailableServersCmd = &cobra.Command{ +var availableServersCmd = &cobra.Command{ Use: "available-servers", Short: "fetch available servers from messaging-discovery", Run: func(_ *cobra.Command, _ []string) { diff --git a/cmd/skywire-cli/commands/node/app.go b/cmd/skywire-cli/commands/node/app.go index 41ce2bdd93..e4a7ffb089 100644 --- a/cmd/skywire-cli/commands/node/app.go +++ b/cmd/skywire-cli/commands/node/app.go @@ -14,7 +14,7 @@ import ( ) func init() { - NodeCmd.AddCommand( + RootCmd.AddCommand( appsCmd, startAppCmd, stopAppCmd, @@ -26,7 +26,7 @@ var appsCmd = &cobra.Command{ Use: "apps", Short: "lists apps running on the node", Run: func(_ *cobra.Command, _ []string) { - states, err := internal.RPCClient().Apps() + states, err := rpcClient().Apps() internal.Catch(err) w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) @@ -50,7 +50,7 @@ var startAppCmd = &cobra.Command{ Short: "starts an app of given name", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { - internal.Catch(internal.RPCClient().StartApp(args[0])) + internal.Catch(rpcClient().StartApp(args[0])) fmt.Println("OK") }, } @@ -60,7 +60,7 @@ var stopAppCmd = &cobra.Command{ Short: "stops an app of given name", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { - internal.Catch(internal.RPCClient().StopApp(args[0])) + internal.Catch(rpcClient().StopApp(args[0])) fmt.Println("OK") }, } @@ -79,7 +79,7 @@ var setAppAutostartCmd = &cobra.Command{ default: internal.Catch(fmt.Errorf("invalid args[1] value: %s", args[1])) } - internal.Catch(internal.RPCClient().SetAutoStart(args[0], autostart)) + internal.Catch(rpcClient().SetAutoStart(args[0], autostart)) fmt.Println("OK") }, } diff --git a/cmd/skywire-cli/commands/node/gen-config.go b/cmd/skywire-cli/commands/node/gen-config.go index b906ca92a0..10cc884d12 100644 --- a/cmd/skywire-cli/commands/node/gen-config.go +++ b/cmd/skywire-cli/commands/node/gen-config.go @@ -19,7 +19,7 @@ var ( ) func init() { - NodeCmd.AddCommand(genConfigCmd) + RootCmd.AddCommand(genConfigCmd) genConfigCmd.Flags().StringVarP(&output, "output", "o", "", "path of output config file. Uses default of 'type' flag if unspecified.") genConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, "whether to allow rewrite of a file that already exists.") genConfigCmd.Flags().VarP(&configLocType, "type", "m", fmt.Sprintf("config generation mode. Valid values: %v", pathutil.AllConfigLocationTypes())) diff --git a/cmd/skywire-cli/commands/node/node.go b/cmd/skywire-cli/commands/node/node.go index 182bf8d643..68076c625f 100644 --- a/cmd/skywire-cli/commands/node/node.go +++ b/cmd/skywire-cli/commands/node/node.go @@ -3,12 +3,30 @@ package node import ( "github.com/skycoin/skycoin/src/util/logging" "github.com/spf13/cobra" + + "net/rpc" + + "github.com/skycoin/skywire/pkg/node" ) var log = logging.MustGetLogger("skywire-cli") -// NodeCmd contains commands that interact with the skywire-node -var NodeCmd = &cobra.Command{ +// RootCmd contains commands that interact with the skywire-node +var RootCmd = &cobra.Command{ Use: "node", Short: "Commands that interact with the skywire-node", } + +var rpcAddr string + +func init() { + RootCmd.PersistentFlags().StringVarP(&rpcAddr, "rpc", "", "localhost:3435", "RPC server address") +} + +func rpcClient() node.RPCClient { + client, err := rpc.Dial("tcp", rpcAddr) + if err != nil { + log.Fatal("RPC connection failed:", err) + } + return node.NewRPCClient(client, node.RPCPrefix) +} diff --git a/cmd/skywire-cli/commands/node/pk.go b/cmd/skywire-cli/commands/node/pk.go index 807f608a3b..44c5d62be4 100644 --- a/cmd/skywire-cli/commands/node/pk.go +++ b/cmd/skywire-cli/commands/node/pk.go @@ -3,13 +3,11 @@ package node import ( "fmt" - "github.com/skycoin/skywire/cmd/skywire-cli/internal" - "github.com/spf13/cobra" ) func init() { - NodeCmd.AddCommand(pkCmd) + RootCmd.AddCommand(pkCmd) } var pkCmd = &cobra.Command{ @@ -17,7 +15,7 @@ var pkCmd = &cobra.Command{ Short: "get public key of node", Run: func(_ *cobra.Command, _ []string) { - client := internal.RPCClient() + client := rpcClient() summary, err := client.Summary() if err != nil { log.Fatal("Failed to connect:", err) diff --git a/cmd/skywire-cli/commands/rtfind/routing.go b/cmd/skywire-cli/commands/node/routes.go similarity index 71% rename from cmd/skywire-cli/commands/rtfind/routing.go rename to cmd/skywire-cli/commands/node/routes.go index 919aae941c..4cd4a53993 100644 --- a/cmd/skywire-cli/commands/rtfind/routing.go +++ b/cmd/skywire-cli/commands/node/routes.go @@ -1,4 +1,4 @@ -package rtfind +package node import ( "errors" @@ -13,33 +13,25 @@ import ( "github.com/skycoin/skywire/cmd/skywire-cli/internal" - "github.com/skycoin/skywire/pkg/cipher" "github.com/skycoin/skywire/pkg/node" - "github.com/skycoin/skywire/pkg/route-finder/client" "github.com/skycoin/skywire/pkg/router" "github.com/skycoin/skywire/pkg/routing" ) -// RtFindCmd contains commands that interact with the route finder -var RtFindCmd = &cobra.Command{ - Use: "rtfind", - Short: "Commands that interact with the route-finder", -} - func init() { - RtFindCmd.AddCommand( + RootCmd.AddCommand( listRulesCmd, ruleCmd, rmRuleCmd, addRuleCmd, - findRoutesCmd) + ) } var listRulesCmd = &cobra.Command{ Use: "list-rules", Short: "lists the local node's routing rules", Run: func(_ *cobra.Command, _ []string) { - rules, err := internal.RPCClient().RoutingRules() + rules, err := rpcClient().RoutingRules() internal.Catch(err) printRoutingRules(rules...) @@ -54,7 +46,7 @@ var ruleCmd = &cobra.Command{ id, err := strconv.ParseUint(args[0], 10, 32) internal.Catch(err) - rule, err := internal.RPCClient().RoutingRule(routing.RouteID(id)) + rule, err := rpcClient().RoutingRule(routing.RouteID(id)) internal.Catch(err) printRoutingRules(&node.RoutingEntry{Key: rule.RouteID(), Value: rule}) @@ -68,7 +60,7 @@ var rmRuleCmd = &cobra.Command{ Run: func(_ *cobra.Command, args []string) { id, err := strconv.ParseUint(args[0], 10, 32) internal.Catch(err) - internal.Catch(internal.RPCClient().RemoveRoutingRule(routing.RouteID(id))) + internal.Catch(rpcClient().RemoveRoutingRule(routing.RouteID(id))) fmt.Println("OK") }, } @@ -117,40 +109,12 @@ var addRuleCmd = &cobra.Command{ ) rule = routing.ForwardRule(time.Now().Add(expire), nextRouteID, nextTpID) } - rIDKey, err := internal.RPCClient().AddRoutingRule(rule) + rIDKey, err := rpcClient().AddRoutingRule(rule) internal.Catch(err) fmt.Println("Routing Rule Key:", rIDKey) }, } -var frAddr string -var frMinHops, frMaxHops uint16 - -var findRoutesCmd = &cobra.Command{ - Use: "find-routes ", - Short: "lists available routes between two nodes via route finder service", - Args: cobra.MinimumNArgs(2), - Run: func(_ *cobra.Command, args []string) { - rfc := client.NewHTTP(frAddr) - - var srcPK, dstPK cipher.PubKey - internal.Catch(srcPK.Set(args[0])) - internal.Catch(dstPK.Set(args[1])) - - forward, reverse, err := rfc.PairedRoutes(srcPK, dstPK, frMinHops, frMaxHops) - internal.Catch(err) - - fmt.Println("forward: ", forward) - fmt.Println("reverse: ", reverse) - }, -} - -func init() { - findRoutesCmd.Flags().StringVar(&frAddr, "addr", "https://routefinder.skywire.skycoin.net", "address in which to contact route finder service") - findRoutesCmd.Flags().Uint16Var(&frMinHops, "min-hops", 1, "min hops for the returning routeFinderRoutesCmd") - findRoutesCmd.Flags().Uint16Var(&frMaxHops, "max-hops", 1000, "max hops for the returning routeFinderRoutesCmd") -} - func printRoutingRules(rules ...*node.RoutingEntry) { printAppRule := func(w io.Writer, id routing.RouteID, s *routing.RuleSummary) { _, err := fmt.Fprintf(w, "%d\t%s\t%d\t%d\t%s\t%d\t%s\t%s\t%s\n", id, s.Type, s.AppFields.LocalPort, diff --git a/cmd/skywire-cli/commands/node/transports.go b/cmd/skywire-cli/commands/node/transports.go new file mode 100644 index 0000000000..bbdc24fa79 --- /dev/null +++ b/cmd/skywire-cli/commands/node/transports.go @@ -0,0 +1,125 @@ +package node + +import ( + "fmt" + "os" + "sort" + "text/tabwriter" + "time" + + "github.com/spf13/cobra" + + "github.com/skycoin/skywire/cmd/skywire-cli/internal" + + "github.com/skycoin/skywire/pkg/cipher" + "github.com/skycoin/skywire/pkg/node" +) + +func init() { + RootCmd.AddCommand( + transportTypesCmd, + listTransportsCmd, + transportCmd, + addTransportCmd, + rmTransportCmd, + ) +} + +var transportTypesCmd = &cobra.Command{ + Use: "transport-types", + Short: "lists transport types used by the local node", + Run: func(_ *cobra.Command, _ []string) { + types, err := rpcClient().TransportTypes() + internal.Catch(err) + for _, t := range types { + fmt.Println(t) + } + }, +} + +var ( + filterTypes []string + filterPubKeys cipher.PubKeys + showLogs bool +) + +var listTransportsCmd = &cobra.Command{ + Use: "list-transports", + Short: "lists the available transports with optional filter flags", + Run: func(_ *cobra.Command, _ []string) { + transports, err := rpcClient().Transports(filterTypes, filterPubKeys, showLogs) + internal.Catch(err) + printTransports(transports...) + }, +} + +func init() { + listTransportsCmd.Flags().StringSliceVar(&filterTypes, "filter-types", filterTypes, "comma-separated; if specified, only shows transports of given types") + listTransportsCmd.Flags().Var(&filterPubKeys, "filter-pks", "comma-separated; if specified, only shows transports associated with given nodes") + listTransportsCmd.Flags().BoolVar(&showLogs, "show-logs", true, "whether to show transport logs in output") +} + +var transportCmd = &cobra.Command{ + Use: "transport ", + Short: "returns summary of given transport by id", + Args: cobra.MinimumNArgs(1), + Run: func(_ *cobra.Command, args []string) { + tpID := internal.ParseUUID("transport-id", args[0]) + tp, err := rpcClient().Transport(tpID) + internal.Catch(err) + printTransports(tp) + }, +} + +var ( + transportType string + public bool + timeout time.Duration +) + +var addTransportCmd = &cobra.Command{ + Use: "add-transport ", + Short: "adds a new transport", + Args: cobra.MinimumNArgs(1), + Run: func(_ *cobra.Command, args []string) { + pk := internal.ParsePK("remote-public-key", args[0]) + tp, err := rpcClient().AddTransport(pk, transportType, public, timeout) + internal.Catch(err) + printTransports(tp) + }, +} + +func init() { + addTransportCmd.Flags().StringVar(&transportType, "type", "messaging", "type of transport to add") + addTransportCmd.Flags().BoolVar(&public, "public", true, "whether to make the transport public") + addTransportCmd.Flags().DurationVarP(&timeout, "timeout", "t", 0, "if specified, sets an operation timeout") +} + +var rmTransportCmd = &cobra.Command{ + Use: "rm-transport ", + Short: "removes transport with given id", + Args: cobra.MinimumNArgs(1), + Run: func(_ *cobra.Command, args []string) { + tID := internal.ParseUUID("transport-id", args[0]) + internal.Catch(rpcClient().RemoveTransport(tID)) + fmt.Println("OK") + }, +} + +func printTransports(tps ...*node.TransportSummary) { + sortTransports(tps...) + w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) + _, err := fmt.Fprintln(w, "type\tid\tlocal\tremote") + internal.Catch(err) + for _, tp := range tps { + _, err = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", tp.Type, tp.ID, tp.Local, tp.Remote) + internal.Catch(err) + } + internal.Catch(w.Flush()) +} + +func sortTransports(tps ...*node.TransportSummary) { + sort.Slice(tps, func(i, j int) bool { + return tps[i].ID.String() < tps[j].ID.String() + }) +} diff --git a/cmd/skywire-cli/commands/root.go b/cmd/skywire-cli/commands/root.go index abd186a3a8..03b187e6d3 100644 --- a/cmd/skywire-cli/commands/root.go +++ b/cmd/skywire-cli/commands/root.go @@ -8,7 +8,7 @@ import ( "github.com/spf13/cobra" ) -var rpcAddr string +var RPCAddr string var rootCmd = &cobra.Command{ Use: "skywire-cli", @@ -17,15 +17,14 @@ var rootCmd = &cobra.Command{ func init() { rootCmd.AddCommand( - node.NodeCmd, - mdisc.MessageDiscoveryCmd, - rtfind.RtFindCmd, - tpdisc.TransportCmd, + node.RootCmd, + mdisc.RootCmd, + rtfind.RootCmd, + tpdisc.RootCmd, ) } // Execute executes root CLI command. func Execute() { - rootCmd.PersistentFlags().StringVarP(&rpcAddr, "rpc", "", "localhost:3435", "RPC server address") rootCmd.Execute() //nolint:errcheck } diff --git a/cmd/skywire-cli/commands/rtfind/rtfind.go b/cmd/skywire-cli/commands/rtfind/rtfind.go new file mode 100644 index 0000000000..8861ff9ef8 --- /dev/null +++ b/cmd/skywire-cli/commands/rtfind/rtfind.go @@ -0,0 +1,52 @@ +package rtfind + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/skycoin/skywire/cmd/skywire-cli/internal" + + "github.com/skycoin/skywire/pkg/cipher" + "github.com/skycoin/skywire/pkg/route-finder/client" +) + +// RootCmd contains commands that interact with the route finder +var RootCmd = &cobra.Command{ + Use: "rtfind", + Short: "Commands that interact with the route-finder", +} + +func init() { + RootCmd.AddCommand( + findRoutesCmd, + ) +} + +var frAddr string +var frMinHops, frMaxHops uint16 + +var findRoutesCmd = &cobra.Command{ + Use: "find-routes ", + Short: "lists available routes between two nodes via route finder service", + Args: cobra.MinimumNArgs(2), + Run: func(_ *cobra.Command, args []string) { + rfc := client.NewHTTP(frAddr) + + var srcPK, dstPK cipher.PubKey + internal.Catch(srcPK.Set(args[0])) + internal.Catch(dstPK.Set(args[1])) + + forward, reverse, err := rfc.PairedRoutes(srcPK, dstPK, frMinHops, frMaxHops) + internal.Catch(err) + + fmt.Println("forward: ", forward) + fmt.Println("reverse: ", reverse) + }, +} + +func init() { + findRoutesCmd.Flags().StringVar(&frAddr, "addr", "https://routefinder.skywire.skycoin.net", "address in which to contact route finder service") + findRoutesCmd.Flags().Uint16Var(&frMinHops, "min-hops", 1, "min hops for the returning routeFinderRoutesCmd") + findRoutesCmd.Flags().Uint16Var(&frMaxHops, "max-hops", 1000, "max hops for the returning routeFinderRoutesCmd") +} diff --git a/cmd/skywire-cli/commands/tpdisc/tpdisc.go b/cmd/skywire-cli/commands/tpdisc/tpdisc.go index 2062d3ae21..266c5f3363 100644 --- a/cmd/skywire-cli/commands/tpdisc/tpdisc.go +++ b/cmd/skywire-cli/commands/tpdisc/tpdisc.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "os" - "sort" "text/tabwriter" "time" @@ -15,109 +14,22 @@ import ( "github.com/skycoin/skywire/cmd/skywire-cli/internal" "github.com/skycoin/skywire/pkg/cipher" - "github.com/skycoin/skywire/pkg/node" "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/transport-discovery/client" ) -// TransportCmd contains commands that interact with transport-discovery -var TransportCmd = &cobra.Command{ +// RootCmd contains commands that interact with transport-discovery +var RootCmd = &cobra.Command{ Use: "tpdisc", Short: "Commands that interact with transport-discovery", } func init() { - TransportCmd.AddCommand( - transportTypesCmd, - listTransportsCmd, - transportCmd, - addTransportCmd, - rmTransportCmd, + RootCmd.AddCommand( findTransport, ) } -var transportTypesCmd = &cobra.Command{ - Use: "transport-types", - Short: "lists transport types used by the local node", - Run: func(_ *cobra.Command, _ []string) { - types, err := internal.RPCClient().TransportTypes() - internal.Catch(err) - for _, t := range types { - fmt.Println(t) - } - }, -} - -var ( - filterTypes []string - filterPubKeys cipher.PubKeys - showLogs bool -) - -var listTransportsCmd = &cobra.Command{ - Use: "list-transports", - Short: "lists the available transports with optional filter flags", - Run: func(_ *cobra.Command, _ []string) { - transports, err := internal.RPCClient().Transports(filterTypes, filterPubKeys, showLogs) - internal.Catch(err) - printTransports(transports...) - }, -} - -func init() { - listTransportsCmd.Flags().StringSliceVar(&filterTypes, "filter-types", filterTypes, "comma-separated; if specified, only shows transports of given types") - listTransportsCmd.Flags().Var(&filterPubKeys, "filter-pks", "comma-separated; if specified, only shows transports associated with given nodes") - listTransportsCmd.Flags().BoolVar(&showLogs, "show-logs", true, "whether to show transport logs in output") -} - -var transportCmd = &cobra.Command{ - Use: "transport ", - Short: "returns summary of given transport by id", - Args: cobra.MinimumNArgs(1), - Run: func(_ *cobra.Command, args []string) { - tpID := internal.ParseUUID("transport-id", args[0]) - tp, err := internal.RPCClient().Transport(tpID) - internal.Catch(err) - printTransports(tp) - }, -} - -var ( - transportType string - public bool - timeout time.Duration -) - -var addTransportCmd = &cobra.Command{ - Use: "add-transport ", - Short: "adds a new transport", - Args: cobra.MinimumNArgs(1), - Run: func(_ *cobra.Command, args []string) { - pk := internal.ParsePK("remote-public-key", args[0]) - tp, err := internal.RPCClient().AddTransport(pk, transportType, public, timeout) - internal.Catch(err) - printTransports(tp) - }, -} - -func init() { - addTransportCmd.Flags().StringVar(&transportType, "type", "messaging", "type of transport to add") - addTransportCmd.Flags().BoolVar(&public, "public", true, "whether to make the transport public") - addTransportCmd.Flags().DurationVarP(&timeout, "timeout", "t", 0, "if specified, sets an operation timeout") -} - -var rmTransportCmd = &cobra.Command{ - Use: "rm-transport ", - Short: "removes transport with given id", - Args: cobra.MinimumNArgs(1), - Run: func(_ *cobra.Command, args []string) { - tID := internal.ParseUUID("transport-id", args[0]) - internal.Catch(internal.RPCClient().RemoveTransport(tID)) - fmt.Println("OK") - }, -} - var ( addr string tpID transportID @@ -164,18 +76,6 @@ func init() { findTransport.Flags().Var(&tpPK, "pk", "if specified, obtains transports associated with given public key") } -func printTransports(tps ...*node.TransportSummary) { - sortTransports(tps...) - w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) - _, err := fmt.Fprintln(w, "type\tid\tlocal\tremote") - internal.Catch(err) - for _, tp := range tps { - _, err = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", tp.Type, tp.ID, tp.Local, tp.Remote) - internal.Catch(err) - } - internal.Catch(w.Flush()) -} - func printTransportEntries(entries ...*transport.EntryWithStatus) { w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) _, err := fmt.Fprintln(w, "id\ttype\tpublic\tregistered\tup\tedge1\tedge2\topinion1\topinion2") @@ -187,9 +87,3 @@ func printTransportEntries(entries ...*transport.EntryWithStatus) { } internal.Catch(w.Flush()) } - -func sortTransports(tps ...*node.TransportSummary) { - sort.Slice(tps, func(i, j int) bool { - return tps[i].ID.String() < tps[j].ID.String() - }) -} diff --git a/cmd/skywire-cli/internal/internal.go b/cmd/skywire-cli/internal/internal.go index 9a38d76370..3bbd3df576 100644 --- a/cmd/skywire-cli/internal/internal.go +++ b/cmd/skywire-cli/internal/internal.go @@ -2,29 +2,16 @@ package internal import ( "fmt" - "net/rpc" "github.com/google/uuid" "github.com/skycoin/skywire/pkg/cipher" "github.com/skycoin/skycoin/src/util/logging" - "github.com/skycoin/skywire/pkg/node" ) var log = logging.MustGetLogger("skywire-cli") -var rpcAddr string - -// RPCClient connects to the nodes rpc methods -func RPCClient() node.RPCClient { - client, err := rpc.Dial("tcp", rpcAddr) - if err != nil { - log.Fatal("RPC connection failed:", err) - } - return node.NewRPCClient(client, node.RPCPrefix) -} - // Catch handles errors for skywire-cli commands packages func Catch(err error, msgs ...string) { if err != nil { From 4e0f146c60d38cca9f9337f45666c6ffa424070e Mon Sep 17 00:00:00 2001 From: Nickson Date: Thu, 4 Apr 2019 13:15:34 +0300 Subject: [PATCH 06/11] removed unused string --- cmd/skywire-cli/README.org | 8 -------- cmd/skywire-cli/commands/root.go | 2 -- 2 files changed, 10 deletions(-) delete mode 100644 cmd/skywire-cli/README.org diff --git a/cmd/skywire-cli/README.org b/cmd/skywire-cli/README.org deleted file mode 100644 index 2ff98adfed..0000000000 --- a/cmd/skywire-cli/README.org +++ /dev/null @@ -1,8 +0,0 @@ -* CLI for skywire node - -~skywire-cli~ implements management commands for ~skywire~ node via -RPC interface. - -** Available commands - -TODO: add commands diff --git a/cmd/skywire-cli/commands/root.go b/cmd/skywire-cli/commands/root.go index 03b187e6d3..3e27939cd0 100644 --- a/cmd/skywire-cli/commands/root.go +++ b/cmd/skywire-cli/commands/root.go @@ -8,8 +8,6 @@ import ( "github.com/spf13/cobra" ) -var RPCAddr string - var rootCmd = &cobra.Command{ Use: "skywire-cli", Short: "Command Line Interface for skywire", From d4c3b696ddb3302efe6030ccaa086acb5bd5b40f Mon Sep 17 00:00:00 2001 From: Nickson Date: Thu, 4 Apr 2019 13:17:37 +0300 Subject: [PATCH 07/11] changed documentation file type --- cmd/skywire-cli/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 cmd/skywire-cli/README.md diff --git a/cmd/skywire-cli/README.md b/cmd/skywire-cli/README.md new file mode 100644 index 0000000000..aa7d1cbb4f --- /dev/null +++ b/cmd/skywire-cli/README.md @@ -0,0 +1,12 @@ +# CLI for skywire node + +skywire-cli implements management commands for skywire node via +RPC interface. + +## Available commands + +- node + |Sub command|Details| + |-----------|-------| + |add-rule |adds a new routing rule| + | \ No newline at end of file From 7bd2023be8545e071964c544846fd5e7475f5aa9 Mon Sep 17 00:00:00 2001 From: ivcosla Date: Thu, 4 Apr 2019 12:41:42 +0200 Subject: [PATCH 08/11] fixed tests --- internal/ioutil/ack.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/ioutil/ack.go b/internal/ioutil/ack.go index ef737796d8..05f049f73b 100644 --- a/internal/ioutil/ack.go +++ b/internal/ioutil/ack.go @@ -192,6 +192,7 @@ func (arw *AckReadWriter) confirm(seq byte, hash []byte) { if ack.hash != rcvHash { ack.errChan <- errors.New("invalid CRC") + return } ack.errChan <- nil From 7840e3d40392b457e750379d8568887d115d28a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E5=BF=97=E5=AE=87?= Date: Fri, 5 Apr 2019 13:52:52 +0800 Subject: [PATCH 09/11] tweaks to skywire-cli --- cmd/skywire-cli/README.md | 12 ---- .../commands/mdisc/{mdisc.go => root.go} | 17 ++--- cmd/skywire-cli/commands/node/app.go | 15 ++--- cmd/skywire-cli/commands/node/gen-config.go | 9 +-- cmd/skywire-cli/commands/node/pk.go | 2 +- .../commands/node/{node.go => root.go} | 16 ++--- cmd/skywire-cli/commands/node/routes.go | 19 +++--- cmd/skywire-cli/commands/node/transports.go | 65 +++++++++---------- cmd/skywire-cli/commands/root.go | 5 +- cmd/skywire-cli/commands/rtfind/root.go | 40 ++++++++++++ cmd/skywire-cli/commands/rtfind/rtfind.go | 52 --------------- .../commands/tpdisc/{tpdisc.go => root.go} | 50 +++++++------- cmd/skywire-cli/commands/tpdisc/transport.go | 23 ------- cmd/skywire-cli/internal/internal.go | 3 +- 14 files changed, 143 insertions(+), 185 deletions(-) delete mode 100644 cmd/skywire-cli/README.md rename cmd/skywire-cli/commands/mdisc/{mdisc.go => root.go} (88%) rename cmd/skywire-cli/commands/node/{node.go => root.go} (88%) create mode 100644 cmd/skywire-cli/commands/rtfind/root.go delete mode 100644 cmd/skywire-cli/commands/rtfind/rtfind.go rename cmd/skywire-cli/commands/tpdisc/{tpdisc.go => root.go} (65%) delete mode 100644 cmd/skywire-cli/commands/tpdisc/transport.go diff --git a/cmd/skywire-cli/README.md b/cmd/skywire-cli/README.md deleted file mode 100644 index aa7d1cbb4f..0000000000 --- a/cmd/skywire-cli/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# CLI for skywire node - -skywire-cli implements management commands for skywire node via -RPC interface. - -## Available commands - -- node - |Sub command|Details| - |-----------|-------| - |add-rule |adds a new routing rule| - | \ No newline at end of file diff --git a/cmd/skywire-cli/commands/mdisc/mdisc.go b/cmd/skywire-cli/commands/mdisc/root.go similarity index 88% rename from cmd/skywire-cli/commands/mdisc/mdisc.go rename to cmd/skywire-cli/commands/mdisc/root.go index 5e62733ee4..876e8098e9 100644 --- a/cmd/skywire-cli/commands/mdisc/mdisc.go +++ b/cmd/skywire-cli/commands/mdisc/root.go @@ -10,21 +10,22 @@ import ( "github.com/spf13/cobra" "github.com/skycoin/skywire/cmd/skywire-cli/internal" - "github.com/skycoin/skywire/pkg/messaging-discovery/client" ) -// RootCmd contains commands that interact with messaging services -var RootCmd = &cobra.Command{ - Use: "mdisc", - Short: "Commands that interact with messaging-discovery", -} - var mdAddr string func init() { RootCmd.PersistentFlags().StringVar(&mdAddr, "addr", "https://messaging.discovery.skywire.skycoin.net", "address of messaging discovery server") +} +// RootCmd is the command that contains sub-commands which interacts with messaging services. +var RootCmd = &cobra.Command{ + Use: "mdisc", + Short: "Contains sub-commands that interact with a remote Messaging Discovery", +} + +func init() { RootCmd.AddCommand( entryCmd, availableServersCmd, @@ -33,7 +34,7 @@ func init() { var entryCmd = &cobra.Command{ Use: "entry ", - Short: "fetch entry from messaging-discovery", + Short: "fetches an entry from messaging-discovery", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) diff --git a/cmd/skywire-cli/commands/node/app.go b/cmd/skywire-cli/commands/node/app.go index e4a7ffb089..23d3092341 100644 --- a/cmd/skywire-cli/commands/node/app.go +++ b/cmd/skywire-cli/commands/node/app.go @@ -9,22 +9,21 @@ import ( "github.com/spf13/cobra" "github.com/skycoin/skywire/cmd/skywire-cli/internal" - "github.com/skycoin/skywire/pkg/node" ) func init() { RootCmd.AddCommand( - appsCmd, + lsAppsCmd, startAppCmd, stopAppCmd, setAppAutostartCmd, ) } -var appsCmd = &cobra.Command{ - Use: "apps", - Short: "lists apps running on the node", +var lsAppsCmd = &cobra.Command{ + Use: "ls-apps", + Short: "Lists apps running on the local node", Run: func(_ *cobra.Command, _ []string) { states, err := rpcClient().Apps() internal.Catch(err) @@ -47,7 +46,7 @@ var appsCmd = &cobra.Command{ var startAppCmd = &cobra.Command{ Use: "start-app ", - Short: "starts an app of given name", + Short: "Starts an app of given name", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { internal.Catch(rpcClient().StartApp(args[0])) @@ -57,7 +56,7 @@ var startAppCmd = &cobra.Command{ var stopAppCmd = &cobra.Command{ Use: "stop-app ", - Short: "stops an app of given name", + Short: "Stops an app of given name", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { internal.Catch(rpcClient().StopApp(args[0])) @@ -67,7 +66,7 @@ var stopAppCmd = &cobra.Command{ var setAppAutostartCmd = &cobra.Command{ Use: "set-app-autostart (on|off)", - Short: "sets the autostart flag for an app of given name", + Short: "Sets the autostart flag for an app of given name", Args: cobra.MinimumNArgs(2), Run: func(_ *cobra.Command, args []string) { var autostart bool diff --git a/cmd/skywire-cli/commands/node/gen-config.go b/cmd/skywire-cli/commands/node/gen-config.go index 10cc884d12..220b572574 100644 --- a/cmd/skywire-cli/commands/node/gen-config.go +++ b/cmd/skywire-cli/commands/node/gen-config.go @@ -12,6 +12,10 @@ import ( "github.com/skycoin/skywire/pkg/node" ) +func init() { + RootCmd.AddCommand(genConfigCmd) +} + var ( output string replace bool @@ -19,7 +23,6 @@ var ( ) func init() { - RootCmd.AddCommand(genConfigCmd) genConfigCmd.Flags().StringVarP(&output, "output", "o", "", "path of output config file. Uses default of 'type' flag if unspecified.") genConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, "whether to allow rewrite of a file that already exists.") genConfigCmd.Flags().VarP(&configLocType, "type", "m", fmt.Sprintf("config generation mode. Valid values: %v", pathutil.AllConfigLocationTypes())) @@ -27,7 +30,7 @@ func init() { var genConfigCmd = &cobra.Command{ Use: "gen-config", - Short: "Generate default config file", + Short: "Generates a config file", PreRun: func(_ *cobra.Command, _ []string) { if output == "" { output = pathutil.NodeDefaults().Get(configLocType) @@ -59,7 +62,6 @@ func homeConfig() *node.Config { c.AppsPath = filepath.Join(pathutil.HomeDir(), ".skycoin/skywire/apps") c.Transport.LogStore.Location = filepath.Join(pathutil.HomeDir(), ".skycoin/skywire/transport_logs") c.Routing.Table.Location = filepath.Join(pathutil.HomeDir(), ".skycoin/skywire/routing.db") - return c } @@ -68,7 +70,6 @@ func localConfig() *node.Config { c.AppsPath = "/usr/local/skycoin/skywire/apps" c.Transport.LogStore.Location = "/usr/local/skycoin/skywire/transport_logs" c.Routing.Table.Location = "/usr/local/skycoin/skywire/routing.db" - return c } diff --git a/cmd/skywire-cli/commands/node/pk.go b/cmd/skywire-cli/commands/node/pk.go index 44c5d62be4..f87379b9e1 100644 --- a/cmd/skywire-cli/commands/node/pk.go +++ b/cmd/skywire-cli/commands/node/pk.go @@ -12,7 +12,7 @@ func init() { var pkCmd = &cobra.Command{ Use: "pk", - Short: "get public key of node", + Short: "Obtains the public key of the node", Run: func(_ *cobra.Command, _ []string) { client := rpcClient() diff --git a/cmd/skywire-cli/commands/node/node.go b/cmd/skywire-cli/commands/node/root.go similarity index 88% rename from cmd/skywire-cli/commands/node/node.go rename to cmd/skywire-cli/commands/node/root.go index 68076c625f..f6fbf18bf8 100644 --- a/cmd/skywire-cli/commands/node/node.go +++ b/cmd/skywire-cli/commands/node/root.go @@ -1,28 +1,28 @@ package node import ( + "net/rpc" + "github.com/skycoin/skycoin/src/util/logging" "github.com/spf13/cobra" - "net/rpc" - "github.com/skycoin/skywire/pkg/node" ) var log = logging.MustGetLogger("skywire-cli") -// RootCmd contains commands that interact with the skywire-node -var RootCmd = &cobra.Command{ - Use: "node", - Short: "Commands that interact with the skywire-node", -} - var rpcAddr string func init() { RootCmd.PersistentFlags().StringVarP(&rpcAddr, "rpc", "", "localhost:3435", "RPC server address") } +// RootCmd contains commands that interact with the skywire-node +var RootCmd = &cobra.Command{ + Use: "node", + Short: "Contains sub-commands that interact with the local Skywire (App) Node", +} + func rpcClient() node.RPCClient { client, err := rpc.Dial("tcp", rpcAddr) if err != nil { diff --git a/cmd/skywire-cli/commands/node/routes.go b/cmd/skywire-cli/commands/node/routes.go index 4cd4a53993..b80c3ab49c 100644 --- a/cmd/skywire-cli/commands/node/routes.go +++ b/cmd/skywire-cli/commands/node/routes.go @@ -12,7 +12,6 @@ import ( "github.com/spf13/cobra" "github.com/skycoin/skywire/cmd/skywire-cli/internal" - "github.com/skycoin/skywire/pkg/node" "github.com/skycoin/skywire/pkg/router" "github.com/skycoin/skywire/pkg/routing" @@ -20,16 +19,16 @@ import ( func init() { RootCmd.AddCommand( - listRulesCmd, + lsRulesCmd, ruleCmd, rmRuleCmd, addRuleCmd, ) } -var listRulesCmd = &cobra.Command{ - Use: "list-rules", - Short: "lists the local node's routing rules", +var lsRulesCmd = &cobra.Command{ + Use: "ls-rules", + Short: "Lists the local node's routing rules", Run: func(_ *cobra.Command, _ []string) { rules, err := rpcClient().RoutingRules() internal.Catch(err) @@ -40,7 +39,7 @@ var listRulesCmd = &cobra.Command{ var ruleCmd = &cobra.Command{ Use: "rule ", - Short: "returns a routing rule via route ID key", + Short: "Returns a routing rule via route ID key", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { id, err := strconv.ParseUint(args[0], 10, 32) @@ -55,7 +54,7 @@ var ruleCmd = &cobra.Command{ var rmRuleCmd = &cobra.Command{ Use: "rm-rule ", - Short: "removes a routing rule via route ID key", + Short: "Removes a routing rule via route ID key", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { id, err := strconv.ParseUint(args[0], 10, 32) @@ -65,15 +64,15 @@ var rmRuleCmd = &cobra.Command{ }, } +var expire time.Duration + func init() { addRuleCmd.PersistentFlags().DurationVar(&expire, "expire", router.RouteTTL, "duration after which routing rule will expire") } -var expire time.Duration - var addRuleCmd = &cobra.Command{ Use: "add-rule (app | fwd )", - Short: "adds a new routing rule", + Short: "Adds a new routing rule", Args: func(_ *cobra.Command, args []string) error { if len(args) > 0 { switch rt := args[0]; rt { diff --git a/cmd/skywire-cli/commands/node/transports.go b/cmd/skywire-cli/commands/node/transports.go index bbdc24fa79..4fbc0cf412 100644 --- a/cmd/skywire-cli/commands/node/transports.go +++ b/cmd/skywire-cli/commands/node/transports.go @@ -10,24 +10,23 @@ import ( "github.com/spf13/cobra" "github.com/skycoin/skywire/cmd/skywire-cli/internal" - "github.com/skycoin/skywire/pkg/cipher" "github.com/skycoin/skywire/pkg/node" ) func init() { RootCmd.AddCommand( - transportTypesCmd, - listTransportsCmd, - transportCmd, - addTransportCmd, - rmTransportCmd, + lsTypesCmd, + lsTpCmd, + tpCmd, + addTpCmd, + rmTpCmd, ) } -var transportTypesCmd = &cobra.Command{ - Use: "transport-types", - Short: "lists transport types used by the local node", +var lsTypesCmd = &cobra.Command{ + Use: "ls-types", + Short: "Lists transport types used by the local node", Run: func(_ *cobra.Command, _ []string) { types, err := rpcClient().TransportTypes() internal.Catch(err) @@ -43,9 +42,15 @@ var ( showLogs bool ) -var listTransportsCmd = &cobra.Command{ - Use: "list-transports", - Short: "lists the available transports with optional filter flags", +func init() { + lsTpCmd.Flags().StringSliceVar(&filterTypes, "filter-types", filterTypes, "comma-separated; if specified, only shows transports of given types") + lsTpCmd.Flags().Var(&filterPubKeys, "filter-pks", "comma-separated; if specified, only shows transports associated with given nodes") + lsTpCmd.Flags().BoolVar(&showLogs, "show-logs", true, "whether to show transport logs in output") +} + +var lsTpCmd = &cobra.Command{ + Use: "ls-tp", + Short: "Lists the available transports with optional filter flags", Run: func(_ *cobra.Command, _ []string) { transports, err := rpcClient().Transports(filterTypes, filterPubKeys, showLogs) internal.Catch(err) @@ -53,15 +58,9 @@ var listTransportsCmd = &cobra.Command{ }, } -func init() { - listTransportsCmd.Flags().StringSliceVar(&filterTypes, "filter-types", filterTypes, "comma-separated; if specified, only shows transports of given types") - listTransportsCmd.Flags().Var(&filterPubKeys, "filter-pks", "comma-separated; if specified, only shows transports associated with given nodes") - listTransportsCmd.Flags().BoolVar(&showLogs, "show-logs", true, "whether to show transport logs in output") -} - -var transportCmd = &cobra.Command{ - Use: "transport ", - Short: "returns summary of given transport by id", +var tpCmd = &cobra.Command{ + Use: "tp ", + Short: "Returns summary of given transport by id", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { tpID := internal.ParseUUID("transport-id", args[0]) @@ -77,9 +76,15 @@ var ( timeout time.Duration ) -var addTransportCmd = &cobra.Command{ - Use: "add-transport ", - Short: "adds a new transport", +func init() { + addTpCmd.Flags().StringVar(&transportType, "type", "messaging", "type of transport to add") + addTpCmd.Flags().BoolVar(&public, "public", true, "whether to make the transport public") + addTpCmd.Flags().DurationVarP(&timeout, "timeout", "t", 0, "if specified, sets an operation timeout") +} + +var addTpCmd = &cobra.Command{ + Use: "add-tp ", + Short: "Adds a new transport", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { pk := internal.ParsePK("remote-public-key", args[0]) @@ -89,15 +94,9 @@ var addTransportCmd = &cobra.Command{ }, } -func init() { - addTransportCmd.Flags().StringVar(&transportType, "type", "messaging", "type of transport to add") - addTransportCmd.Flags().BoolVar(&public, "public", true, "whether to make the transport public") - addTransportCmd.Flags().DurationVarP(&timeout, "timeout", "t", 0, "if specified, sets an operation timeout") -} - -var rmTransportCmd = &cobra.Command{ - Use: "rm-transport ", - Short: "removes transport with given id", +var rmTpCmd = &cobra.Command{ + Use: "rm-tp ", + Short: "Removes transport with given id", Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { tID := internal.ParseUUID("transport-id", args[0]) diff --git a/cmd/skywire-cli/commands/root.go b/cmd/skywire-cli/commands/root.go index 3e27939cd0..c08a222bc2 100644 --- a/cmd/skywire-cli/commands/root.go +++ b/cmd/skywire-cli/commands/root.go @@ -1,11 +1,12 @@ package commands import ( + "github.com/spf13/cobra" + "github.com/skycoin/skywire/cmd/skywire-cli/commands/mdisc" "github.com/skycoin/skywire/cmd/skywire-cli/commands/node" "github.com/skycoin/skywire/cmd/skywire-cli/commands/rtfind" "github.com/skycoin/skywire/cmd/skywire-cli/commands/tpdisc" - "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ @@ -24,5 +25,5 @@ func init() { // Execute executes root CLI command. func Execute() { - rootCmd.Execute() //nolint:errcheck + _ = rootCmd.Execute() //nolint:errcheck } diff --git a/cmd/skywire-cli/commands/rtfind/root.go b/cmd/skywire-cli/commands/rtfind/root.go new file mode 100644 index 0000000000..fcb0bfeda8 --- /dev/null +++ b/cmd/skywire-cli/commands/rtfind/root.go @@ -0,0 +1,40 @@ +package rtfind + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/skycoin/skywire/cmd/skywire-cli/internal" + "github.com/skycoin/skywire/pkg/cipher" + "github.com/skycoin/skywire/pkg/route-finder/client" +) + +var frAddr string +var frMinHops, frMaxHops uint16 + +func init() { + RootCmd.Flags().StringVar(&frAddr, "addr", "https://routefinder.skywire.skycoin.net", "address in which to contact route finder service") + RootCmd.Flags().Uint16Var(&frMinHops, "min-hops", 1, "min hops for the returning routeFinderRoutesCmd") + RootCmd.Flags().Uint16Var(&frMaxHops, "max-hops", 1000, "max hops for the returning routeFinderRoutesCmd") +} + +// RootCmd is the command that queries the route-finder. +var RootCmd = &cobra.Command{ + Use: "rtfind ", + Short: "Queries the Route Finder for available routes between two nodes", + Args: cobra.MinimumNArgs(2), + Run: func(_ *cobra.Command, args []string) { + rfc := client.NewHTTP(frAddr) + + var srcPK, dstPK cipher.PubKey + internal.Catch(srcPK.Set(args[0])) + internal.Catch(dstPK.Set(args[1])) + + forward, reverse, err := rfc.PairedRoutes(srcPK, dstPK, frMinHops, frMaxHops) + internal.Catch(err) + + fmt.Println("forward: ", forward) + fmt.Println("reverse: ", reverse) + }, +} diff --git a/cmd/skywire-cli/commands/rtfind/rtfind.go b/cmd/skywire-cli/commands/rtfind/rtfind.go deleted file mode 100644 index 8861ff9ef8..0000000000 --- a/cmd/skywire-cli/commands/rtfind/rtfind.go +++ /dev/null @@ -1,52 +0,0 @@ -package rtfind - -import ( - "fmt" - - "github.com/spf13/cobra" - - "github.com/skycoin/skywire/cmd/skywire-cli/internal" - - "github.com/skycoin/skywire/pkg/cipher" - "github.com/skycoin/skywire/pkg/route-finder/client" -) - -// RootCmd contains commands that interact with the route finder -var RootCmd = &cobra.Command{ - Use: "rtfind", - Short: "Commands that interact with the route-finder", -} - -func init() { - RootCmd.AddCommand( - findRoutesCmd, - ) -} - -var frAddr string -var frMinHops, frMaxHops uint16 - -var findRoutesCmd = &cobra.Command{ - Use: "find-routes ", - Short: "lists available routes between two nodes via route finder service", - Args: cobra.MinimumNArgs(2), - Run: func(_ *cobra.Command, args []string) { - rfc := client.NewHTTP(frAddr) - - var srcPK, dstPK cipher.PubKey - internal.Catch(srcPK.Set(args[0])) - internal.Catch(dstPK.Set(args[1])) - - forward, reverse, err := rfc.PairedRoutes(srcPK, dstPK, frMinHops, frMaxHops) - internal.Catch(err) - - fmt.Println("forward: ", forward) - fmt.Println("reverse: ", reverse) - }, -} - -func init() { - findRoutesCmd.Flags().StringVar(&frAddr, "addr", "https://routefinder.skywire.skycoin.net", "address in which to contact route finder service") - findRoutesCmd.Flags().Uint16Var(&frMinHops, "min-hops", 1, "min hops for the returning routeFinderRoutesCmd") - findRoutesCmd.Flags().Uint16Var(&frMaxHops, "max-hops", 1000, "max hops for the returning routeFinderRoutesCmd") -} diff --git a/cmd/skywire-cli/commands/tpdisc/tpdisc.go b/cmd/skywire-cli/commands/tpdisc/root.go similarity index 65% rename from cmd/skywire-cli/commands/tpdisc/tpdisc.go rename to cmd/skywire-cli/commands/tpdisc/root.go index 266c5f3363..e0fb6399df 100644 --- a/cmd/skywire-cli/commands/tpdisc/tpdisc.go +++ b/cmd/skywire-cli/commands/tpdisc/root.go @@ -12,33 +12,27 @@ import ( "github.com/spf13/cobra" "github.com/skycoin/skywire/cmd/skywire-cli/internal" - "github.com/skycoin/skywire/pkg/cipher" "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/transport-discovery/client" ) -// RootCmd contains commands that interact with transport-discovery -var RootCmd = &cobra.Command{ - Use: "tpdisc", - Short: "Commands that interact with transport-discovery", -} - -func init() { - RootCmd.AddCommand( - findTransport, - ) -} - var ( addr string tpID transportID tpPK cipher.PubKey ) -var findTransport = &cobra.Command{ - Use: "find-transport (--id= | --pk=)", - Short: "finds and lists transport(s) of given transport ID or edge public key from transport discovery", +func init() { + RootCmd.Flags().StringVar(&addr, "addr", "https://transport.discovery.skywire.skycoin.net", "address of transport discovery") + RootCmd.Flags().Var(&tpID, "id", "if specified, obtains a single transport of given ID") + RootCmd.Flags().Var(&tpPK, "pk", "if specified, obtains transports associated with given public key") +} + +// RootCmd is the command that queries the transport-discovery. +var RootCmd = &cobra.Command{ + Use: "tpdisc (--id= | --pk=)", + Short: "Queries the Transport Discovery to find transport(s) of given transport ID or edge public key", Args: func(_ *cobra.Command, _ []string) error { var ( nilID = uuid.UUID(tpID) == (uuid.UUID{}) @@ -70,12 +64,6 @@ var findTransport = &cobra.Command{ }, } -func init() { - findTransport.Flags().StringVar(&addr, "addr", "https://transport.discovery.skywire.skycoin.net", "address of transport discovery") - findTransport.Flags().Var(&tpID, "id", "if specified, obtains a single transport of given ID") - findTransport.Flags().Var(&tpPK, "pk", "if specified, obtains transports associated with given public key") -} - func printTransportEntries(entries ...*transport.EntryWithStatus) { w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', tabwriter.TabIndent) _, err := fmt.Fprintln(w, "id\ttype\tpublic\tregistered\tup\tedge1\tedge2\topinion1\topinion2") @@ -87,3 +75,21 @@ func printTransportEntries(entries ...*transport.EntryWithStatus) { } internal.Catch(w.Flush()) } + +type transportID uuid.UUID + +// String implements pflag.Value +func (t transportID) String() string { return uuid.UUID(t).String() } + +// Type implements pflag.Value +func (transportID) Type() string { return "transportID" } + +// Set implements pflag.Value +func (t *transportID) Set(s string) error { + tID, err := uuid.Parse(s) + if err != nil { + return err + } + *t = transportID(tID) + return nil +} diff --git a/cmd/skywire-cli/commands/tpdisc/transport.go b/cmd/skywire-cli/commands/tpdisc/transport.go deleted file mode 100644 index e1570afb81..0000000000 --- a/cmd/skywire-cli/commands/tpdisc/transport.go +++ /dev/null @@ -1,23 +0,0 @@ -package tpdisc - -import ( - "github.com/google/uuid" -) - -type transportID uuid.UUID - -// String implements pflag.Value -func (t transportID) String() string { return uuid.UUID(t).String() } - -// Type implements pflag.Value -func (transportID) Type() string { return "transportID" } - -// Set implements pflag.Value -func (t *transportID) Set(s string) error { - tID, err := uuid.Parse(s) - if err != nil { - return err - } - *t = transportID(tID) - return nil -} diff --git a/cmd/skywire-cli/internal/internal.go b/cmd/skywire-cli/internal/internal.go index 3bbd3df576..0557eae116 100644 --- a/cmd/skywire-cli/internal/internal.go +++ b/cmd/skywire-cli/internal/internal.go @@ -4,10 +4,9 @@ import ( "fmt" "github.com/google/uuid" + "github.com/skycoin/skycoin/src/util/logging" "github.com/skycoin/skywire/pkg/cipher" - - "github.com/skycoin/skycoin/src/util/logging" ) var log = logging.MustGetLogger("skywire-cli") From 5dbdd23d5d6728286b5d70f5aebe6a1ea94efc23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E5=BF=97=E5=AE=87?= Date: Fri, 5 Apr 2019 13:58:09 +0800 Subject: [PATCH 10/11] updated README to accommodate changes to skywire-cli --- README.md | 56 ++++++++++++++++++++---------------------------------- users.db | Bin 32768 -> 0 bytes 2 files changed, 21 insertions(+), 35 deletions(-) delete mode 100644 users.db diff --git a/README.md b/README.md index d64786e8d7..14e3a2424f 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ $ make install # compiles and installs all binaries **Generate default json config** ```bash -skywire-cli gen-config +$ skywire-cli node gen-config ``` ### Run `skywire-node` @@ -88,37 +88,23 @@ The `skywire-cli` tool is used to control the `skywire-node`. Refer to the help ```bash $ skywire-cli -h -# Command Line Interface for skywire -# -# Usage: -# skywire-cli [command] -# -# Available Commands: -# add-rule adds a new routing rule -# add-transport adds a new transport -# apps lists apps running on the node -# find-routes lists available routes between two nodes via route finder service -# find-transport finds and lists transport(s) of given transport ID or edge public key from transport discovery -# gen-config Generate default config file -# help Help about any command -# list-rules lists the local node's routing rules -# list-transports lists the available transports with optional filter flags -# messaging manage operations with messaging services -# pk get public key of node -# rm-rule removes a routing rule via route ID key -# rm-transport removes transport with given id -# rule returns a routing rule via route ID key -# set-app-autostart sets the autostart flag for an app of given name -# start-app starts an app of given name -# stop-app stops an app of given name -# transport returns summary of given transport by id -# transport-types lists transport types used by the local node -# -# Flags: -# -h, --help help for skywire-cli -# --rpc string RPC server address (default "localhost:3435") -# -# Use "skywire-cli [command] --help" for more information about a command. +# Command Line Interface for skywire +# +# Usage: +# skywire-cli [command] +# +# Available Commands: +# help Help about any command +# mdisc Contains sub-commands that interact with a remote Messaging Discovery +# node Contains sub-commands that interact with the local Skywire (App) Node +# rtfind Queries the Route Finder for available routes between two nodes +# tpdisc Queries the Transport Discovery to find transport(s) of given transport ID or edge public key +# +# Flags: +# -h, --help help for skywire-cli +# +# Use "skywire-cli [command] --help" for more information about a command. + ``` ### Apps @@ -138,10 +124,10 @@ Transports can be established via the `skywire-cli`. ```bash # Establish transport to `0276ad1c5e77d7945ad6343a3c36a8014f463653b3375b6e02ebeaa3a21d89e881`. -$ skywire-cli add-transport 0276ad1c5e77d7945ad6343a3c36a8014f463653b3375b6e02ebeaa3a21d89e881 +$ skywire-cli node add-tp 0276ad1c5e77d7945ad6343a3c36a8014f463653b3375b6e02ebeaa3a21d89e881 # List established transports. -$ skywire-cli transports list +$ skywire-cli node ls-tp ``` ## App programming API @@ -309,7 +295,7 @@ $ GO111MODULE=on GOOS=linux go build -o /tmp/SKYNODE/apps/therealproxy.v1.0 ./cm $ GO111MODULE=on GOOS=linux go build -o /tmp/SKYNODE/apps/therealssh.v1.0 ./cmd/apps/therealssh $ GO111MODULE=on GOOS=linux go build -o /tmp/SKYNODE/apps/therealssh-client.v1.0 ./cmd/apps/therealssh-client # 4. Create skywire-config.json for node -$ skywire-cli gen-config -o /tmp/SKYNODE/skywire-config.json +$ skywire-cli node gen-config -o /tmp/SKYNODE/skywire-config.json # 2019/03/15 16:43:49 Done! $ tree /tmp/SKYNODE # /tmp/SKYNODE diff --git a/users.db b/users.db deleted file mode 100644 index 27182d35c599779c2dab3d2f41200ba210a26b1f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeI)t**i_6ae7zt4J;y3<`xKyu%gO@DNB45Cl9b?*JahcGn{y;7Dk~*JNwAbw(=hw>`_?>vMW`F`||_kVs5%hBR*`q14a0RjXF5FkK+009C72oNApfI#H)dAYxU zh=f3Z009C72oNAZfB*pk1PHuaAlCO|9^l>M8;<}10t5&UAV7cs0RjXF5cs@6?Bj2z zW+#7ruJ;1e-}z6fW4~K%cUAqgm^66G;wAwC1PBlyK!5-N0t5&UC`};#=Qm7EG9M{@ z> Date: Fri, 5 Apr 2019 13:59:14 +0800 Subject: [PATCH 11/11] update .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 377188452a..d46d8cacd7 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,4 @@ pkg/node/foo/ /skywire-cli /therealssh-cli /node -PK +/users.db \ No newline at end of file