4 // Copyright (C) 2005 Novell, Inc.
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
29 using System
.Diagnostics
;
30 using System
.Collections
;
31 using System
.Collections
.Specialized
;
32 using System
.Reflection
;
39 public static class ConfigTool
{
41 private static void PrintUsageAndExit ()
44 "beagle-config: Command-line interface to the Beagle config file.\n" +
45 "Web page: http://www.gnome.org/projects/beagle\n" +
46 "Copyright (C) 2005 Novell, Inc.\n\n";
48 "Usage: beagle-config [OPTIONS]\n" +
49 " or: beagle-config <SECTION>\n" +
50 " or: beagle-config <SECTION> <SECTIONOPTION> [PARAMS]\n\n" +
52 " --beagled-reload-config\tAsk the beagle daemon to reload\n" +
53 " \tthe configuration file.\n" +
54 " --list-sections\t\tList all available configuration sections.\n" +
55 " --help\t\t\tPrint this usage message.\n\n" +
56 "If a section is specified with no options, then a list of the available commands for that section is shown.\n";
58 Console
.WriteLine (usage
);
60 System
.Environment
.Exit (0);
63 private static void ListSectionsAndExit ()
65 Console
.WriteLine ("Available configuration sections: ");
66 foreach (string key
in Conf
.Sections
.Keys
)
67 Console
.WriteLine (" - {0}", key
);
69 System
.Environment
.Exit (0);
72 private static void ListSectionOptionsAndExit (string sectionname
, Hashtable options
)
74 Console
.WriteLine ("Available options for section '{0}':", sectionname
);
75 foreach (string key
in options
.Keys
) {
76 Console
.Write (" - {0}", key
);
77 if (options
[key
] != null)
78 Console
.Write (" ({0})", options
[key
]);
82 if (sectionname
== "daemon")
83 Console
.WriteLine (" - ListBackends (List enabled and disabled backends)");
85 System
.Environment
.Exit (0);
88 private static void ReloadConfigAndExit ()
91 ReloadConfigRequest request
= new ReloadConfigRequest ();
93 Console
.WriteLine ("ReloadConfig request was sent successfully.");
94 System
.Environment
.Exit (0);
95 } catch (Exception e
) {
96 Console
.Error
.WriteLine ("ERROR: Could not send ReloadConfig request: {0}", e
.Message
);
97 System
.Environment
.Exit (-1);
101 public static void Main (string [] args
)
103 Application
.InitCheck ("beagle-config", ref args
);
105 if (args
.Length
== 0)
106 PrintUsageAndExit ();
109 while (i
< args
.Length
) {
111 case "--list-sections":
113 ListSectionsAndExit ();
117 case "--beagled-reload-config":
118 ReloadConfigAndExit ();
123 PrintUsageAndExit ();
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") {
152 Environment
.Exit (0);
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];
167 for (j
= 0, k
= 2; k
< args
.Length
; j
++, k
++)
168 optionparams
[j
] = args
[k
];
171 string output
= null;
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
184 Console
.WriteLine (output
);
186 Console
.Error
.WriteLine ("ERROR: Command execution failed.");
187 Console
.Error
.WriteLine (output
);
188 Environment
.Exit (-4);
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;
211 if (!found_daemon_lib
) {
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
.ScanAssemblyForInterface (assembly
, typeof (Beagle
.Daemon
.IQueryable
))) {
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
))
238 Console
.WriteLine (" - {0}", name
);
243 Console
.WriteLine (" (none)");
245 Console
.WriteLine ();
249 Console
.WriteLine ("Denied backends:");
250 foreach (string name
in Conf
.Daemon
.DeniedBackends
) {
251 Console
.WriteLine (" - {0}", name
);
256 Console
.WriteLine (" (none)");