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
;
40 public static class ConfigTool
{
42 private static void PrintUsageAndExit ()
45 "beagle-config: Command-line interface to the Beagle config file.\n" +
46 "Web page: http://www.gnome.org/projects/beagle\n" +
47 "Copyright (C) 2005 Novell, Inc.\n\n";
49 "Usage: beagle-config [OPTIONS]\n" +
50 " or: beagle-config <SECTION>\n" +
51 " or: beagle-config <SECTION> <SECTIONOPTION> [PARAMS]\n\n" +
53 " --beagled-reload-config\tAsk the beagle daemon to reload\n" +
54 " \tthe configuration file.\n" +
55 " --list-sections\t\tList all available configuration sections.\n" +
56 " --help\t\t\tPrint this usage message.\n\n" +
57 "If a section is specified with no options, then a list of the available commands for that section is shown.\n";
59 Console
.WriteLine (usage
);
61 System
.Environment
.Exit (0);
64 private static void ListSectionsAndExit ()
66 Console
.WriteLine ("Available configuration sections: ");
67 foreach (string key
in Conf
.Sections
.Keys
)
68 Console
.WriteLine (" - {0}", key
);
70 System
.Environment
.Exit (0);
73 private static void ListSectionOptionsAndExit (string sectionname
, Hashtable options
)
75 Console
.WriteLine ("Available options for section '{0}':", sectionname
);
76 foreach (string key
in options
.Keys
) {
77 Console
.Write (" - {0}", key
);
78 if (options
[key
] != null)
79 Console
.Write (" ({0})", options
[key
]);
83 if (sectionname
== "daemon")
84 Console
.WriteLine (" - ListBackends (List enabled and disabled backends)");
86 System
.Environment
.Exit (0);
89 private static void ReloadConfigAndExit ()
92 ReloadConfigRequest request
= new ReloadConfigRequest ();
94 Console
.WriteLine ("ReloadConfig request was sent successfully.");
95 System
.Environment
.Exit (0);
96 } catch (Exception e
) {
97 Console
.Error
.WriteLine ("ERROR: Could not send ReloadConfig request: {0}", e
.Message
);
98 System
.Environment
.Exit (-1);
102 public static void Main (string [] args
)
104 if (args
.Length
== 0)
105 PrintUsageAndExit ();
108 while (i
< args
.Length
) {
110 case "--list-sections":
112 ListSectionsAndExit ();
116 case "--beagled-reload-config":
117 ReloadConfigAndExit ();
122 PrintUsageAndExit ();
133 string sectionname
= args
[0];
135 if (! Conf
.Sections
.ContainsKey (sectionname
)) {
136 Console
.Error
.WriteLine ("ERROR: Invalid section name '{0}'", sectionname
);
137 Environment
.Exit (-1);
140 Conf
.Section section
= (Conf
.Section
) Conf
.Sections
[sectionname
];
141 Hashtable options
= Conf
.GetOptions (section
);
143 // No option specified?
144 if (args
.Length
== 1)
145 ListSectionOptionsAndExit (sectionname
, options
);
147 string optionname
= args
[1];
148 if (! options
.ContainsKey (optionname
)) {
149 if (sectionname
== "daemon" && optionname
== "ListBackends") {
151 Environment
.Exit (0);
153 Console
.Error
.WriteLine ("ERROR: Invalid option name '{0}'", optionname
);
154 Environment
.Exit (-2);
159 // Invoke the method the user has chosen
162 // Pack the remaining command line params into an array used for
163 // params of the method.
164 string [] optionparams
= new string [args
.Length
- 2];
166 for (j
= 0, k
= 2; k
< args
.Length
; j
++, k
++)
167 optionparams
[j
] = args
[k
];
170 string output
= null;
174 result
= Conf
.InvokeOption (section
, optionname
, optionparams
, out output
);
175 } catch (Exception e
) {
176 Console
.Error
.WriteLine("ERROR: Command execution failed - caught exception.");
177 Console
.Error
.WriteLine(e
.Message
);
178 Environment
.Exit (-3);
181 // Read the result and show the output
183 Console
.WriteLine (output
);
185 Console
.Error
.WriteLine ("ERROR: Command execution failed.");
186 Console
.Error
.WriteLine (output
);
187 Environment
.Exit (-4);
191 Environment
.Exit (0);
195 private static void ListBackends ()
197 ArrayList backends
= new ArrayList ();
199 ArrayList assemblies
= ReflectionFu
.ScanEnvironmentForAssemblies ("BEAGLE_BACKEND_PATH", PathFinder
.BackendDir
);
201 // Add BeagleDaemonLib if it hasn't already been added.
202 bool found_daemon_lib
= false;
203 foreach (Assembly assembly
in assemblies
) {
204 if (assembly
.GetName ().Name
== "BeagleDaemonLib") {
205 found_daemon_lib
= true;
210 if (!found_daemon_lib
) {
212 assemblies
.Add (Assembly
.LoadFrom (Path
.Combine (PathFinder
.PkgLibDir
, "BeagleDaemonLib.dll")));
213 } catch (FileNotFoundException
) {
214 Console
.WriteLine ("WARNING: Could not find backend list.");
215 Environment
.Exit (1);
219 foreach (Assembly assembly
in assemblies
) {
220 foreach (Type type
in ReflectionFu
.ScanAssemblyForInterface (assembly
, typeof (Beagle
.Daemon
.IQueryable
))) {
221 foreach (Beagle
.Daemon
.QueryableFlavor flavor
in ReflectionFu
.ScanTypeForAttribute (type
, typeof (Beagle
.Daemon
.QueryableFlavor
)))
222 backends
.Add (flavor
.Name
);
226 if ( Directory
.Exists (PathFinder
.SystemIndexesDir
)) {
227 foreach (DirectoryInfo index_dir
in new DirectoryInfo (PathFinder
.SystemIndexesDir
).GetDirectories ())
228 backends
.Add (index_dir
.Name
);
231 bool found_any
= false;
233 Console
.WriteLine ("Allowed backends:");
234 foreach (string name
in backends
) {
235 if (Conf
.Daemon
.DeniedBackends
.Contains (name
))
237 Console
.WriteLine (" - {0}", name
);
242 Console
.WriteLine (" (none)");
244 Console
.WriteLine ();
248 Console
.WriteLine ("Denied backends:");
249 foreach (string name
in Conf
.Daemon
.DeniedBackends
) {
250 Console
.WriteLine (" - {0}", name
);
255 Console
.WriteLine (" (none)");