[Groonga-commit] groonga/groonga [master] --config-path option

Back to archive index

null+****@clear***** null+****@clear*****
2010年 6月 25日 (金) 14:22:57 JST


Nobuyoshi Nakada	2010-06-25 05:22:57 +0000 (Fri, 25 Jun 2010)

  New Revision: 7e2a183bd7200a4c8fdc6d5059990e4aabf35417

  Log:
    --config-path option

  Modified files:
    src/groonga.c
    test/unit/command/test-option.rb
    test/unit/lib/ruby/groonga-test-utils.rb

  Modified: src/groonga.c (+79 -12)
===================================================================
--- src/groonga.c    2010-07-02 06:57:41 +0000 (01dadac)
+++ src/groonga.c    2010-06-25 05:22:57 +0000 (5271eb2)
@@ -97,6 +97,7 @@ usage(FILE *output)
           "  --log-path <path>:                specify log path\n"
           "  --query-log-path <path>:          specify query log path\n"
           "  --pid-file <path>:                specify pid file path (daemon mode only)\n"
+          "  --config-path <path>:             specify config file path\n"
           "\n"
           "dest: <db pathname> [<command>] or <dest hostname>\n"
           "  <db pathname> [<command>]: when standalone/server mode\n"
@@ -1586,6 +1587,7 @@ enum {
   mode_server,
   mode_usage,
   mode_version,
+  mode_config,
   mode_error
 };
 
@@ -1626,7 +1628,7 @@ load_config_file(const char *path,
                  const grn_str_getopt_opt *opts, int *flags)
 {
   int name_len, value_len;
-  char buf[1024+2], *str, *name, *value, *args[4];
+  char buf[1024+2], *str, *name, *value = NULL, *args[4];
   FILE *file;
 
   if (!(file = fopen(path, "r"))) return 0;
@@ -1667,6 +1669,42 @@ load_config_file(const char *path,
   return 1;
 }
 
+static void
+show_config(FILE *out, const grn_str_getopt_opt *opts, int flags)
+{
+  const grn_str_getopt_opt *o;
+
+  for (o = opts; o->opt != '\0' || o->longopt != NULL; o++) {
+    switch (o->op) {
+    case getopt_op_none:
+      if (o->arg && *o->arg) {
+        if (o->longopt) {
+          fprintf(out, "%s=%s\n", o->longopt, *o->arg);
+        }
+      }
+      break;
+    case getopt_op_on:
+      if (flags & o->flag) {
+        goto no_arg;
+      }
+      break;
+    case getopt_op_off:
+      if (!(flags & o->flag)) {
+        goto no_arg;
+      }
+      break;
+    case getopt_op_update:
+      if (flags == o->flag) {
+      no_arg:
+        if (o->longopt) {
+          fprintf(out, "%s\n", o->longopt);
+        }
+      }
+      break;
+    }
+  }
+}
+
 int
 main(int argc, char **argv)
 {
@@ -1674,6 +1712,7 @@ main(int argc, char **argv)
   const char *portstr = NULL, *encstr = NULL,
     *max_nfthreadsstr = NULL, *loglevel = NULL,
     *listen_addressstr = NULL, *hostnamestr = NULL, *protocol = NULL;
+  const char *config_path = NULL;
   int r, i, mode = mode_alone;
   static grn_str_getopt_opt opts[] = {
     {'p', "port", NULL, 0, getopt_op_none},
@@ -1694,6 +1733,8 @@ main(int argc, char **argv)
     {'\0', "log-path", NULL, 0, getopt_op_none},
     {'\0', "query-log-path", NULL, 0, getopt_op_none},
     {'\0', "pid-file", NULL, 0, getopt_op_none},
+    {'\0', "config-path", NULL, 0, getopt_op_none},
+    {'\0', "show-config", NULL, mode_config, getopt_op_update},
     {'\0', NULL, NULL, 0, 0}
   };
   opts[0].arg = &portstr;
@@ -1707,19 +1748,51 @@ main(int argc, char **argv)
   opts[15].arg = &grn_log_path;
   opts[16].arg = &grn_qlog_path;
   opts[17].arg = &pidfile_path;
+  opts[18].arg = &config_path;
   if (!(default_max_nfthreads = get_core_number())) {
     default_max_nfthreads = DEFAULT_MAX_NFTHREADS;
   }
   strcpy(listen_address, "0.0.0.0");
-  {
-    const char *config_path = getenv("GRN_CONFIG_PATH");
+  i = grn_str_getopt(argc, argv, opts, &mode);
+  if (i < 0) {
+    usage(stderr);
+    return EXIT_FAILURE;
+  }
+  opts[18].arg = NULL;
+  if (config_path) {
+    if (!load_config_file(config_path, opts, &mode)) {
+      fprintf(stderr, "%s: can't open config file: %s (%s)\n",
+              argv[0], config_path, strerror(errno));
+      return EXIT_FAILURE;
+    }
+  } else {
+    config_path = getenv("GRN_CONFIG_PATH");
     if (!config_path) {
       config_path = GRN_CONFIG_PATH;
     }
-    load_config_file(config_path, opts, &mode);
+    if (*config_path) {
+      load_config_file(config_path, opts, &mode);
+    }
   }
+  /* ignore mode option in config file */
+  mode = (mode == mode_error) ? mode_alone :
+    ((mode & ~MODE_MASK) | mode_alone);
   i = grn_str_getopt(argc, argv, opts, &mode);
   if (i < 0) { mode = mode_error; }
+  switch (mode & MODE_MASK) {
+  case mode_version :
+    show_version();
+    return EXIT_SUCCESS;
+  case mode_usage :
+    usage(stdout);
+    return EXIT_SUCCESS;
+  case mode_config :
+    show_config(stdout, opts, mode & ~MODE_MASK);
+    return EXIT_SUCCESS;
+  case mode_error :
+    usage(stderr);
+    return EXIT_FAILURE;
+  }
   if (portstr) { port = atoi(portstr); }
   if (encstr) {
     switch (*encstr) {
@@ -1838,14 +1911,8 @@ main(int argc, char **argv)
   case mode_server :
     r = do_server(argc > i ? argv[i] : NULL);
     break;
-  case mode_version :
-    show_version(); r = 0;
-    break;
-  case mode_usage :
-    usage(stdout); r = 0;
-    break;
-  default :
-    usage(stderr); r = -1;
+  default:
+    r = -1;
     break;
   }
 #ifdef WITH_LIBEDIT

  Modified: test/unit/command/test-option.rb (+36 -0)
===================================================================
--- test/unit/command/test-option.rb    2010-07-02 06:57:41 +0000 (32ad769)
+++ test/unit/command/test-option.rb    2010-06-25 05:22:57 +0000 (06f92b6)
@@ -18,6 +18,8 @@
 class OptionTest < Test::Unit::TestCase
   include GroongaTestUtils
 
+  CONFIG_ENV = {"GRN_CONFIG_PATH" => ""}
+
   def setup
     setup_database_path
   end
@@ -58,4 +60,38 @@ class OptionTest < Test::Unit::TestCase
       assert_not_predicate(status, :success?)
     end
   end
+
+  def test_config_path
+    test_options = %W[
+      port=1.1.1.1 default-encoding=none default-encoding=euc-jp
+      max-threads=12345 address=localhost
+      log-level=1 server=localhost
+    ]
+    config_file = File.join(@tmp_dir, "test-option.config")
+    assert_path_not_exist(config_file)
+    status = assert_run_groonga("",
+                                /can't open config file: #{Regexp.quote(config_file)} /,
+                                [CONFIG_ENV, "--config-path=#{config_file}"])
+    assert_not_predicate(status, :success?)
+    open(config_file, "w") {}
+    status = assert_run_groonga("", "", [CONFIG_ENV, "--config-path=#{config_file}"])
+    assert_predicate(status, :success?)
+
+    default_config = run_groonga("--show-config")
+
+    test_options.each do |opt|
+      status = assert_run_groonga([opt, default_config].join("\n"), "",
+                                  [CONFIG_ENV, "--#{opt}", "--config-path=#{config_file}", "--show-config"])
+      assert_predicate(status, :success?)
+    end
+
+    test_options.each do |opt|
+      open(config_file, "w") {|f| f.puts opt}
+      status = assert_run_groonga([opt, default_config].join("\n"), "",
+                                  [CONFIG_ENV, "--config-path=#{config_file}", "--show-config"])
+      assert_predicate(status, :success?)
+    end
+  ensure
+    FileUtils.rm_f(config_file)
+  end
 end

  Modified: test/unit/lib/ruby/groonga-test-utils.rb (+2 -1)
===================================================================
--- test/unit/lib/ruby/groonga-test-utils.rb    2010-07-02 06:57:41 +0000 (8897529)
+++ test/unit/lib/ruby/groonga-test-utils.rb    2010-06-25 05:22:57 +0000 (e5aedab)
@@ -17,13 +17,14 @@
 
 require 'fileutils'
 require 'shellwords'
+require 'tmpdir'
 require 'groonga-constants'
 
 module GroongaTestUtils
   include GroongaConstants
 
   def setup_database_path
-    @tmp_dir = File.join(File.dirname(__FILE__), "tmp")
+    @tmp_dir = Dir.mktmpdir("tmp", ENV["BUILD_DIR"])
     FileUtils.rm_rf(@tmp_dir)
     FileUtils.mkdir_p(@tmp_dir)
     @database_path = File.join(@tmp_dir, "database")




Groonga-commit メーリングリストの案内
Back to archive index