Skip to content

Commit

Permalink
feat: show help when running empty commands (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
radulucut authored Jan 18, 2025
1 parent 3b2fe8c commit b066ac7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 25 deletions.
15 changes: 12 additions & 3 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ import (
"github.com/jsdelivr/globalping-cli/storage"
"github.com/jsdelivr/globalping-cli/version"
"github.com/jsdelivr/globalping-cli/view"
"github.com/spf13/cobra"
)

var (
ErrTargetIPVersionNotAllowed = errors.New("ipVersion is not allowed when target is not a domain")
ErrResolverIPVersionNotAllowed = errors.New("ipVersion is not allowed when resolver is not a domain")
)

func (r *Root) updateContext(cmd string, args []string) error {
r.ctx.Cmd = cmd // Get the command name
func (r *Root) updateContext(cmd *cobra.Command, args []string) error {
r.ctx.Cmd = cmd.CalledAs() // Get the command name

targetQuery, err := parseTargetQuery(cmd, args)
// if the command does not have any arguments or flags, show help
if len(os.Args) == 2 {
cmd.SilenceErrors = true
cmd.SilenceUsage = true
cmd.Help()
return errors.New("")
}

targetQuery, err := parseTargetQuery(r.ctx.Cmd, args)
if err != nil {
return err
}
Expand Down
48 changes: 35 additions & 13 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/jsdelivr/globalping-cli/version"
"github.com/jsdelivr/globalping-cli/view"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

Expand All @@ -30,8 +31,11 @@ func test_updateContext_NoArg(t *testing.T) {
printer := view.NewPrinter(nil, nil, nil)
root := NewRoot(printer, ctx, nil, nil, nil, nil, nil)

err := root.updateContext("test", []string{"1.1.1.1"})
assert.Equal(t, "test", ctx.Cmd)
cmd := &cobra.Command{Use: "ping"}
cmd.Execute()

err := root.updateContext(cmd, []string{"1.1.1.1"})
assert.Equal(t, "ping", ctx.Cmd)
assert.Equal(t, "1.1.1.1", ctx.Target)
assert.Equal(t, "world", ctx.From)
assert.NoError(t, err)
Expand All @@ -42,8 +46,11 @@ func test_updateContext_Country(t *testing.T) {
printer := view.NewPrinter(nil, nil, nil)
root := NewRoot(printer, ctx, nil, nil, nil, nil, nil)

err := root.updateContext("test", []string{"1.1.1.1", "from", "Germany"})
assert.Equal(t, "test", ctx.Cmd)
cmd := &cobra.Command{Use: "ping"}
cmd.Execute()

err := root.updateContext(cmd, []string{"1.1.1.1", "from", "Germany"})
assert.Equal(t, "ping", ctx.Cmd)
assert.Equal(t, "1.1.1.1", ctx.Target)
assert.Equal(t, "Germany", ctx.From)
assert.NoError(t, err)
Expand All @@ -55,8 +62,11 @@ func test_updateContext_CountryWhitespace(t *testing.T) {
printer := view.NewPrinter(nil, nil, nil)
root := NewRoot(printer, ctx, nil, nil, nil, nil, nil)

err := root.updateContext("test", []string{"1.1.1.1", "from", " Germany, France"})
assert.Equal(t, "test", ctx.Cmd)
cmd := &cobra.Command{Use: "ping"}
cmd.Execute()

err := root.updateContext(cmd, []string{"1.1.1.1", "from", " Germany, France"})
assert.Equal(t, "ping", ctx.Cmd)
assert.Equal(t, "1.1.1.1", ctx.Target)
assert.Equal(t, "Germany, France", ctx.From)
assert.NoError(t, err)
Expand All @@ -67,7 +77,10 @@ func test_updateContext_NoTarget(t *testing.T) {
printer := view.NewPrinter(nil, nil, nil)
root := NewRoot(printer, ctx, nil, nil, nil, nil, nil)

err := root.updateContext("test", []string{})
cmd := &cobra.Command{Use: "ping"}
cmd.Execute()

err := root.updateContext(cmd, []string{})
assert.Error(t, err)
}

Expand All @@ -80,8 +93,11 @@ func test_updateContext_CIEnv(t *testing.T) {
printer := view.NewPrinter(nil, nil, nil)
root := NewRoot(printer, ctx, nil, nil, nil, nil, nil)

err := root.updateContext("test", []string{"1.1.1.1"})
assert.Equal(t, "test", ctx.Cmd)
cmd := &cobra.Command{Use: "ping"}
cmd.Execute()

err := root.updateContext(cmd, []string{"1.1.1.1"})
assert.Equal(t, "ping", ctx.Cmd)
assert.Equal(t, "1.1.1.1", ctx.Target)
assert.Equal(t, "world", ctx.From)
assert.True(t, ctx.CIMode)
Expand All @@ -94,12 +110,15 @@ func test_updateContext_TargetIsNotAHostname(t *testing.T) {
printer := view.NewPrinter(nil, nil, nil)
root := NewRoot(printer, ctx, nil, nil, nil, nil, nil)

err := root.updateContext("ping", []string{"1.1.1.1"})
cmd := &cobra.Command{Use: "ping"}
cmd.Execute()

err := root.updateContext(cmd, []string{"1.1.1.1"})
assert.EqualError(t, err, ErrTargetIPVersionNotAllowed.Error())

ctx.Ipv4 = false
ctx.Ipv6 = true
err = root.updateContext("ping", []string{"1.1.1.1"})
err = root.updateContext(cmd, []string{"1.1.1.1"})
assert.EqualError(t, err, ErrTargetIPVersionNotAllowed.Error())
}

Expand All @@ -109,12 +128,15 @@ func test_updateContext_ResolverIsNotAHostname(t *testing.T) {
printer := view.NewPrinter(nil, nil, nil)
root := NewRoot(printer, ctx, nil, nil, nil, nil, nil)

err := root.updateContext("dns", []string{"example.com", "@1.1.1.1"})
cmd := &cobra.Command{Use: "dns"}
cmd.Execute()

err := root.updateContext(cmd, []string{"example.com", "@1.1.1.1"})
assert.EqualError(t, err, ErrResolverIPVersionNotAllowed.Error())

ctx.Ipv4 = false
ctx.Ipv6 = true
err = root.updateContext("dns", []string{"example.com", "@1.1.1.1"})
err = root.updateContext(cmd, []string{"example.com", "@1.1.1.1"})
assert.EqualError(t, err, ErrResolverIPVersionNotAllowed.Error())
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (r *Root) initDNS(measurementFlags *pflag.FlagSet, localFlags *pflag.FlagSe
GroupID: "Measurements",
Short: "Resolve DNS records, similar to the dig command",
Long: `The dns command (similar to the "dig" command) performs DNS lookups and displays the responses from the queried name servers, helping you troubleshoot DNS-related issues.
Note that a probe's local settings or DHCP determine the default nameserver the command uses. To specify a DNS resolver, use the --resolver argument or @resolver format:
Note that a probe's local settings or DHCP determine the default nameserver the command uses. To specify a DNS resolver, use the --resolver argument or @resolver format:
- dns jsdelivr.com from Berlin --resolver 1.1.1.1
- dns jsdelivr.com @1.1.1.1 from Berlin
Expand Down Expand Up @@ -48,7 +48,7 @@ Examples:
# Resolve jsdelivr.com from a probe in ASN 123 and output the results in JSON format.
dns jsdelivr.com from 123 --json
# Resolve jsdelivr.com from a non-data center probe in Europe and add a link to view the results online..
dns jsdelivr.com from europe+eyeball --share`,
}
Expand All @@ -67,7 +67,7 @@ Examples:
}

func (r *Root) RunDNS(cmd *cobra.Command, args []string) error {
err := r.updateContext(cmd.CalledAs(), args)
err := r.updateContext(cmd, args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Examples:
}

func (r *Root) RunHTTP(cmd *cobra.Command, args []string) error {
err := r.updateContext(cmd.CalledAs(), args)
err := r.updateContext(cmd, args)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/mtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Examples:
# MTR jsdelivr.com from a probe in ASN 123 and output the results in JSON format.
mtr jsdelivr.com from 123 --json
# MTR jsdelivr.com from a non-data center probe in Europe and add a link to view the results online.
mtr jsdelivr.com from europe+eyeball --share `,
}
Expand All @@ -58,7 +58,7 @@ Examples:
}

func (r *Root) RunMTR(cmd *cobra.Command, args []string) error {
err := r.updateContext(cmd.CalledAs(), args)
err := r.updateContext(cmd, args)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Examples:
}

func (r *Root) RunPing(cmd *cobra.Command, args []string) error {
err := r.updateContext(cmd.CalledAs(), args)
err := r.updateContext(cmd, args)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Examples:
# Traceroute jsdelivr.com from a probe in ASN 123 and output the results in JSON format.
traceroute jsdelivr.com from 123 --json
# Traceroute jsdelivr.com from a non-data center probe in Europe and add a link to view the results online.
traceroute jsdelivr.com from europe+eyeball --share`,
}
Expand All @@ -60,7 +60,7 @@ Examples:
}

func (r *Root) RunTraceroute(cmd *cobra.Command, args []string) error {
err := r.updateContext(cmd.CalledAs(), args)
err := r.updateContext(cmd, args)
if err != nil {
return err
}
Expand Down

0 comments on commit b066ac7

Please sign in to comment.