Skip to content

Commit e18a000

Browse files
committed
POD-135: Refactor a bit
1 parent 3bd2a59 commit e18a000

9 files changed

Lines changed: 173 additions & 108 deletions

File tree

cmd/cities.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

cmd/countries.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

cmd/list.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/ProspectOne/perfops-cli/perfops"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var (
11+
listTypesMap = map[string]func(client *perfops.Client) error{
12+
"countries": runCountriesCmd,
13+
"cities": runCitiesCmd,
14+
}
15+
16+
listCmd = &cobra.Command{
17+
Use: "list [type]",
18+
Short: "Get a locations where PerfOps nodes are present",
19+
Long: `Get a locations where PerfOps nodes are present, e.g., 'list countries' or 'list cities'`,
20+
Example: `perfops list countries`,
21+
Args: func(cmd *cobra.Command, args []string) error {
22+
if len(args) < 1 {
23+
return errors.New("specify the type of data You want to get")
24+
}
25+
return nil
26+
},
27+
RunE: func(cmd *cobra.Command, args []string) error {
28+
c, err := newPerfOpsClient()
29+
if err != nil {
30+
return err
31+
}
32+
return chkRunError(runListCmd(c, args[0]))
33+
},
34+
}
35+
)
36+
37+
func initListCmd(parentCmd *cobra.Command) {
38+
// Disable global flags
39+
parentCmd.ResetFlags()
40+
41+
parentCmd.AddCommand(listCmd)
42+
}
43+
44+
func runListCmd(c *perfops.Client, dataType string) error {
45+
if f, ok := listTypesMap[dataType]; ok {
46+
err := f(c)
47+
if err != nil {
48+
return errors.New(fmt.Sprintf("error happened %v", err))
49+
}
50+
51+
return nil
52+
}
53+
54+
return errors.New(fmt.Sprintf("no data with type '%s'", dataType))
55+
}

cmd/list_cities.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"github.com/ProspectOne/perfops-cli/cmd/internal"
6+
"github.com/ProspectOne/perfops-cli/perfops"
7+
"net/http"
8+
)
9+
10+
func runCitiesCmd(c *perfops.Client) error {
11+
var res *[]perfops.City
12+
13+
ctx := context.Background()
14+
u := c.BasePath + "/analytics/dns/city"
15+
16+
f := internal.NewFormatter(debug)
17+
f.StartSpinner()
18+
19+
req, _ := http.NewRequest("GET", u, nil)
20+
req = req.WithContext(ctx)
21+
22+
err := c.DoRequest(req, &res);
23+
f.StopSpinner()
24+
25+
if err != nil {
26+
return err
27+
}
28+
29+
internal.PrintOutputJSON(res)
30+
31+
return nil
32+
}

cmd/list_countries.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"github.com/ProspectOne/perfops-cli/cmd/internal"
6+
"github.com/ProspectOne/perfops-cli/perfops"
7+
"net/http"
8+
)
9+
10+
func runCountriesCmd(c *perfops.Client) error {
11+
var res *[]perfops.Country
12+
13+
ctx := context.Background()
14+
u := c.BasePath + "/analytics/dns/countries"
15+
16+
f := internal.NewFormatter(debug)
17+
f.StartSpinner()
18+
19+
req, _ := http.NewRequest("GET", u, nil)
20+
req = req.WithContext(ctx)
21+
22+
err := c.DoRequest(req, &res);
23+
f.StopSpinner()
24+
25+
if err != nil {
26+
return err
27+
}
28+
29+
internal.PrintOutputJSON(res)
30+
31+
return nil
32+
}

cmd/list_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"testing"
6+
)
7+
8+
func TestInitListCmd(t *testing.T) {
9+
parent := &cobra.Command{}
10+
listCmd.ResetFlags()
11+
initListCmd(parent)
12+
flags := listCmd.HasAvailableLocalFlags()
13+
if flags == true {
14+
t.Fatalf("expected that command doesn't have flags")
15+
}
16+
}
17+
18+
func TestRunListCmd(t *testing.T) {
19+
testCases := map[string]struct {
20+
dataType string
21+
url string
22+
expectedErr interface{}
23+
}{
24+
"Countries": {"countries", "/analytics/dns/countries", nil},
25+
"Cities": {"cities", "/analytics/dns/city", nil},
26+
"Wrong type": {"country", "/analytics/dns/country", "no data with type 'country'"},
27+
}
28+
29+
for name, tc := range testCases {
30+
t.Run(name, func(t *testing.T) {
31+
tr := &recordingTransport{}
32+
c, err := newTestPerfopsClient(tr)
33+
if err != nil {
34+
t.Fatalf("unexpected error %v", err)
35+
}
36+
37+
err = runListCmd(c, tc.dataType)
38+
39+
if tc.expectedErr != nil {
40+
if tc.expectedErr != err.Error() {
41+
t.Fatalf("expected %v error; got %v", tc.expectedErr, err)
42+
}
43+
44+
// No reason to continue if we check for error case
45+
return
46+
}
47+
48+
if got, exp := tr.req.URL.Path, tc.url; got != exp {
49+
t.Fatalf("expected %v; got %v", exp, got)
50+
}
51+
})
52+
}
53+
}

cmd/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ func Execute() error {
7979
initDNSResolveCmd(rootCmd)
8080
initCurlCmd(rootCmd)
8181
initCreditsCmd(rootCmd)
82-
initCountriesCmd(rootCmd)
83-
initCitiesCmd(rootCmd)
82+
initListCmd(rootCmd)
8483
return rootCmd.Execute()
8584
}
8685

0 commit comments

Comments
 (0)