]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
New command-line option -k/--get-config prints the value of a config key
authorGabriel Scherer <gabriel.scherer@gmail.com>
Sun, 10 Jun 2018 10:53:04 +0000 (12:53 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 14 Oct 2018 19:57:51 +0000 (21:57 +0200)
We print exactly the value of the configuration setting, rather
than a full item pair "(origin) key = value" as --print-config does.

This is useful for scripting. For example, $(ccache -k cache_dir)
gives the cache directory path.

src/ccache.c
src/conf.c
src/conf.h
unittest/test_conf.c

index 0665582fd1382739a22344780d0fd01b1e46530d..1a9ba146f4da9211457ccf549b8c743a58ad8fb0 100644 (file)
@@ -66,6 +66,7 @@ static const char USAGE_TEXT[] =
        "    -C, --clear           clear the cache completely (except configuration)\n"
        "    -F, --max-files=N     set maximum number of files in cache to N (use 0 for\n"
        "                          no limit)\n"
+        "    -k, --get-config=K    get the value of the configuration key K\n"
        "    -M, --max-size=SIZE   set maximum size of cache to SIZE (use 0 for no\n"
        "                          limit); available suffixes: k, M, G, T (decimal) and\n"
        "                          Ki, Mi, Gi, Ti (binary); default suffix: G\n"
@@ -3544,6 +3545,7 @@ ccache_main_options(int argc, char *argv[])
                {"cleanup",       no_argument,       0, 'c'},
                {"clear",         no_argument,       0, 'C'},
                {"dump-manifest", required_argument, 0, DUMP_MANIFEST},
+               {"get-config",    required_argument, 0, 'k'},
                {"hash-file",     required_argument, 0, HASH_FILE},
                {"help",          no_argument,       0, 'h'},
                {"max-files",     required_argument, 0, 'F'},
@@ -3557,7 +3559,8 @@ ccache_main_options(int argc, char *argv[])
        };
 
        int c;
-       while ((c = getopt_long(argc, argv, "cChF:M:o:psVz", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "cCk:hF:M:o:psVz", options, NULL))
+               != -1) {
                switch (c) {
                case DUMP_MANIFEST:
                        manifest_dump(optarg, stdout);
@@ -3595,6 +3598,16 @@ ccache_main_options(int argc, char *argv[])
                        fputs(USAGE_TEXT, stdout);
                        x_exit(0);
 
+               case 'k': // --get-config
+               {
+                       initialize();
+                       char *errmsg;
+                       if (!conf_print_value(conf, optarg, stdout, &errmsg)) {
+                               fatal("%s", errmsg);
+                       }
+               }
+               break;
+
                case 'F': // --max-files
                {
                        initialize();
index 2bf96bcb4de56c21477628f85e1fbbc61e8ee0b1..404d2d7f7a5871cda37807335e03fde5a2838619 100644 (file)
@@ -629,6 +629,22 @@ conf_set_value_in_file(const char *path, const char *key, const char *value,
        return true;
 }
 
+bool
+conf_print_value(struct conf *conf, const char *key,
+                 FILE *file, char **errmsg)
+{
+       const struct conf_item *item = find_conf(key);
+       if (!item) {
+               *errmsg = format("unknown configuration option \"%s\"", key);
+               return false;
+       }
+       void *value = (char *)conf + item->offset;
+       char *str = (char *)item->formatter(value);
+       fprintf(file, "%s\n", str);
+       free(str);
+       return true;
+}
+
 static bool
 print_item(struct conf *conf, const char *key,
            void (*printer)(const char *descr, const char *origin,
index b0ace1c297eeb9e7aece27da15249d179dcba0d2..9c9e28f42cf02e98de4be994daac39a27a9a6b27 100644 (file)
@@ -45,6 +45,8 @@ struct conf *conf_create(void);
 void conf_free(struct conf *conf);
 bool conf_read(struct conf *conf, const char *path, char **errmsg);
 bool conf_update_from_environment(struct conf *conf, char **errmsg);
+bool conf_print_value(struct conf *conf, const char *key,
+                      FILE *file, char **errmsg);
 bool conf_set_value_in_file(const char *path, const char *key,
                             const char *value, char **errmsg);
 bool conf_print_items(struct conf *conf,
index 2028ddc8c14ffce91a1f6cf31b8d855842dd4e2b..f3d63da98a37e1d38cb2d045929fc8c0297aedfd 100644 (file)
@@ -383,6 +383,50 @@ TEST(conf_set_existing_value)
        CHECK_STR_EQ_FREE2("path = vanilla\nstats = chocolate\n", data);
 }
 
+TEST(conf_print_existing_value)
+{
+       struct conf *conf = conf_create();
+       conf->max_files = 42;
+       char *errmsg;
+       {
+               FILE *log = fopen("log", "w");
+               CHECK(log);
+               CHECK(conf_print_value(conf, "max_files", log, &errmsg));
+               fclose(log);
+       }
+       {
+               FILE *log = fopen("log", "r");
+               CHECK(log);
+               char buf[100];
+               CHECK(fgets(buf, 100, log));
+               CHECK_STR_EQ("42\n", buf);
+               fclose(log);
+       }
+       conf_free(conf);
+}
+
+TEST(conf_print_unknown_value)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+       {
+               FILE *log = fopen("log", "w");
+               CHECK(log);
+               CHECK(!conf_print_value(conf, "foo", log, &errmsg));
+               CHECK_STR_EQ_FREE2("unknown configuration option \"foo\"",
+                                  errmsg);
+               fclose(log);
+       }
+       {
+               FILE *log = fopen("log", "r");
+               CHECK(log);
+               char buf[100];
+               CHECK(!fgets(buf, 100, log));
+               fclose(log);
+       }
+       conf_free(conf);
+}
+
 TEST(conf_print_items)
 {
        size_t i;