55757b8a6b7dff32d7f864d9f6befb172e62ed6d
[awesomized/libmemcached] / src / bin / memexist.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - 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 "memexist"
19 #define PROGRAM_DESCRIPTION "Check for the existence of a key within a memcached cluster."
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, "key [key ...]"};
27
28 for (const auto &def : opt.defaults) {
29 opt.add(def);
30 }
31
32 char **argp = nullptr;
33 if (!opt.parse(argc, argv, &argp)) {
34 exit(EXIT_FAILURE);
35 }
36
37 memcached_st memc;
38 if (!check_memcached(opt, memc)) {
39 exit(EXIT_FAILURE);
40 }
41
42 if (!opt.apply(&memc)) {
43 memcached_free(&memc);
44 exit(EXIT_FAILURE);
45 }
46
47 if (!check_argp(opt, argp, "No key(s) provided.")) {
48 memcached_free(&memc);
49 exit(EXIT_FAILURE);
50 }
51
52 auto exit_code = EXIT_SUCCESS;
53 for (auto arg = argp; *arg; ++arg) {
54 auto key = *arg;
55 if (*key) {
56 if (check_return(opt, memc, key, memcached_exist(&memc, *arg, strlen(*arg)))) {
57 if (opt.isset("verbose")) {
58 std::cerr << "Found key '" << *arg << "'.\n";
59 }
60 } else {
61 exit_code = EXIT_FAILURE;
62 }
63 }
64 }
65
66 memcached_free(&memc);
67 exit(exit_code);
68 }