2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
12 #include "mem_config.h"
20 #include <libmemcached-1.0/memcached.h>
21 #include "client_options.h"
22 #include "utilities.h"
24 static int opt_binary
= 0;
25 static int opt_verbose
= 0;
26 static char *opt_servers
= NULL
;
27 static char *opt_hash
= NULL
;
28 static char *opt_username
;
29 static char *opt_passwd
;
31 #define PROGRAM_NAME "memexist"
32 #define PROGRAM_DESCRIPTION "Check for the existance of a key within a cluster."
35 static void options_parse(int argc
, char *argv
[]);
37 int main(int argc
, char *argv
[])
40 memcached_server_st
*servers
;
42 options_parse(argc
, argv
);
49 if ((temp
= getenv("MEMCACHED_SERVERS")))
51 opt_servers
= strdup(temp
);
55 std::cerr
<< "No Servers provided" << std::endl
;
60 memc
= memcached_create(NULL
);
61 process_hash_option(memc
, opt_hash
);
63 servers
= memcached_servers_parse(opt_servers
);
64 memcached_server_push(memc
, servers
);
65 memcached_server_list_free(servers
);
66 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
67 (uint64_t) opt_binary
);
69 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
72 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
78 memcached_return_t ret
;
79 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
81 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
87 int return_code
= EXIT_SUCCESS
;
91 memcached_return_t rc
= memcached_exist(memc
, argv
[optind
], strlen(argv
[optind
]));
93 if (rc
== MEMCACHED_NOTFOUND
)
97 std::cout
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
100 return_code
= EXIT_FAILURE
;
102 else if (memcached_failed(rc
))
106 std::cerr
<< "Fatal error for key \"" << argv
[optind
] << "\" :" << memcached_last_error_message(memc
) << std::endl
;
109 return_code
= EXIT_FAILURE
;
115 std::cout
<< "Found key " << argv
[optind
] << std::endl
;
122 memcached_free(memc
);
138 static void options_parse(int argc
, char *argv
[])
140 memcached_programs_help_st help_options
[]=
145 static struct option long_options
[]=
147 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
148 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
149 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
150 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
151 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
152 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
153 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
154 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
155 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
156 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
160 bool opt_version
= false;
161 bool opt_help
= false;
166 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
181 case OPT_VERBOSE
: /* --verbose or -v */
182 opt_verbose
= OPT_VERBOSE
;
185 case OPT_DEBUG
: /* --debug or -d */
186 opt_verbose
= OPT_DEBUG
;
189 case OPT_VERSION
: /* --version or -V */
193 case OPT_HELP
: /* --help or -h */
197 case OPT_SERVERS
: /* --servers or -s */
198 opt_servers
= strdup(optarg
);
202 opt_hash
= strdup(optarg
);
206 opt_username
= optarg
;
218 /* getopt_long already printed an error message. */
228 version_command(PROGRAM_NAME
);
234 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);