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
[])
39 options_parse(argc
, argv
);
42 if (opt_servers
== NULL
)
46 if ((temp
= getenv("MEMCACHED_SERVERS")))
48 opt_servers
= strdup(temp
);
51 if (opt_servers
== NULL
)
53 std::cerr
<< "No Servers provided" << std::endl
;
58 memcached_server_st
* servers
= memcached_servers_parse(opt_servers
);
59 if (servers
== NULL
or memcached_server_list_count(servers
) == 0)
61 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
65 memcached_st
* memc
= memcached_create(NULL
);
66 process_hash_option(memc
, opt_hash
);
68 memcached_server_push(memc
, servers
);
69 memcached_server_list_free(servers
);
70 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
71 (uint64_t) opt_binary
);
73 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
76 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
82 memcached_return_t ret
;
83 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
85 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
91 int return_code
= EXIT_SUCCESS
;
95 memcached_return_t rc
= memcached_exist(memc
, argv
[optind
], strlen(argv
[optind
]));
97 if (rc
== MEMCACHED_NOTFOUND
)
101 std::cout
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
104 return_code
= EXIT_FAILURE
;
106 else if (memcached_failed(rc
))
110 std::cerr
<< "Fatal error for key \"" << argv
[optind
] << "\" :" << memcached_last_error_message(memc
) << std::endl
;
113 return_code
= EXIT_FAILURE
;
119 std::cout
<< "Found key " << argv
[optind
] << std::endl
;
126 memcached_free(memc
);
142 static void options_parse(int argc
, char *argv
[])
144 memcached_programs_help_st help_options
[]=
149 static struct option long_options
[]=
151 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
152 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
153 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
154 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
155 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
156 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
157 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
158 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
159 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
160 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
164 bool opt_version
= false;
165 bool opt_help
= false;
170 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
185 case OPT_VERBOSE
: /* --verbose or -v */
186 opt_verbose
= OPT_VERBOSE
;
189 case OPT_DEBUG
: /* --debug or -d */
190 opt_verbose
= OPT_DEBUG
;
193 case OPT_VERSION
: /* --version or -V */
197 case OPT_HELP
: /* --help or -h */
201 case OPT_SERVERS
: /* --servers or -s */
202 opt_servers
= strdup(optarg
);
206 opt_hash
= strdup(optarg
);
210 opt_username
= optarg
;
222 /* getopt_long already printed an error message. */
232 version_command(PROGRAM_NAME
);
238 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);