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 time_t opt_expire
= 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 "memrm"
32 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached 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_delete(memc
, argv
[optind
], strlen(argv
[optind
]), opt_expire
);
93 if (rc
== MEMCACHED_NOTFOUND
)
97 std::cerr
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
100 else if (memcached_failed(rc
))
104 std::cerr
<< "Failed to delete key \"" << argv
[optind
] << "\" :" << memcached_last_error_message(memc
) << std::endl
;
107 return_code
= EXIT_FAILURE
;
113 std::cout
<< "Deleted key " << argv
[optind
];
116 std::cout
<< " expires: " << opt_expire
<< std::endl
;
118 std::cout
<< std::endl
;
125 memcached_free(memc
);
141 static void options_parse(int argc
, char *argv
[])
143 memcached_programs_help_st help_options
[]=
148 static struct option long_options
[]=
150 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
151 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
152 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
153 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
154 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
155 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
156 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
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
);
205 case OPT_EXPIRE
: /* --expire */
206 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
210 opt_hash
= strdup(optarg
);
214 opt_username
= optarg
;
226 /* getopt_long already printed an error message. */
236 version_command(PROGRAM_NAME
);
242 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);