@@ -24,30 +24,89 @@ func getAppknoxAccessToken() string {
24
24
return accessToken
25
25
}
26
26
27
+ // GetHostMappings returns a map of host names to URLs.
28
+ func GetHostMappings () map [string ]string {
29
+ return map [string ]string {
30
+ "global" : "https://api.appknox.com/" ,
31
+ "saudi" : "https://sa.secure.appknox.com/" ,
32
+ // Add more mappings as needed
33
+ }
34
+ }
35
+
36
+ // ResolveHostAndRegion checks the host and region and returns the resolved base URL
37
+ func ResolveHostAndRegion (host , region string , hostMappings map [string ]string ) (string , error ) {
38
+ // If both region and host are provided, prioritize host and ignore region
39
+ if host != "" {
40
+ fmt .Printf ("Both region and host provided. Using host URL: %s, ignoring region\n " , host )
41
+ // Validate the host is a proper URL
42
+ _ , err := url .ParseRequestURI (host )
43
+ if err != nil {
44
+ return "" , fmt .Errorf ("invalid host URL: %s" , host )
45
+ }
46
+ return host , nil
47
+ }
48
+
49
+ // If region is provided, map it to the host URL
50
+ if region != "" {
51
+ if mappedHost , exists := hostMappings [region ]; exists {
52
+ return mappedHost , nil
53
+ }
54
+ // Invalid region, return error and show available regions
55
+ availableRegions := make ([]string , 0 , len (hostMappings ))
56
+ for key := range hostMappings {
57
+ availableRegions = append (availableRegions , key )
58
+ }
59
+ return "" , fmt .Errorf ("Invalid region name: %s. Available regions: %s" , region , strings .Join (availableRegions , ", " ))
60
+ }
61
+
62
+ // If neither host nor region are provided, default to the global host
63
+ return hostMappings ["global" ], nil
64
+ }
65
+
66
+
27
67
func getClient () * appknox.Client {
28
- token := getAppknoxAccessToken ()
29
- host := viper .GetString ("host" )
30
- client , err := appknox .NewClient (token )
31
- if err != nil {
32
- fmt .Println (err )
33
- os .Exit (1 )
34
- }
35
- proxyURL , err := GetProxy ()
36
- if err != nil {
37
- fmt .Println (err )
38
- os .Exit (1 )
39
- }
40
- insecure := viper .GetBool ("insecure" )
41
- client = client .SetHTTPTransportParams (proxyURL , insecure )
42
- baseHost , err := url .Parse (host )
68
+ token := getAppknoxAccessToken ()
69
+
70
+ // Check for region and host first
71
+ region := viper .GetString ("region" )
72
+ host := viper .GetString ("host" )
73
+
74
+ // Get the host mappings
75
+ hostMappings := GetHostMappings ()
76
+
77
+ // Use the new function to resolve the host and region
78
+ resolvedHost , err := ResolveHostAndRegion (host , region , hostMappings )
43
79
if err != nil {
44
80
fmt .Println (err )
45
81
os .Exit (1 )
46
82
}
47
- client .BaseURL = baseHost
48
- return client
83
+
84
+ client , err := appknox .NewClient (token )
85
+ if err != nil {
86
+ fmt .Println (err )
87
+ os .Exit (1 )
88
+ }
89
+
90
+ proxyURL , err := GetProxy ()
91
+ if err != nil {
92
+ fmt .Println (err )
93
+ os .Exit (1 )
94
+ }
95
+
96
+ insecure := viper .GetBool ("insecure" )
97
+ client = client .SetHTTPTransportParams (proxyURL , insecure )
98
+
99
+ baseHost , err := url .Parse (resolvedHost )
100
+ if err != nil {
101
+ fmt .Println (err )
102
+ os .Exit (1 )
103
+ }
104
+ client .BaseURL = baseHost
105
+ return client
49
106
}
50
107
108
+
109
+
51
110
// CheckToken checks if access token is valid.
52
111
func CheckToken () (* appknox.Me , error ) {
53
112
return GetMe ()
0 commit comments