Browse Source

:art: Refactor services CLI UX

Make --srcaddress and --remoteaddress just --address. Additionally, take
first args instead of providing flags. Flags have precedence
Ettore Di Giacinto 3 years ago
parent
commit
5f72fc6bb4
4 changed files with 89 additions and 30 deletions
  1. 2 2
      .github/servicestest.sh
  2. 8 4
      README.md
  3. 39 10
      cmd/file.go
  4. 40 14
      cmd/service.go

+ 2 - 2
.github/servicestest.sh

@@ -2,7 +2,7 @@
 ./edgevpn api &
 
 if [ $1 == "expose" ]; then
-    ./edgevpn service-add --name "testservice" --remoteaddress "127.0.0.1:8080" &
+    ./edgevpn service-add testservice 127.0.0.1:8080 &
 
     ((count = 100))                        
     while [[ $count -ne 0 ]] ; do
@@ -25,7 +25,7 @@ if [ $1 == "expose" ]; then
     fi
     
 else
-    ./edgevpn service-connect --name "testservice" --srcaddress ":9090" &
+    ./edgevpn service-connect testservice :9090 &
 
     ((count = 240))                        
     while [[ $count -ne 0 ]] ; do

+ 8 - 4
README.md

@@ -117,29 +117,33 @@ A Service is a generalized TCP service running in a host (also outside the netwo
 To expose a service to your EdgeVPN network then:
 
 ```bash
-$ edgevpn service-add --name "MyCoolService" --remoteaddress "127.0.0.1:22"
+$ edgevpn service-add "MyCoolService" "127.0.0.1:22"
 ```
 
 To reach the service, EdgeVPN will setup a local port and bind to it, it will tunnel the traffic to the service over the VPN, for e.g. to bind locally to `9090`:
 
 ```bash
-$ edgevpn service-connect --name "MyCoolService" --srcaddress "127.0.0.1:9090"
+$ edgevpn service-connect "MyCoolService" "127.0.0.1:9090"
 ```
 
 with the example above, 'sshing into `9090` locally would forward to `22`.
 
 ## :mailbox: Sending and receiving files
 
+EdgeVPN can be used to send and receive files between hosts via p2p with the  `file-send` and `file-receive` subcommand.
+
+Sending and receiving files, as services, don't establish a VPN connection.
+
 ### :outbox_tray: Sending
 
 ```bash
-$ edgevpn file-send --name 'unique-id' --path '/src/path'
+$ edgevpn file-send 'unique-id' '/src/path'
 ```
 
 ### :inbox_tray: Receiving
 
 ```bash
-$ edgevpn file-receive --name 'unique-id' --path '/dst/path'
+$ edgevpn file-receive 'unique-id' '/dst/path'
 ```
 
 ## :globe_with_meridians: Web interface

+ 39 - 10
cmd/file.go

@@ -1,17 +1,39 @@
 package cmd
 
 import (
+	"errors"
+
 	"github.com/mudler/edgevpn/pkg/edgevpn"
 	"github.com/urfave/cli"
 )
 
+func cliNamePath(c *cli.Context) (name, path string, err error) {
+	name = c.Args().Get(0)
+	path = c.Args().Get(1)
+	if name == "" && c.String("name") == "" {
+		err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
+		return
+	}
+	if path == "" && c.String("path") == "" {
+		err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
+		return
+	}
+	if c.String("name") != "" {
+		name = c.String("name")
+	}
+	if c.String("path") != "" {
+		path = c.String("path")
+	}
+	return name, path, nil
+}
+
 func FileSend() cli.Command {
 	return cli.Command{
 		Name:        "file-send",
 		Aliases:     []string{"fs"},
 		Usage:       "Serve a file to the network",
 		Description: `Serve a file to the network without connecting over VPN`,
-		UsageText:   "edgevpn file-send --name 'unique-id' --path '/src/path'",
+		UsageText:   "edgevpn file-send unique-id /src/path",
 		Flags: append(CommonFlags,
 			cli.StringFlag{
 				Name:     "name",
@@ -26,6 +48,10 @@ This is also the ID used to refer when receiving it.`,
 			},
 		),
 		Action: func(c *cli.Context) error {
+			name, path, err := cliNamePath(c)
+			if err != nil {
+				return err
+			}
 			e := edgevpn.New(cliToOpts(c)...)
 
 			displayStart(e)
@@ -35,7 +61,7 @@ This is also the ID used to refer when receiving it.`,
 				return err
 			}
 			// Join the node to the network, using our ledger
-			e.SendFile(ledger, c.String("name"), c.String("path"))
+			e.SendFile(ledger, name, path)
 			// Join the node to the network, using our ledger
 			if err := e.Join(); err != nil {
 				return err
@@ -53,20 +79,23 @@ func FileReceive() cli.Command {
 		Aliases:     []string{"fr"},
 		Usage:       "Receive a file which is served from the network",
 		Description: `Receive a file from the network without connecting over VPN`,
-		UsageText:   "edgevpn file-receive --name 'unique-id' --path '/dst/path'",
+		UsageText:   "edgevpn file-receive unique-id /dst/path",
 		Flags: append(CommonFlags,
 			cli.StringFlag{
-				Name:     "name",
-				Usage:    `Unique name of the file to be received over the network.`,
-				Required: true,
+				Name:  "name",
+				Usage: `Unique name of the file to be received over the network.`,
 			},
 			cli.StringFlag{
-				Name:     "path",
-				Usage:    `Destination where to save the file`,
-				Required: true,
+				Name:  "path",
+				Usage: `Destination where to save the file`,
 			},
 		),
 		Action: func(c *cli.Context) error {
+			name, path, err := cliNamePath(c)
+			if err != nil {
+				return err
+			}
+
 			e := edgevpn.New(cliToOpts(c)...)
 
 			displayStart(e)
@@ -78,7 +107,7 @@ func FileReceive() cli.Command {
 
 			ledger, _ := e.Ledger()
 
-			return e.ReceiveFile(ledger, c.String("name"), c.String("path"))
+			return e.ReceiveFile(ledger, name, path)
 		},
 	}
 }

+ 40 - 14
cmd/service.go

@@ -1,10 +1,32 @@
 package cmd
 
 import (
+	"errors"
+
 	"github.com/mudler/edgevpn/pkg/edgevpn"
 	"github.com/urfave/cli"
 )
 
+func cliNameAddress(c *cli.Context) (name, address string, err error) {
+	name = c.Args().Get(0)
+	address = c.Args().Get(1)
+	if name == "" && c.String("name") == "" {
+		err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
+		return
+	}
+	if address == "" && c.String("address") == "" {
+		err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
+		return
+	}
+	if c.String("name") != "" {
+		name = c.String("name")
+	}
+	if c.String("address") != "" {
+		address = c.String("address")
+	}
+	return name, address, nil
+}
+
 func ServiceAdd() cli.Command {
 	return cli.Command{
 		Name:    "service-add",
@@ -12,21 +34,23 @@ func ServiceAdd() cli.Command {
 		Usage:   "Expose a service to the network without creating a VPN",
 		Description: `Expose a local or a remote endpoint connection as a service in the VPN. 
 		The host will act as a proxy between the service and the connection`,
-		UsageText: "edgevpn service-add --name 'unique-id' --remoteaddress 'ip:port'",
+		UsageText: "edgevpn service-add unique-id ip:port",
 		Flags: append(CommonFlags,
 			cli.StringFlag{
-				Name:     "name",
-				Usage:    `Unique name of the service to be server over the network.`,
-				Required: true,
+				Name:  "name",
+				Usage: `Unique name of the service to be server over the network.`,
 			},
 			cli.StringFlag{
-				Name:     "remoteaddress",
-				Required: true,
+				Name: "address",
 				Usage: `Remote address that the service is running to. That can be a remote webserver, a local SSH server, etc.
 For example, '192.168.1.1:80', or '127.0.0.1:22'.`,
 			},
 		),
 		Action: func(c *cli.Context) error {
+			name, address, err := cliNameAddress(c)
+			if err != nil {
+				return err
+			}
 			e := edgevpn.New(cliToOpts(c)...)
 
 			displayStart(e)
@@ -37,7 +61,7 @@ For example, '192.168.1.1:80', or '127.0.0.1:22'.`,
 			}
 
 			// Join the node to the network, using our ledger
-			e.ExposeService(ledger, c.String("name"), c.String("remoteaddress"))
+			e.ExposeService(ledger, name, address)
 			// Join the node to the network, using our ledger
 			if err := e.Join(); err != nil {
 				return err
@@ -57,21 +81,23 @@ func ServiceConnect() cli.Command {
 		Description: `Bind a local port to connect to a remote service in the network.
 Creates a local listener which connects over the service in the network without creating a VPN.
 `,
-		UsageText: "edgevpn service-connect --name 'unique-id' --srcaddress '(ip):port'",
+		UsageText: "edgevpn service-connect unique-id (ip):port",
 		Flags: append(CommonFlags,
 			cli.StringFlag{
-				Name:     "name",
-				Usage:    `Unique name of the service in the network.`,
-				Required: true,
+				Name:  "name",
+				Usage: `Unique name of the service in the network.`,
 			},
 			cli.StringFlag{
-				Name: "srcaddress",
+				Name: "address",
 				Usage: `Address where to bind locally. E.g. ':8080'. A proxy will be created
 to the service over the network`,
-				Required: true,
 			},
 		),
 		Action: func(c *cli.Context) error {
+			name, address, err := cliNameAddress(c)
+			if err != nil {
+				return err
+			}
 			e := edgevpn.New(cliToOpts(c)...)
 
 			displayStart(e)
@@ -82,7 +108,7 @@ to the service over the network`,
 			}
 
 			ledger, _ := e.Ledger()
-			return e.ConnectToService(ledger, c.String("name"), c.String("srcaddress"))
+			return e.ConnectToService(ledger, name, address)
 		},
 	}
 }