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"
21 #include <libmemcached-1.0/memcached.h>
22 #include "client_options.h"
23 #include "utilities.h"
25 static int opt_binary
= 0;
26 static int opt_verbose
= 0;
27 static time_t opt_expire
= 0;
28 static char *opt_servers
= NULL
;
29 static char *opt_hash
= NULL
;
30 static char *opt_username
;
31 static char *opt_passwd
;
33 #define PROGRAM_NAME "memrm"
34 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
37 static void options_parse(int argc
, char *argv
[]);
39 int main(int argc
, char *argv
[])
41 options_parse(argc
, argv
);
44 if (opt_servers
== NULL
)
48 if ((temp
= getenv("MEMCACHED_SERVERS")))
50 opt_servers
= strdup(temp
);
53 if (opt_servers
== NULL
)
55 std::cerr
<< "No Servers provided" << std::endl
;
60 memcached_server_st
* servers
= memcached_servers_parse(opt_servers
);
61 if (servers
== NULL
or memcached_server_list_count(servers
) == 0)
63 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
67 memcached_st
* memc
= memcached_create(NULL
);
68 process_hash_option(memc
, opt_hash
);
70 memcached_server_push(memc
, servers
);
71 memcached_server_list_free(servers
);
72 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
73 (uint64_t) opt_binary
);
75 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
78 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
84 memcached_return_t ret
;
85 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
87 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
93 int return_code
= EXIT_SUCCESS
;
97 memcached_return_t rc
= memcached_delete(memc
, argv
[optind
], strlen(argv
[optind
]), opt_expire
);
99 if (rc
== MEMCACHED_NOTFOUND
)
103 std::cerr
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
106 else if (memcached_fatal(rc
))
110 std::cerr
<< "Failed to delete key \"" << argv
[optind
] << "\" :" << memcached_last_error_message(memc
) << std::endl
;
113 return_code
= EXIT_FAILURE
;
119 std::cout
<< "Deleted key " << argv
[optind
];
122 std::cout
<< " expires: " << opt_expire
<< std::endl
;
124 std::cout
<< std::endl
;
131 memcached_free(memc
);
147 static void options_parse(int argc
, char *argv
[])
149 memcached_programs_help_st help_options
[]=
154 static struct option long_options
[]=
156 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
157 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
158 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
159 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
160 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
161 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
162 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
163 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
164 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
165 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
166 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
170 bool opt_version
= false;
171 bool opt_help
= false;
176 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
191 case OPT_VERBOSE
: /* --verbose or -v */
192 opt_verbose
= OPT_VERBOSE
;
195 case OPT_DEBUG
: /* --debug or -d */
196 opt_verbose
= OPT_DEBUG
;
199 case OPT_VERSION
: /* --version or -V */
203 case OPT_HELP
: /* --help or -h */
207 case OPT_SERVERS
: /* --servers or -s */
208 opt_servers
= strdup(optarg
);
211 case OPT_EXPIRE
: /* --expire */
213 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
216 std::cerr
<< "Incorrect value passed to --expire: `" << optarg
<< "`" << std::endl
;
222 opt_hash
= strdup(optarg
);
226 opt_username
= optarg
;
238 /* getopt_long already printed an error message. */
248 version_command(PROGRAM_NAME
);
254 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);