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_username
;
28 static char *opt_passwd
;
30 #define PROGRAM_NAME "memflush"
31 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
34 void options_parse(int argc
, char *argv
[]);
36 int main(int argc
, char *argv
[])
38 options_parse(argc
, argv
);
40 if (opt_servers
== false)
44 if ((temp
= getenv("MEMCACHED_SERVERS")))
46 opt_servers
= strdup(temp
);
50 std::cerr
<< "No Servers provided" << std::endl
;
55 memcached_st
*memc
= memcached_create(NULL
);
57 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
58 memcached_server_push(memc
, servers
);
59 memcached_server_list_free(servers
);
60 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
61 (uint64_t) opt_binary
);
63 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
66 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
72 memcached_return_t ret
;
73 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
75 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
81 memcached_return_t rc
= memcached_flush(memc
, opt_expire
);
82 if (rc
!= MEMCACHED_SUCCESS
)
84 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
95 void options_parse(int argc
, char *argv
[])
97 static struct option long_options
[]=
99 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
100 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
101 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
102 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
103 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
104 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
105 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
106 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
107 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
108 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
112 bool opt_version
= false;
113 bool opt_help
= false;
117 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
118 if (option_rv
== -1) break;
128 case OPT_VERBOSE
: /* --verbose or -v */
129 opt_verbose
= OPT_VERBOSE
;
132 case OPT_DEBUG
: /* --debug or -d */
133 opt_verbose
= OPT_DEBUG
;
136 case OPT_VERSION
: /* --version or -V */
140 case OPT_HELP
: /* --help or -h */
144 case OPT_SERVERS
: /* --servers or -s */
145 opt_servers
= strdup(optarg
);
148 case OPT_EXPIRE
: /* --expire */
149 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
153 opt_username
= optarg
;
165 /* getopt_long already printed an error message. */
175 version_command(PROGRAM_NAME
);
181 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);