NoiseFilter: Dont drop last word of apparent hostnames. Too many non-hostnames can...
[beagle.git] / tools / Config.cs
blobced613f98917f3ec4256df75ae10140175ef3a0c
1 //
2 // Config.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Diagnostics;
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Reflection;
34 using Beagle;
35 using Beagle.Daemon;
36 using Beagle.Util;
38 //using Gtk;
39 using GLib;
41 public static class ConfigTool {
43 private static void PrintUsageAndExit ()
45 string usage =
46 "beagle-config: Command-line interface to the Beagle config file.\n" +
47 "Web page: http://www.gnome.org/projects/beagle\n" +
48 "Copyright (C) 2005-2006 Novell, Inc.\n\n";
49 usage +=
50 "Usage: beagle-config [OPTIONS]\n" +
51 " or: beagle-config <SECTION>\n" +
52 " or: beagle-config <SECTION> <SECTIONOPTION> [PARAMS]\n\n" +
53 "Options:\n" +
54 " --beagled-reload-config\tAsk the beagle daemon to reload\n" +
55 " \tthe configuration file.\n" +
56 " --list-sections\t\tList all available configuration sections.\n" +
57 " --help\t\t\tPrint this usage message.\n\n" +
58 "If a section is specified with no options, then a list of the available commands for that section is shown.\n";
60 Console.WriteLine (usage);
62 System.Environment.Exit (0);
65 private static void ListSectionsAndExit ()
67 Console.WriteLine ("Available configuration sections: ");
68 foreach (string key in Conf.Sections.Keys)
69 Console.WriteLine (" - {0}", key);
71 System.Environment.Exit (0);
74 private static void ListSectionOptionsAndExit (string sectionname, Hashtable options)
76 Console.WriteLine ("Available options for section '{0}':", sectionname);
77 foreach (string key in options.Keys) {
78 Console.Write (" - {0}", key);
79 if (options [key] != null)
80 Console.Write (" ({0})", options [key]);
82 Console.WriteLine ();
84 if (sectionname == "daemon")
85 Console.WriteLine (" - ListBackends (List enabled and disabled backends)");
87 System.Environment.Exit (0);
90 private static void ReloadConfigAndExit ()
92 try {
93 ReloadConfigRequest request = new ReloadConfigRequest ();
94 request.Send ();
95 Console.WriteLine ("ReloadConfig request was sent successfully.");
96 System.Environment.Exit (0);
97 } catch (Exception e) {
98 Console.Error.WriteLine ("ERROR: Could not send ReloadConfig request: {0}", e.Message);
99 System.Environment.Exit (-1);
103 public static void Main (string [] args)
105 if (args.Length == 0)
106 PrintUsageAndExit ();
108 int i = 0;
109 while (i < args.Length) {
110 switch (args [i]) {
111 case "--list-sections":
112 Conf.Load ();
113 ListSectionsAndExit ();
114 return;
116 case "--reload":
117 case "--beagled-reload-config":
118 ReloadConfigAndExit ();
119 return;
121 case "--help":
122 case "--usage":
123 PrintUsageAndExit ();
124 return;
126 default:
127 break;
129 ++i;
132 Conf.Load ();
134 string sectionname = args [0];
136 if (! Conf.Sections.ContainsKey (sectionname)) {
137 Console.Error.WriteLine ("ERROR: Invalid section name '{0}'", sectionname);
138 Environment.Exit (-1);
141 Conf.Section section = (Conf.Section) Conf.Sections [sectionname];
142 Hashtable options = Conf.GetOptions (section);
144 // No option specified?
145 if (args.Length == 1)
146 ListSectionOptionsAndExit (sectionname, options);
148 string optionname = args [1];
149 if (! options.ContainsKey (optionname)) {
150 if (sectionname == "daemon" && optionname == "ListBackends") {
151 ListBackends ();
152 Environment.Exit (0);
153 } else {
154 Console.Error.WriteLine ("ERROR: Invalid option name '{0}'", optionname);
155 Environment.Exit (-2);
160 // Invoke the method the user has chosen
163 // Pack the remaining command line params into an array used for
164 // params of the method.
165 string [] optionparams = new string [args.Length - 2];
166 int j, k;
167 for (j = 0, k = 2; k < args.Length; j++, k++)
168 optionparams [j] = args [k];
170 // Invoke the method
171 string output = null;
172 bool result = false;
174 try {
175 result = Conf.InvokeOption (section, optionname, optionparams, out output);
176 } catch (Exception e) {
177 Console.Error.WriteLine("ERROR: Command execution failed - caught exception.");
178 Console.Error.WriteLine(e.Message);
179 Environment.Exit (-3);
182 // Read the result and show the output
183 if (result == true)
184 Console.WriteLine (output);
185 else {
186 Console.Error.WriteLine ("ERROR: Command execution failed.");
187 Console.Error.WriteLine (output);
188 Environment.Exit (-4);
191 Conf.Save ();
192 Environment.Exit (0);
196 private static void ListBackends ()
198 ArrayList backends = new ArrayList ();
200 ArrayList assemblies = ReflectionFu.ScanEnvironmentForAssemblies ("BEAGLE_BACKEND_PATH", PathFinder.BackendDir);
202 // Add BeagleDaemonLib if it hasn't already been added.
203 bool found_daemon_lib = false;
204 foreach (Assembly assembly in assemblies) {
205 if (assembly.GetName ().Name == "BeagleDaemonLib") {
206 found_daemon_lib = true;
207 break;
211 if (!found_daemon_lib) {
212 try {
213 assemblies.Add (Assembly.LoadFrom (Path.Combine (PathFinder.PkgLibDir, "BeagleDaemonLib.dll")));
214 } catch (FileNotFoundException) {
215 Console.WriteLine ("WARNING: Could not find backend list.");
216 Environment.Exit (1);
220 foreach (Assembly assembly in assemblies) {
221 foreach (Type type in ReflectionFu.GetTypesFromAssemblyAttribute (assembly, typeof (IQueryableTypesAttribute))) {
222 foreach (Beagle.Daemon.QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute (type, typeof (Beagle.Daemon.QueryableFlavor)))
223 backends.Add (flavor.Name);
227 if ( Directory.Exists (PathFinder.SystemIndexesDir)) {
228 foreach (DirectoryInfo index_dir in new DirectoryInfo (PathFinder.SystemIndexesDir).GetDirectories ())
229 backends.Add (index_dir.Name);
232 bool found_any = false;
234 Console.WriteLine ("Allowed backends:");
235 foreach (string name in backends) {
236 if (Conf.Daemon.DeniedBackends.Contains (name))
237 continue;
238 Console.WriteLine (" - {0}", name);
239 found_any = true;
242 if (! found_any)
243 Console.WriteLine (" (none)");
245 Console.WriteLine ();
247 found_any = false;
249 Console.WriteLine ("Denied backends:");
250 foreach (string name in Conf.Daemon.DeniedBackends) {
251 Console.WriteLine (" - {0}", name);
252 found_any = true;
255 if (! found_any)
256 Console.WriteLine (" (none)");