1
1
package cmd
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"net/url"
6
7
@@ -20,43 +21,11 @@ can connect to Kong's Admin API.`,
20
21
Args : validateNoArgs ,
21
22
RunE : func (cmd * cobra.Command , args []string ) error {
22
23
ctx := cmd .Context ()
23
- cmdStr := "ping"
24
- var version string
25
- var err error
26
24
mode := getMode (nil )
27
25
if mode == modeKonnect {
28
- // get Konnect client
29
- httpClient := utils .HTTPClient ()
30
- konnectClient , err := utils .GetKonnectClient (httpClient , konnectConfig )
31
- if err != nil {
32
- return err
33
- }
34
- u , _ := url .Parse (konnectConfig .Address )
35
- // authenticate with konnect
36
- res , err := authenticate (ctx , konnectClient , u .Host )
37
- if err != nil {
38
- return fmt .Errorf ("authenticating with Konnect: %w" , err )
39
- }
40
- fullName := res .FullName
41
- if res .FullName == "" {
42
- fullName = fmt .Sprintf ("%s %s" , res .FirstName , res .LastName )
43
- }
44
- fmt .Printf ("Successfully Konnected as %s (%s)!\n " ,
45
- fullName , res .Organization )
46
- if konnectConfig .Debug {
47
- fmt .Printf ("Organization ID: %s\n " , res .OrganizationID )
48
- }
49
- } else {
50
- wsConfig := rootConfig .ForWorkspace (pingWorkspace )
51
- version , err = fetchKongVersion (ctx , wsConfig )
52
- if err != nil {
53
- return fmt .Errorf ("reading Kong version: %w" , err )
54
- }
55
- fmt .Println ("Successfully connected to Kong!" )
56
- fmt .Println ("Kong version: " , version )
26
+ return pingKonnect (ctx )
57
27
}
58
- _ = sendAnalytics (cmdStr , version , mode )
59
- return nil
28
+ return pingKong (ctx )
60
29
},
61
30
}
62
31
@@ -66,3 +35,41 @@ can connect to Kong's Admin API.`,
66
35
"Useful when RBAC permissions are scoped to a Workspace." )
67
36
return pingCmd
68
37
}
38
+
39
+ func pingKonnect (ctx context.Context ) error {
40
+ // get Konnect client
41
+ httpClient := utils .HTTPClient ()
42
+ konnectClient , err := utils .GetKonnectClient (httpClient , konnectConfig )
43
+ if err != nil {
44
+ return err
45
+ }
46
+ u , _ := url .Parse (konnectConfig .Address )
47
+ // authenticate with konnect
48
+ res , err := authenticate (ctx , konnectClient , u .Host )
49
+ if err != nil {
50
+ return fmt .Errorf ("authenticating with Konnect: %w" , err )
51
+ }
52
+ fullName := res .FullName
53
+ if res .FullName == "" {
54
+ fullName = fmt .Sprintf ("%s %s" , res .FirstName , res .LastName )
55
+ }
56
+ fmt .Printf ("Successfully Konnected as %s (%s)!\n " ,
57
+ fullName , res .Organization )
58
+ if konnectConfig .Debug {
59
+ fmt .Printf ("Organization ID: %s\n " , res .OrganizationID )
60
+ }
61
+ _ = sendAnalytics ("ping" , "" , modeKonnect )
62
+ return nil
63
+ }
64
+
65
+ func pingKong (ctx context.Context ) error {
66
+ wsConfig := rootConfig .ForWorkspace (pingWorkspace )
67
+ version , err := fetchKongVersion (ctx , wsConfig )
68
+ if err != nil {
69
+ return fmt .Errorf ("reading Kong version: %w" , err )
70
+ }
71
+ fmt .Println ("Successfully connected to Kong!" )
72
+ fmt .Println ("Kong version: " , version )
73
+ _ = sendAnalytics ("ping" , version , modeKong )
74
+ return nil
75
+ }
0 commit comments