|
| 1 | +package fi.helsinki.cs.tmc.cli.command; |
| 2 | + |
| 3 | +import fi.helsinki.cs.tmc.cli.core.AbstractCommand; |
| 4 | +import fi.helsinki.cs.tmc.cli.core.CliContext; |
| 5 | +import fi.helsinki.cs.tmc.cli.core.Command; |
| 6 | +import fi.helsinki.cs.tmc.cli.io.Io; |
| 7 | + |
| 8 | +import org.apache.commons.cli.CommandLine; |
| 9 | +import org.apache.commons.cli.Options; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.HashMap; |
| 16 | + |
| 17 | +@Command(name = "config", desc = "Set/unset TMC-CLI properties") |
| 18 | +public class ConfigCommand extends AbstractCommand { |
| 19 | + |
| 20 | + private static final Logger logger = LoggerFactory.getLogger(ConfigCommand.class); |
| 21 | + private CliContext context; |
| 22 | + private Io io; |
| 23 | + |
| 24 | + private HashMap<String, String> properties; |
| 25 | + private boolean quiet; |
| 26 | + |
| 27 | + @Override |
| 28 | + public String[] getUsages() { |
| 29 | + return new String[] { |
| 30 | + "[-q|--quiet] KEY=\"VALUE\"...", |
| 31 | + "-d|--delete [-q|--quiet] KEY...", |
| 32 | + "-l|--list", |
| 33 | + "-g|--get=KEY"}; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void getOptions(Options options) { |
| 38 | + options.addOption("g", "get", true, "Get value of a key"); |
| 39 | + options.addOption("d", "delete", false, "Unset given property keys"); |
| 40 | + options.addOption("q", "quiet", false, "Don't ask confirmations"); |
| 41 | + options.addOption("l", "list", false, "List all properties"); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void run(CliContext context, CommandLine args) { |
| 46 | + this.context = context; |
| 47 | + this.io = context.getIo(); |
| 48 | + |
| 49 | + boolean get = args.hasOption("g"); |
| 50 | + boolean listing = args.hasOption("l"); |
| 51 | + boolean delete = args.hasOption("d"); |
| 52 | + this.quiet = args.hasOption("q"); |
| 53 | + |
| 54 | + String[] arguments = args.getArgs(); |
| 55 | + this.properties = context.getProperties(); |
| 56 | + |
| 57 | + if ((get ? 1 : 0) + (listing ? 1 : 0) + (delete ? 1 : 0) > 1) { |
| 58 | + io.errorln("Only one of the --get or --list or --delete options can " |
| 59 | + + "be used at same time."); |
| 60 | + printUsage(context); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (listing) { |
| 65 | + if (arguments.length != 0) { |
| 66 | + io.errorln("Listing option doesn't take any arguments."); |
| 67 | + printUsage(context); |
| 68 | + return; |
| 69 | + } |
| 70 | + printAllProperties(); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (get) { |
| 75 | + if (arguments.length != 0) { |
| 76 | + io.errorln("There should not be extra arguments when using --get option."); |
| 77 | + printUsage(context); |
| 78 | + return; |
| 79 | + } |
| 80 | + String key = args.getOptionValue('g'); |
| 81 | + boolean exists = properties.containsKey(key); |
| 82 | + if (!exists && !quiet) { |
| 83 | + io.errorln("The property " + key + " doesn't exist."); |
| 84 | + return; |
| 85 | + } |
| 86 | + io.println(exists ? properties.get(key) : ""); |
| 87 | + return; |
| 88 | + } else if (delete) { |
| 89 | + deleteProperties(arguments); |
| 90 | + } else { |
| 91 | + setProperties(arguments); |
| 92 | + } |
| 93 | + context.saveProperties(); |
| 94 | + } |
| 95 | + |
| 96 | + private void printAllProperties() { |
| 97 | + ArrayList<String> array = new ArrayList<>(properties.keySet()); |
| 98 | + Collections.sort(array); |
| 99 | + |
| 100 | + for (String key : array) { |
| 101 | + io.println(key + "=" + properties.get(key)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void deleteProperties(String[] keys) { |
| 106 | + if (this.quiet) { |
| 107 | + for (String key : keys) { |
| 108 | + if (properties.containsKey(key)) { |
| 109 | + properties.remove(key); |
| 110 | + } |
| 111 | + } |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if (keys.length == 0) { |
| 116 | + io.errorln("Expected at least one property as argument."); |
| 117 | + printUsage(context); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + for (String key : keys) { |
| 122 | + if (!properties.containsKey(key)) { |
| 123 | + io.error("Key " + key + " doesn't exist."); |
| 124 | + return; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + io.println("Deleting " + keys.length + " properties."); |
| 129 | + |
| 130 | + if (!io.readConfirmation("Are you sure?", true)) { |
| 131 | + return; |
| 132 | + } |
| 133 | + for (String key : keys) { |
| 134 | + String oldValue = properties.remove(key); |
| 135 | + io.println("Deleted key " + key + ", was " + oldValue); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private void setProperties(String[] arguments) { |
| 140 | + if (arguments.length == 0) { |
| 141 | + io.errorln("Expected at least one key-value pair."); |
| 142 | + printUsage(context); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + if (this.quiet) { |
| 147 | + setPropertiesQuietly(arguments); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + io.print("Setting property keys:"); |
| 152 | + for (String argument : arguments) { |
| 153 | + String[] parts = argument.split("=", 2); |
| 154 | + String oldValue = properties.get(parts[0]); |
| 155 | + io.print(" " + parts[0] + " set to \"" + parts[1] + "\""); |
| 156 | + |
| 157 | + if (oldValue != null) { |
| 158 | + io.println(", it was \"" + oldValue + "\"."); |
| 159 | + } else { |
| 160 | + io.println("."); |
| 161 | + } |
| 162 | + } |
| 163 | + io.println(); |
| 164 | + if (!io.readConfirmation("Are you sure?", true)) { |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + setPropertiesQuietly(arguments); |
| 169 | + } |
| 170 | + |
| 171 | + private void setPropertiesQuietly(String[] arguments) { |
| 172 | + for (String argument : arguments) { |
| 173 | + String[] parts = argument.split("=", 2); |
| 174 | + properties.put(parts[0], parts[1]); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments