]> source.dussan.org Git - gitblit.git/commitdiff
Add administrative SSH command to manage Gitblit's runtime settings
authorJames Moger <james.moger@gitblit.com>
Fri, 21 Mar 2014 16:42:24 +0000 (12:42 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 10 Apr 2014 22:58:09 +0000 (18:58 -0400)
src/main/java/com/gitblit/transport/ssh/gitblit/ConfigCommand.java [new file with mode: 0644]
src/main/java/com/gitblit/transport/ssh/gitblit/GitblitDispatcher.java

diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/ConfigCommand.java b/src/main/java/com/gitblit/transport/ssh/gitblit/ConfigCommand.java
new file mode 100644 (file)
index 0000000..695f085
--- /dev/null
@@ -0,0 +1,166 @@
+package com.gitblit.transport.ssh.gitblit;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.kohsuke.args4j.Argument;
+import org.kohsuke.args4j.Option;
+import org.parboiled.common.StringUtils;
+
+import com.gitblit.manager.IGitblit;
+import com.gitblit.models.ServerSettings;
+import com.gitblit.models.SettingModel;
+import com.gitblit.transport.ssh.commands.CommandMetaData;
+import com.gitblit.transport.ssh.commands.SshCommand;
+import com.google.common.collect.Maps;
+
+@CommandMetaData(name = "config", description = "Administer Gitblit settings", admin = true)
+public class ConfigCommand extends SshCommand {
+
+       @Argument(index = 0, metaVar = "KEY", usage = "The setting to describe or update")
+       protected String setting;
+
+       @Argument(index = 1, metaVar = "VALUE", usage = "The new value for the setting")
+       protected String value;
+
+       @Option(name = "--list", aliases = { "-l" }, usage = "List all settings")
+       private boolean listAll;
+
+       @Option(name = "--modified", aliases = { "-m" }, usage = "List modified settings")
+       private boolean listModified;
+
+       @Option(name = "--reset", usage = "Reset a setting to it's default value")
+       private boolean reset;
+
+       @Override
+       public void run() throws UnloggedFailure {
+               IGitblit gitblit = getContext().getGitblit();
+               ServerSettings settings = gitblit.getSettingsModel();
+
+               if (listAll || listModified) {
+                       /*
+                        *  List settings
+                        */
+                       List<SettingModel> list = new ArrayList<SettingModel>();
+                       int maxLen = 0;
+                       for (String key : settings.getKeys()) {
+                               SettingModel model = settings.get(key);
+                               if (listModified) {
+                                       if (!model.isDefaultValue()) {
+                                               list.add(model);
+                                       } else {
+                                               continue;
+                                       }
+                               } else {
+                                       list.add(model);
+                               }
+
+                               if (key.length() > maxLen) {
+                                       maxLen = key.length();
+                               }
+                       }
+                       String pattern = MessageFormat.format("%s%-{0,number,0}s : %s", maxLen);
+                       for (SettingModel model : list) {
+                               stdout.println(String.format(pattern,
+                                               model.isDefaultValue() ? " " : "*",
+                                               model.name,
+                                               model.currentValue));
+                       }
+               } else if (!StringUtils.isEmpty(setting) && value == null && !reset) {
+                       /*
+                        *  Describe a setting
+                        */
+                       SettingModel model = settings.get(setting);
+                       if (model == null) {
+                               // unknown setting
+                               String value = gitblit.getSettings().getString(setting, null);
+                               if (value == null) {
+                                       // setting does not exist, can not describe
+                                       stdout.println(String.format("\"%s\" is not a valid setting.", setting));
+                                       return;
+                               }
+
+                               model = new SettingModel();
+                               model.defaultValue = "";
+                               model.currentValue = value;
+                       }
+                       stdout.println();
+                       stdout.println(model.name);
+                       if (!StringUtils.isEmpty(model.since)) {
+                               stdout.println(SettingModel.SINCE + " " + model.since);
+                       }
+                       if (model.restartRequired) {
+                               stdout.println(SettingModel.RESTART_REQUIRED);
+                       }
+                       if (model.spaceDelimited) {
+                               stdout.println(SettingModel.SPACE_DELIMITED);
+                       }
+                       if (!StringUtils.isEmpty(model.description)) {
+                               stdout.println();
+                               stdout.println(model.description);
+                       }
+                       stdout.println();
+                       if (model.defaultValue != null) {
+                               stdout.println("default: " + model.defaultValue);
+                       }
+                       if (!model.isDefaultValue()) {
+                               stdout.println("current: " + model.currentValue);
+                       } else {
+                               stdout.println("current: <default>");
+                       }
+                       stdout.println();
+               } else if (!StringUtils.isEmpty(setting) && value == null && reset) {
+                       /*
+                        *  Reset a setting
+                        */
+                       SettingModel model = settings.get(setting);
+                       if (model == null) {
+                               stdout.println(String.format("\"%s\" is not a valid setting.", setting));
+                               return;
+                       }
+
+                       if (model.defaultValue == null || model.defaultValue.equals("null")) {
+                               // no default value, remove setting
+                               gitblit.getSettings().removeSetting(setting);
+                               gitblit.getSettings().saveSettings();
+                               settings.remove(setting);
+
+                               stdout.println(String.format("%s removed.", setting));
+                       } else {
+                               // reset to default value
+                               Map<String, String> updates = Maps.newHashMap();
+                               updates.put(setting, model.defaultValue == null ? "" : model.defaultValue);
+                               gitblit.getSettings().saveSettings(updates);
+
+                               // confirm reset
+                               String newValue = gitblit.getSettings().getString(setting, null);
+                               if (model.defaultValue.equals(newValue)) {
+                                       stdout.println(String.format("%s reset to the default value.", setting));
+                               } else {
+                                       stdout.println(String.format("failed to reset %s!", setting));
+                               }
+                       }
+
+               } else if (!StringUtils.isEmpty(setting) && value != null) {
+                       /*
+                        *  Update a setting
+                        */
+                       Map<String, String> updates = Maps.newHashMap();
+                       updates.put(setting, value);
+                       gitblit.getSettings().saveSettings(updates);
+
+                       // confirm update
+                       String newValue = gitblit.getSettings().getString(setting, null);
+                       if (value.equals(newValue)) {
+                               stdout.println(String.format("%s updated.", setting));
+                       } else {
+                               stdout.println(String.format("failed to update %s!", setting));
+                       }
+               } else {
+                       // Display usage
+                       showHelp();
+               }
+       }
+}
\ No newline at end of file
index fe9075d2e089e8a4325125ad7eec8ba6e5d3fe1a..f9b5f4fc970d0dc201e2b7de2a31aad466bef341 100644 (file)
@@ -28,6 +28,7 @@ public class GitblitDispatcher extends DispatchCommand {
                registerCommand(user, VersionCommand.class);
                registerCommand(user, CreateRepository.class);
                registerCommand(user, SetAccountCommand.class);
+               registerCommand(user, ConfigCommand.class);
 
                // nested dispatchers
                registerDispatcher(user, ListDispatcher.class);