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 time_t opt_expire
= 0;
27 static char *opt_servers
= NULL
;
28 static char *opt_hash
= NULL
;
29 static char *opt_username
;
30 static char *opt_passwd
;
32 #define PROGRAM_NAME "memrm"
33 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
36 static void options_parse(int argc
, char *argv
[]);
38 int main(int argc
, char *argv
[])
41 memcached_server_st
*servers
;
43 options_parse(argc
, argv
);
50 if ((temp
= getenv("MEMCACHED_SERVERS")))
52 opt_servers
= strdup(temp
);
56 std::cerr
<< "No Servers provided" << std::endl
;
61 memc
= memcached_create(NULL
);
62 process_hash_option(memc
, opt_hash
);
64 servers
= memcached_servers_parse(opt_servers
);
65 memcached_server_push(memc
, servers
);
66 memcached_server_list_free(servers
);
67 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
68 (uint64_t) opt_binary
);
70 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
73 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
79 memcached_return_t ret
;
80 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
82 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
88 int return_code
= EXIT_SUCCESS
;
92 memcached_return_t rc
= memcached_delete(memc
, argv
[optind
], strlen(argv
[optind
]), opt_expire
);
94 if (rc
== MEMCACHED_NOTFOUND
)
98 std::cerr
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
101 else if (memcached_fatal(rc
))
105 std::cerr
<< "Failed to delete key \"" << argv
[optind
] << "\" :" << memcached_last_error_message(memc
) << std::endl
;
108 return_code
= EXIT_FAILURE
;
114 std::cout
<< "Deleted key " << argv
[optind
];
117 std::cout
<< " expires: " << opt_expire
<< std::endl
;
119 std::cout
<< 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
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
158 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
159 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
160 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
161 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
165 bool opt_version
= false;
166 bool opt_help
= false;
171 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
186 case OPT_VERBOSE
: /* --verbose or -v */
187 opt_verbose
= OPT_VERBOSE
;
190 case OPT_DEBUG
: /* --debug or -d */
191 opt_verbose
= OPT_DEBUG
;
194 case OPT_VERSION
: /* --version or -V */
198 case OPT_HELP
: /* --help or -h */
202 case OPT_SERVERS
: /* --servers or -s */
203 opt_servers
= strdup(optarg
);
206 case OPT_EXPIRE
: /* --expire */
207 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
211 opt_hash
= strdup(optarg
);
215 opt_username
= optarg
;
227 /* getopt_long already printed an error message. */
237 version_command(PROGRAM_NAME
);
243 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);