Skip to content

Commit

Permalink
Merge pull request #37 from didil/real-time-updates
Browse files Browse the repository at this point in the history
Implement In Progress Updates support
  • Loading branch information
jimaek authored Apr 7, 2023
2 parents 0ebac75 + edd27b8 commit e3029a7
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 37 deletions.
22 changes: 22 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"strings"

"github.com/jsdelivr/globalping-cli/model"
)

func createLocations(from string) []model.Locations {
fromArr := strings.Split(from, ",")
locations := make([]model.Locations, len(fromArr))
for i, v := range fromArr {
locations[i] = model.Locations{
Magic: strings.TrimSpace(v),
}
}
return locations
}

func inProgressUpdates(ci bool) bool {
return !(ci)
}
17 changes: 17 additions & 0 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestInProgressUpdates_CI(t *testing.T) {
ci := true
assert.Equal(t, false, inProgressUpdates(ci))
}

func TestInProgressUpdates_NotCI(t *testing.T) {
ci := false
assert.Equal(t, true, inProgressUpdates(ci))
}
13 changes: 7 additions & 6 deletions cmd/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var dnsCmd = &cobra.Command{
Use: "dns [target] from [location]",
GroupID: "Measurements",
Short: "Use the native dig command",
Long: `Performs DNS lookups and displays the answers that are returned from the name server(s) that were queried.
Long: `Performs DNS lookups and displays the answers that are returned from the name server(s) that were queried.
The default nameserver depends on the probe and is defined by the user's local settings or DHCP.
Examples:
# Resolve google.com from 2 probes in New York
dns google.com from New York --limit 2
Expand All @@ -39,10 +39,11 @@ Examples:

// Make post struct
opts = model.PostMeasurement{
Type: "dns",
Target: ctx.Target,
Locations: createLocations(ctx.From),
Limit: ctx.Limit,
Type: "dns",
Target: ctx.Target,
Locations: createLocations(ctx.From),
Limit: ctx.Limit,
InProgressUpdates: inProgressUpdates(ctx.CI),
Options: &model.MeasurementOptions{
Protocol: protocol,
Port: port,
Expand Down
1 change: 1 addition & 0 deletions cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func buildHttpMeasurementRequest() (model.PostMeasurement, error) {
m.Target = urlData.Host
m.Locations = createLocations(ctx.From)
m.Limit = ctx.Limit
m.InProgressUpdates = inProgressUpdates(ctx.CI)
m.Options = &model.MeasurementOptions{
Protocol: overrideOpt(urlData.Protocol, httpCmdOpts.Protocol),
Port: overrideOptInt(urlData.Port, httpCmdOpts.Port),
Expand Down
11 changes: 6 additions & 5 deletions cmd/mtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var mtrCmd = &cobra.Command{
GroupID: "Measurements",
Short: "Use the native mtr command",
Long: `mtr combines the functionality of the traceroute and ping programs in a single network diagnostic tool.
Examples:
# MTR google.com from 2 probes in New York
mtr google.com from New York --limit 2
Expand All @@ -37,10 +37,11 @@ Examples:

// Make post struct
opts = model.PostMeasurement{
Type: "mtr",
Target: ctx.Target,
Locations: createLocations(ctx.From),
Limit: ctx.Limit,
Type: "mtr",
Target: ctx.Target,
Locations: createLocations(ctx.From),
Limit: ctx.Limit,
InProgressUpdates: inProgressUpdates(ctx.CI),
Options: &model.MeasurementOptions{
Protocol: protocol,
Port: port,
Expand Down
11 changes: 6 additions & 5 deletions cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var pingCmd = &cobra.Command{
GroupID: "Measurements",
Short: "Use the native ping command",
Long: `The ping command sends an ICMP ECHO_REQUEST to obtain an ICMP ECHO_RESPONSE from a host or gateway.
Examples:
# Ping google.com from 2 probes in New York
ping google.com from New York --limit 2
Expand All @@ -37,10 +37,11 @@ Examples:

// Make post struct
opts = model.PostMeasurement{
Type: "ping",
Target: ctx.Target,
Locations: createLocations(ctx.From),
Limit: ctx.Limit,
Type: "ping",
Target: ctx.Target,
Locations: createLocations(ctx.From),
Limit: ctx.Limit,
InProgressUpdates: inProgressUpdates(ctx.CI),
Options: &model.MeasurementOptions{
Packets: packets,
},
Expand Down
11 changes: 0 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,3 @@ func createContext(cmd string, args []string) error {

return nil
}

func createLocations(from string) []model.Locations {
fromArr := strings.Split(from, ",")
locations := make([]model.Locations, len(fromArr))
for i, v := range fromArr {
locations[i] = model.Locations{
Magic: strings.TrimSpace(v),
}
}
return locations
}
11 changes: 6 additions & 5 deletions cmd/traceroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var tracerouteCmd = &cobra.Command{
GroupID: "Measurements",
Short: "Use the native traceroute command",
Long: `traceroute tracks the route packets taken from an IP network on their way to a given host. It utilizes the IP protocol's time to live (TTL) field and attempts to elicit an ICMP TIME_EXCEEDED response from each gateway along the path to the host.
Examples:
# Traceroute google.com from 2 probes in New York
traceroute google.com from New York --limit 2
Expand All @@ -37,10 +37,11 @@ Examples:

// Make post struct
opts = model.PostMeasurement{
Type: "traceroute",
Target: ctx.Target,
Locations: createLocations(ctx.From),
Limit: ctx.Limit,
Type: "traceroute",
Target: ctx.Target,
Locations: createLocations(ctx.From),
Limit: ctx.Limit,
InProgressUpdates: inProgressUpdates(ctx.CI),
Options: &model.MeasurementOptions{
Protocol: protocol,
Port: port,
Expand Down
11 changes: 6 additions & 5 deletions model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ type MeasurementOptions struct {

// Main struct
type PostMeasurement struct {
Limit int `json:"limit"`
Locations []Locations `json:"locations"`
Type string `json:"type"`
Target string `json:"target"`
Options *MeasurementOptions `json:"measurementOptions,omitempty"`
Limit int `json:"limit"`
Locations []Locations `json:"locations"`
Type string `json:"type"`
Target string `json:"target"`
InProgressUpdates bool `json:"inProgressUpdates"`
Options *MeasurementOptions `json:"measurementOptions,omitempty"`
}

type PostResponse struct {
Expand Down

0 comments on commit e3029a7

Please sign in to comment.