| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#include "consul.h" |
| 6 |
|
| 7 |
int main(int argc, char* argv[]) { |
| 8 |
if (argc < 3) { |
| 9 |
printf("Usage: consul_cli subcmd ServiceName [ServiceAddress ServicePort] [NodeIP NodePort]\n"); |
| 10 |
printf("subcmd=[register,deregister,discover]\n"); |
| 11 |
return -10; |
| 12 |
} |
| 13 |
const char* subcmd = argv[1]; |
| 14 |
const char* ServiceName = argv[2]; |
| 15 |
const char* ServiceAddress = "127.0.0.1"; |
| 16 |
int ServicePort = 0; |
| 17 |
const char* NodeIP = "127.0.0.1"; |
| 18 |
int NodePort = 8500; |
| 19 |
if (argc > 3) { |
| 20 |
ServiceAddress = argv[3]; |
| 21 |
} |
| 22 |
if (argc > 4) { |
| 23 |
ServicePort = atoi(argv[4]); |
| 24 |
} |
| 25 |
if (argc > 5) { |
| 26 |
NodeIP = argv[5]; |
| 27 |
} |
| 28 |
if (argc > 6) { |
| 29 |
NodePort = atoi(argv[6]); |
| 30 |
} |
| 31 |
|
| 32 |
consul_node_t node; |
| 33 |
strncpy(node.ip, NodeIP, sizeof(node.ip)); |
| 34 |
node.port = NodePort; |
| 35 |
|
| 36 |
consul_service_t service; |
| 37 |
strncpy(service.name, ServiceName, sizeof(service.name)); |
| 38 |
strncpy(service.ip, ServiceAddress, sizeof(service.ip)); |
| 39 |
service.port = ServicePort; |
| 40 |
|
| 41 |
consul_health_t health; |
| 42 |
|
| 43 |
if (strcmp(subcmd, "register") == 0) { |
| 44 |
int ret = register_service(&node, &service, &health); |
| 45 |
printf("register_service retval=%d\n", ret); |
| 46 |
goto discover; |
| 47 |
} |
| 48 |
else if (strcmp(subcmd, "deregister") == 0) { |
| 49 |
int ret = deregister_service(&node, &service); |
| 50 |
printf("deregister_service retval=%d\n", ret); |
| 51 |
goto discover; |
| 52 |
} |
| 53 |
else if (strcmp(subcmd, "discover") == 0) { |
| 54 |
discover: |
| 55 |
std::vector<consul_service_t> services; |
| 56 |
discover_services(&node, ServiceName, services); |
| 57 |
for (auto& service : services) { |
| 58 |
printf("name=%s ip=%s port=%d\n", service.name, service.ip, service.port); |
| 59 |
} |
| 60 |
} |
| 61 |
else { |
| 62 |
printf("subcmd error!\n"); |
| 63 |
return -20; |
| 64 |
} |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |