Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error when required flag is missing #33

Merged
merged 1 commit into from
Oct 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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