Skip to content

Commit

Permalink
Merge pull request #33 from condemil/master
Browse files Browse the repository at this point in the history
Return error when required flag is missing
  • Loading branch information
davrodpin authored Oct 20, 2018
2 parents 655fff6 + fae8be2 commit 7cc53ef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
44 changes: 22 additions & 22 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"flag"
"fmt"
"os"
"regexp"
"strings"
)
Expand Down Expand Up @@ -31,7 +32,9 @@ func New(args []string) *App {
}

func (c *App) Parse() error {
f := flag.NewFlagSet(usage(), flag.ExitOnError)
f := flag.NewFlagSet("", flag.ExitOnError)
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)")
Expand All @@ -46,12 +49,6 @@ func (c *App) Parse() error {

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

c.flag = f

if len(c.args[1:]) == 0 {
return fmt.Errorf("not enough arguments provided")
}

if c.Help {
c.Command = "help"
} else if c.Version {
Expand All @@ -76,17 +73,31 @@ func (c *App) Parse() error {
}

func (c App) Validate() error {
if c.Command == "new-alias" && (c.Remote.String() == "" || c.Server.String() == "") {
return fmt.Errorf("remote and server options are required for new alias")
if len(c.args[1:]) == 0 {
return fmt.Errorf("not enough arguments provided")
}

switch c.Command {
case "start", "new-alias":
if c.Remote.String() == "" {
return fmt.Errorf("required flag is missing: -remote")
} else if c.Server.String() == "" {
return fmt.Errorf("required flag is missing: -server")
}
}
return nil
}

// PrintUsage prints, to the standard output, the informational text on how to
// use the tool.
func (c App) PrintUsage() {
fmt.Printf("%s\n", usage())
func (c *App) PrintUsage() {
fmt.Fprintf(os.Stderr, "%s\n\n", `usage:
mole [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
mole -alias <alias_name> [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
mole -alias <alias_name> -delete
mole -start <alias_name>
mole -help
mole -version`)
c.flag.PrintDefaults()
}

Expand Down Expand Up @@ -135,17 +146,6 @@ func (h HostInput) Address() string {
return fmt.Sprintf("%s:%s", h.Host, h.Port)
}

func usage() string {
return `usage:
mole [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
mole -alias <alias_name> [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
mole -alias <alias_name> -delete
mole -start <alias_name>
mole -help
mole -version
`
}

func parseServerInput(input string) map[string]string {
match := re.FindStringSubmatch(input)
result := make(map[string]string)
Expand Down
12 changes: 12 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,22 @@ func TestValidate(t *testing.T) {
args []string
expected bool
}{
{
[]string{"./mole"},
false,
},
{
[]string{"./mole", "-alias", "xyz", "-remote", ":443", "-server", "example1"},
true,
},
{
[]string{"./mole", "-alias", "xyz", "-remote", ":443"},
false,
},
{
[]string{"./mole", "-alias", "xyz", "-server", "example1"},
false,
},
{
[]string{"./mole", "-alias", "xyz", "-server", "example1"},
false,
Expand Down
1 change: 1 addition & 0 deletions cmd/mole/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
app := cli.New(os.Args)
err := app.Parse()
if err != nil {
fmt.Printf("%v\n", err)
app.PrintUsage()
os.Exit(1)
}
Expand Down

0 comments on commit 7cc53ef

Please sign in to comment.