Skip to content

Commit

Permalink
Add short flags (#101)
Browse files Browse the repository at this point in the history
Add short flags

This change breaks the backwards compatibility with older version of mole since the longhand flags are passed using  double dashes, `--`, instead of single dash, `-`. 

Signed-off-by: Ricardo Seriani <ricardo.seriani@gmail.com>
  • Loading branch information
ricardoseriani authored and davrodpin committed Oct 6, 2019
1 parent 254efc9 commit 49efce0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 39 deletions.
59 changes: 35 additions & 24 deletions cli/cli.go
@@ -1,12 +1,13 @@
package cli

import (
"flag"
"fmt"
"os"
"regexp"
"strings"
"time"

flag "github.com/spf13/pflag"
)

var re = regexp.MustCompile(`(?P<user>.+@)?(?P<host>[[:alpha:][:digit:]\_\-\.]+)?(?P<port>:[0-9]+)?`)
Expand Down Expand Up @@ -48,24 +49,24 @@ func (c *App) Parse() error {
f.Usage = c.PrintUsage
c.flag = f

f.StringVar(&c.Alias, "alias", "", "create a tunnel alias")
f.BoolVar(&c.AliasDelete, "delete", false, "delete a tunnel alias (must be used with -alias)")
f.BoolVar(&c.AliasList, "aliases", false, "list all aliases")
f.StringVar(&c.Start, "start", "", "start a tunnel using a given alias")
f.Var(&c.Local, "local", "(optional) set local endpoint address: [<host>]:<port>. Multiple -local args can be provided")
f.Var(&c.Remote, "remote", "(optional) set remote endpoint address: [<host>]:<port>. Multiple -remote args can be provided")
f.Var(&c.Server, "server", "set server address: [<user>@]<host>[:<port>]")
f.StringVar(&c.Key, "key", "", "(optional) Set server authentication key file path")
f.BoolVar(&c.Verbose, "v", false, "(optional) Increase log verbosity")
f.BoolVar(&c.Help, "help", false, "list all options available")
f.StringVarP(&c.Alias, "alias", "a", "", "create a tunnel alias")
f.BoolVarP(&c.AliasDelete, "delete", "d", false, "delete a tunnel alias (must be used with --alias)")
f.BoolVarP(&c.AliasList, "aliases", "I", false, "list all aliases")
f.StringVarP(&c.Start, "start", "S", "", "start a tunnel using a given alias")
f.VarP(&c.Local, "local", "l", "(optional) set local endpoint address: [<host>]:<port>. Multiple -local args can be provided")
f.VarP(&c.Remote, "remote", "r", "(optional) set remote endpoint address: [<host>]:<port>. Multiple -remote args can be provided")
f.VarP(&c.Server, "server", "s", "set server address: [<user>@]<host>[:<port>]")
f.StringVarP(&c.Key, "key", "k", "", "(optional) Set server authentication key file path")
f.BoolVarP(&c.Verbose, "verbose", "v", false, "(optional) Increase log verbosity")
f.BoolVarP(&c.Help, "help", "h", false, "list all options available")
f.BoolVar(&c.Version, "version", false, "display the mole version")
f.BoolVar(&c.Detach, "detach", false, "(optional) run process in background")
f.StringVar(&c.Stop, "stop", "", "stop background process")
f.BoolVar(&c.Insecure, "insecure", false, "(optional) skip host key validation when connecting to ssh server")
f.DurationVar(&c.KeepAliveInterval, "keep-alive-interval", 10*time.Second, "(optional) time interval for keep alive packets to be sent")
f.DurationVar(&c.Timeout, "timeout", 3*time.Second, "(optional) ssh server connection timeout")
f.IntVar(&c.ConnectionRetries, "connection-retries", 3, "(optional) maximum number of connection retries to the ssh server. Provide 0 if mole should never give up or negative number to disable retries")
f.DurationVar(&c.WaitAndRetry, "retry-wait", 3*time.Second, "(optional) time to wait before trying to reconnect to ssh server")
f.BoolVarP(&c.Detach, "detach", "x", false, "(optional) run process in background")
f.StringVarP(&c.Stop, "stop", "o", "", "stop background process")
f.BoolVarP(&c.Insecure, "insecure", "i", false, "(optional) skip host key validation when connecting to ssh server")
f.DurationVarP(&c.KeepAliveInterval, "keep-alive-interval", "K", 10*time.Second, "(optional) time interval for keep alive packets to be sent")
f.DurationVarP(&c.Timeout, "timeout", "t", 3*time.Second, "(optional) ssh server connection timeout")
f.IntVarP(&c.ConnectionRetries, "connection-retries", "R", 3, "(optional) maximum number of connection retries to the ssh server. Provide 0 if mole should never give up or negative number to disable retries")
f.DurationVarP(&c.WaitAndRetry, "retry-wait", "w", 3*time.Second, "(optional) time to wait before trying to reconnect to ssh server")

f.Parse(c.args[1:])

Expand Down Expand Up @@ -120,12 +121,12 @@ func (c App) Validate() error {
// use the tool.
func (c *App) PrintUsage() {
fmt.Fprintf(os.Stderr, "%s\n\n", `usage:
mole [-v] [-insecure] [-detach] (-local [<host>]:<port>)... (-remote [<host>]:<port>)... -server [<user>@]<host>[:<port>] [-key <key_path>] [-keep-alive-interval <time_interval>] [-connection-retries <retries>] [-retry-wait <time>]
mole -alias <alias_name> [-v] (-local [<host>]:<port>)... (-remote [<host>]:<port>)... -server [<user>@]<host>[:<port>] [-key <key_path>] [-keep-alive-interval <time_interval>] [-connection-retries <retries>] [-retry-wait <time>]
mole -alias <alias_name> -delete
mole -start <alias_name>
mole -help
mole -version`)
mole [--verbose] [--insecure] [--detach] (--local [<host>]:<port>)... (--remote [<host>]:<port>)... --server [<user>@]<host>[:<port>] [--key <key_path>] [--keep-alive-interval <time_interval>] [--connection-retries <retries>] [--retry-wait <time>]
mole --alias <alias_name> [--verbose] (--local [<host>]:<port>)... (--remote [<host>]:<port>)... --server [<user>@]<host>[:<port>] [--key <key_path>] [--keep-alive-interval <time_interval>] [--connection-retries <retries>] [--retry-wait <time>]
mole --alias <alias_name> --delete
mole --start <alias_name>
mole --help
mole --version`)
c.flag.PrintDefaults()
}

Expand Down Expand Up @@ -164,6 +165,11 @@ func (h *AddressInput) Set(value string) error {
return nil
}

// Type return a string representation of AddressInput.
func (h *AddressInput) Type() string {
return "[<user>@][<host>]:<port>"
}

// Address returns a string representation of AddressInput to be used to perform
// network connections.
func (h AddressInput) Address() string {
Expand Down Expand Up @@ -213,6 +219,11 @@ func (il *AddressInputList) Set(value string) error {
return nil
}

// Type return a string representation of AddressInputList.
func (il *AddressInputList) Type() string {
return "([<host>]:<port>)..."
}

func (il AddressInputList) List() []string {
sl := []string{}

Expand Down
88 changes: 73 additions & 15 deletions cli/cli_test.go
Expand Up @@ -68,31 +68,56 @@ func TestCommand(t *testing.T) {
expected string
}{
{
[]string{"./mole", "-version"},
[]string{"./mole", "--version"},
"version",
},
{
[]string{"./mole", "-help"},
[]string{"./mole", "--help"},
"help",
},
{
[]string{"./mole", "-remote", ":443", "-server", "example1"},
[]string{"./mole", "--remote", ":443", "--server", "example1"},
"start",
},
{
[]string{"./mole", "-alias", "xyz", "-remote", ":443", "-server", "example1"},
[]string{"./mole", "--alias", "xyz", "--remote", ":443", "--server", "example1"},
"new-alias",
},
{
[]string{"./mole", "-alias", "xyz", "-delete"},
[]string{"./mole", "--alias", "xyz", "--delete"},
"rm-alias",
},
{
[]string{"./mole", "-aliases"},
[]string{"./mole", "--aliases"},
"aliases",
},
{
[]string{"./mole", "-start", "example1-alias"},
[]string{"./mole", "--start", "example1-alias"},
"start-from-alias",
},
// Short Flags
{
[]string{"./mole", "-h"},
"help",
},
{
[]string{"./mole", "-r", ":443", "-s", "example1"},
"start",
},
{
[]string{"./mole", "-a", "xyz", "-r", ":443", "-s", "example1"},
"new-alias",
},
{
[]string{"./mole", "-a", "xyz", "-d"},
"rm-alias",
},
{
[]string{"./mole", "-I"},
"aliases",
},
{
[]string{"./mole", "-S", "example1-alias"},
"start-from-alias",
},
}
Expand All @@ -119,35 +144,68 @@ func TestValidate(t *testing.T) {
false,
},
{
[]string{"./mole", "-alias", "xyz", "-remote", ":443", "-server", "example1"},
[]string{"./mole", "--alias", "xyz", "--remote", ":443", "--server", "example1"},
true,
},
{
[]string{"./mole", "--alias", "xyz", "--remote", ":443"},
false,
},
{
[]string{"./mole", "--alias", "xyz", "--server", "example1"},
true,
},
{
[]string{"./mole", "--alias", "xyz", "--remote", ":443"},
false,
},
{
[]string{"./mole", "--alias", "xyz"},
false,
},
{
[]string{"./mole", "--local", ":8080", "--remote", ":80", "--server", "example1"},
true,
},
{
[]string{"./mole", "--remote", ":3366", "--remote", ":443", "--server", "example1"},
true,
},
{
[]string{"./mole", "--local", ":1234", "--remote", ":3366", "--remote", ":443", "--server", "example1"},
true,
},
// Short Flags
{
[]string{"./mole", "-a", "xyz", "-r", ":443", "-s", "example1"},
true,
},
{
[]string{"./mole", "-alias", "xyz", "-remote", ":443"},
[]string{"./mole", "-a", "xyz", "-r", ":443"},
false,
},
{
[]string{"./mole", "-alias", "xyz", "-server", "example1"},
[]string{"./mole", "-a", "xyz", "-s", "example1"},
true,
},
{
[]string{"./mole", "-alias", "xyz", "-remote", ":443"},
[]string{"./mole", "-a", "xyz", "-r", ":443"},
false,
},
{
[]string{"./mole", "-alias", "xyz"},
[]string{"./mole", "-a", "xyz"},
false,
},
{
[]string{"./mole", "-local", ":8080", "-remote", ":80", "-server", "example1"},
[]string{"./mole", "-l", ":8080", "-r", ":80", "-s", "example1"},
true,
},
{
[]string{"./mole", "-remote", ":3366", "-remote", ":443", "-server", "example1"},
[]string{"./mole", "-r", ":3366", "-r", ":443", "-s", "example1"},
true,
},
{
[]string{"./mole", "-local", ":1234", "-remote", ":3366", "-remote", ":443", "-server", "example1"},
[]string{"./mole", "-s", ":1234", "-r", ":3366", "-r", ":443", "-s", "example1"},
true,
},
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -12,6 +12,7 @@ require (
github.com/pelletier/go-buffruneio v0.2.0 // indirect
github.com/sevlyar/go-daemon v0.1.5
github.com/sirupsen/logrus v1.4.2
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
)
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -23,6 +23,8 @@ github.com/sevlyar/go-daemon v0.1.5 h1:Zy/6jLbM8CfqJ4x4RPr7MJlSKt90f00kNM1D401C+
github.com/sevlyar/go-daemon v0.1.5/go.mod h1:6dJpPatBT9eUwM5VCw9Bt6CdX9Tk6UWvhW3MebLDRKE=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
Expand Down

0 comments on commit 49efce0

Please sign in to comment.