changelog
[awesomized/libmemcached] / src / bin / memflush.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #define PROGRAM_NAME "memflush"
19 #define PROGRAM_DESCRIPTION "Erase all data in a cluster of memcached servers."
20 #define PROGRAM_VERSION "1.1"
21
22 #include "common/options.hpp"
23 #include "common/checks.hpp"
24
25 int main(int argc, char *argv[]) {
26 client_options opt{PROGRAM_NAME, PROGRAM_VERSION, PROGRAM_DESCRIPTION};
27
28 for (const auto &def : opt.defaults) {
29 switch (def.opt.val) {
30 case 'H': // no need for --hash
31 break;
32 default:
33 opt.add(def);
34 break;
35 }
36 }
37
38 opt.add("expire", 'e', required_argument, "Flush based on expire time.");
39
40 if (!opt.parse(argc, argv, nullptr)) {
41 exit(EXIT_FAILURE);
42 }
43
44 memcached_st memc;
45 if (!check_memcached(opt, memc)) {
46 exit(EXIT_FAILURE);
47 }
48
49 if (!opt.apply(&memc)) {
50 memcached_free(&memc);
51 exit(EXIT_FAILURE);
52 }
53
54 time_t expire = 0;
55 if (auto exp_str = opt.argof("expire")) {
56 expire = std::stoul(exp_str);
57 }
58
59 auto exit_code = EXIT_SUCCESS;
60 memcached_return_t rc = memcached_flush(&memc, expire);
61 if (!memcached_success(rc)) {
62 exit_code = EXIT_FAILURE;
63 if (!opt.isset("quiet")) {
64 std::cerr << "Failed to flush server: " << memcached_last_error_message(&memc) << "\n";
65 }
66 }
67
68 if (!check_buffering(opt, memc)) {
69 exit_code = EXIT_FAILURE;
70 }
71
72 memcached_free(&memc);
73 exit(exit_code);
74 }