Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit c1e6917

Browse files
authored
Merge pull request #393 from testmycode/config-command
Config command created.
2 parents 8cffbcc + d54f480 commit c1e6917

File tree

6 files changed

+389
-15
lines changed

6 files changed

+389
-15
lines changed

scripts/stub.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ tmc_uninstall() {
231231
}
232232

233233
tmc_main() {
234+
local TMC_FLAGS=
235+
234236
if [ "${1-}" == "++internal-update" ]; then
235237
tmc_install_update
236238
fi
@@ -240,17 +242,15 @@ tmc_main() {
240242
exit
241243
fi
242244

243-
# check if this is first time running the tmc
244-
if [ ! -e "$(tmc_autocomplete_file)" ]; then
245-
tmc_update_autocomplete
246-
exit
247-
fi
248-
249-
local TMC_FLAGS=
250-
251245
# disable auto updates if tmc is from native package
252246
if [[ $TMC_NATIVE_PACKAGE == 1 ]]; then
253247
TMC_FLAGS="-d"
248+
else
249+
# check if this is first time running the tmc
250+
if [ ! -e "$(tmc_autocomplete_file)" ]; then
251+
tmc_update_autocomplete
252+
exit
253+
fi
254254
fi
255255

256256
#EMBED_UNIT_TESTS_SH
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
}

src/main/java/fi/helsinki/cs/tmc/cli/command/PropertiesCommand.java renamed to src/main/java/fi/helsinki/cs/tmc/cli/command/hidden/PropertiesCommand.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package fi.helsinki.cs.tmc.cli.command;
1+
package fi.helsinki.cs.tmc.cli.command.hidden;
22

33
import fi.helsinki.cs.tmc.cli.core.AbstractCommand;
44
import fi.helsinki.cs.tmc.cli.core.CliContext;
@@ -37,6 +37,9 @@ public void run(CliContext context, CommandLine args) {
3737
boolean unset = args.hasOption("u");
3838
String[] arguments = args.getArgs();
3939
HashMap<String, String> props = context.getProperties();
40+
41+
io.errorln("This is deprecated command; use config command instead.");
42+
4043
if (arguments.length == 0) {
4144
printAllProps(props);
4245
return;

0 commit comments

Comments
 (0)