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
28 using System
.Collections
;
29 using System
.Collections
.Specialized
;
30 using System
.Reflection
;
37 public static class ConfigTool
{
39 private static void PrintUsageAndExit ()
42 "beagle-config: Command-line interface to the Beagle config file.\n" +
43 "Web page: http://www.gnome.org/projects/beagle\n" +
44 "Copyright (C) 2005 Novell, Inc.\n\n";
46 "Usage: beagle-config [OPTIONS]\n" +
47 " or: beagle-config <SECTION>\n" +
48 " or: beagle-config <SECTION> <SECTIONOPTION> [PARAMS]\n\n" +
50 " --beagled-reload-config\tAsk the beagle daemon to reload\n" +
51 " \tthe configuration file.\n" +
52 " --list-sections\t\tList all available configuration sections.\n" +
53 " --help\t\t\tPrint this usage message.\n\n" +
54 "If a section is specified with no options, then a list of the available commands for that section is shown.\n";
56 Console
.WriteLine (usage
);
58 System
.Environment
.Exit (0);
61 private static void ListSectionsAndExit ()
63 Console
.WriteLine ("Available configuration sections: ");
64 foreach (string key
in Conf
.Sections
.Keys
)
65 Console
.WriteLine (" - {0}", key
);
67 System
.Environment
.Exit (0);
70 private static void ListSectionOptionsAndExit (string sectionname
, Hashtable options
)
72 Console
.WriteLine ("Available options for section '{0}':", sectionname
);
73 foreach (string key
in options
.Keys
) {
74 Console
.Write (" - {0}", key
);
75 if (options
[key
] != null)
76 Console
.Write (" ({0})", options
[key
]);
81 System
.Environment
.Exit (0);
84 private static void ReloadConfigAndExit ()
87 ReloadConfigRequest request
= new ReloadConfigRequest ();
89 Console
.WriteLine ("ReloadConfig request was sent successfully.");
90 System
.Environment
.Exit (0);
91 } catch (Exception e
) {
92 Console
.Error
.WriteLine ("ERROR: Could not send ReloadConfig request: {0}", e
.Message
);
93 System
.Environment
.Exit (-1);
97 public static void Main (string [] args
)
99 Application
.InitCheck ("beagle-config", ref args
);
101 if (args
.Length
== 0)
102 PrintUsageAndExit ();
105 while (i
< args
.Length
) {
107 case "--list-sections":
109 ListSectionsAndExit ();
113 case "--beagled-reload-config":
114 ReloadConfigAndExit ();
119 PrintUsageAndExit ();
130 string sectionname
= args
[0];
132 if (! Conf
.Sections
.ContainsKey (sectionname
)) {
133 Console
.Error
.WriteLine ("ERROR: Invalid section name '{0}'", sectionname
);
134 Environment
.Exit (-1);
137 Conf
.Section section
= (Conf
.Section
) Conf
.Sections
[sectionname
];
138 Hashtable options
= Conf
.GetOptions (section
);
140 // No option specified?
141 if (args
.Length
== 1)
142 ListSectionOptionsAndExit (sectionname
, options
);
144 string optionname
= args
[1];
145 if (! options
.ContainsKey (optionname
)) {
146 Console
.Error
.WriteLine ("ERROR: Invalid option name '{0}'", optionname
);
147 Environment
.Exit (-2);
151 // Invoke the method the user has chosen
154 // Pack the remaining command line params into an array used for
155 // params of the method.
156 string [] optionparams
= new string [args
.Length
- 2];
158 for (j
= 0, k
= 2; k
< args
.Length
; j
++, k
++)
159 optionparams
[j
] = args
[k
];
162 string output
= null;
166 result
= Conf
.InvokeOption (section
, optionname
, optionparams
, out output
);
167 } catch (Exception e
) {
168 Console
.Error
.WriteLine("ERROR: Command execution failed - caught exception.");
169 Console
.Error
.WriteLine(e
.Message
);
170 Environment
.Exit (-3);
173 // Read the result and show the output
175 Console
.WriteLine (output
);
177 Console
.Error
.WriteLine ("ERROR: Command execution failed.");
178 Console
.Error
.WriteLine (output
);
179 Environment
.Exit (-4);
183 Environment
.Exit (0);