From: James Moger Date: Fri, 21 Mar 2014 16:42:24 +0000 (-0400) Subject: Add administrative SSH command to manage Gitblit's runtime settings X-Git-Tag: v1.5.0~68^2~46 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=dca0d1667cf5e6e8b14c0ad633429f2ce8fcc64c;p=gitblit.git Add administrative SSH command to manage Gitblit's runtime settings --- 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 index 00000000..695f0850 --- /dev/null +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/ConfigCommand.java @@ -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 list = new ArrayList(); + 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: "); + } + 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 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 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 diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/GitblitDispatcher.java b/src/main/java/com/gitblit/transport/ssh/gitblit/GitblitDispatcher.java index fe9075d2..f9b5f4fc 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/GitblitDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/GitblitDispatcher.java @@ -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);