2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
19 #include <libmemcached/memcached.h>
20 #include "client_options.h"
21 #include "utilities.h"
23 static int opt_binary
= 0;
24 static int opt_verbose
= 0;
25 static char *opt_servers
= NULL
;
26 static char *opt_hash
= NULL
;
27 static char *opt_username
;
28 static char *opt_passwd
;
30 #define PROGRAM_NAME "memexist"
31 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
34 static void options_parse(int argc
, char *argv
[]);
36 int main(int argc
, char *argv
[])
39 memcached_server_st
*servers
;
41 options_parse(argc
, argv
);
48 if ((temp
= getenv("MEMCACHED_SERVERS")))
50 opt_servers
= strdup(temp
);
54 std::cerr
<< "No Servers provided" << std::endl
;
59 memc
= memcached_create(NULL
);
60 process_hash_option(memc
, opt_hash
);
62 servers
= memcached_servers_parse(opt_servers
);
63 memcached_server_push(memc
, servers
);
64 memcached_server_list_free(servers
);
65 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
66 (uint64_t) opt_binary
);
68 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
71 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
77 memcached_return_t ret
;
78 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
80 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
86 int return_code
= EXIT_SUCCESS
;
90 memcached_return_t rc
= memcached_exist(memc
, argv
[optind
], strlen(argv
[optind
]));
92 if (rc
== MEMCACHED_NOTFOUND
)
96 std::cout
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
99 return_code
= EXIT_FAILURE
;
101 else if (memcached_failed(rc
))
105 std::cerr
<< "Fatal error for key \"" << argv
[optind
] << "\" :" << memcached_last_error_message(memc
) << std::endl
;
108 return_code
= EXIT_FAILURE
;
114 std::cout
<< "Found key " << argv
[optind
] << std::endl
;
121 memcached_free(memc
);
137 static void options_parse(int argc
, char *argv
[])
139 memcached_programs_help_st help_options
[]=
144 static struct option long_options
[]=
146 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
147 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
148 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
149 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
150 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
151 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
152 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
153 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
154 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
155 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
159 bool opt_version
= false;
160 bool opt_help
= false;
165 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
180 case OPT_VERBOSE
: /* --verbose or -v */
181 opt_verbose
= OPT_VERBOSE
;
184 case OPT_DEBUG
: /* --debug or -d */
185 opt_verbose
= OPT_DEBUG
;
188 case OPT_VERSION
: /* --version or -V */
192 case OPT_HELP
: /* --help or -h */
196 case OPT_SERVERS
: /* --servers or -s */
197 opt_servers
= strdup(optarg
);
201 opt_hash
= strdup(optarg
);
205 opt_username
= optarg
;
217 /* getopt_long already printed an error message. */
227 version_command(PROGRAM_NAME
);
233 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);