Skip to content

Commit 9ca84f5

Browse files
author
John Donovan
committed
Removing confusing properties, relying on command line opts.
1 parent 11999a5 commit 9ca84f5

9 files changed

Lines changed: 116 additions & 48 deletions

File tree

.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="src" path="test"/>
55
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
67
<classpathentry kind="lib" path="lib/commons-cli-1.3-SNAPSHOT.jar"/>
78
<classpathentry kind="lib" path="lib/zip4j_1.3.2.jar"/>
8-
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
99
<classpathentry kind="output" path="bin"/>
1010
</classpath>

src/com/gtnexus/appxpress/cli/AppXpressOption.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public interface AppXpressOption extends CLIOption {
1414
public boolean shouldBeOmitted();
1515
public boolean isValid(String val);
1616
public String getDefaultValue();
17+
public boolean isStoreableProperty();
1718

1819
}

src/com/gtnexus/appxpress/cli/CLIOption.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
public interface CLIOption {
55
public String toString();
6-
public String getName();
6+
public String getLongName();
7+
public String getFlag();
78
public Class<?> getType();
89
public String getDescription();
910
public boolean hasArg();

src/com/gtnexus/appxpress/cli/CommandLineInterfaceParser.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public CommandLineInterfaceParser(String appName, String[] userArgs, Set<T> cliO
4949
this.cliOptionSet = cliOptionSet;
5050
this.options = new Options();
5151
for (CLIOption opt : cliOptionSet) {
52-
options.addOption(Option.builder(opt.getName())
52+
options.addOption(Option.builder(opt.getFlag())
53+
.longOpt(opt.getLongName())
5354
.type(opt.getType())
5455
.desc(opt.getDescription())
5556
.hasArg(opt.hasArg())
@@ -100,7 +101,7 @@ public boolean hasOption(CLIOption opt) {
100101
if (cmd == null) {
101102
return false;
102103
}
103-
return cmd.hasOption(opt.getName());
104+
return cmd.hasOption(opt.getLongName()) || cmd.hasOption(opt.getFlag());
104105
}
105106

106107
public Map<T, String> getOptionsMap() {
@@ -109,8 +110,8 @@ public Map<T, String> getOptionsMap() {
109110
}
110111
Map<T, String> optMap = new HashMap<>();
111112
for (T opt : cliOptionSet) {
112-
if (cmd.hasOption(opt.getName())) {
113-
optMap.put(opt, cmd.getOptionValue(opt.getName()));
113+
if (cmd.hasOption(opt.getLongName())) {
114+
optMap.put(opt, cmd.getOptionValue(opt.getLongName()));
114115
}
115116
}
116117
return optMap;

src/com/gtnexus/appxpress/pmbuilder/cli/BuilderOption.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import com.gtnexus.appxpress.cli.ValidityProvider;
66

77
public enum BuilderOption implements AppXpressOption {
8-
HELP("help", String.class, "Display usage for this tool", false, false, null),
9-
CUSTOMER("customer", String.class, "The customer who owns this module.",true, true, null ),
10-
PLATFORM("platform", String.class, "The name of the platform.", true, true, null),
11-
LOCAL_DIR("localDir", String.class, "Relative Path of GIT staging folder",true, true, null),
8+
HELP("h", "help", String.class, "Display usage for this tool", false, false, null),
9+
CUSTOMER("c", "customer", String.class, "The customer who owns this module.",true, true, null ),
10+
PLATFORM("p", "platform", String.class, "The name of the platform.", true, true, null),
11+
LOCAL_DIR("ld", "localDir", String.class, "Relative Path of GIT staging folder",true, true, null),
1212
;
1313

14+
private final String flag;
1415
private final String name;
1516
private final Class<?> type;
1617
private final boolean hasArg;
@@ -20,8 +21,9 @@ public enum BuilderOption implements AppXpressOption {
2021
private static final OptionMessageProvider msgProvider = new OptionMessageProvider();
2122
private static final ValidityProvider validityProvider = new ValidityProvider();
2223

23-
private BuilderOption(String name, Class<?> type, String description,
24+
private BuilderOption(String flag, String name, Class<?> type, String description,
2425
boolean hasArg, boolean isMandatory, String defaulValue) {
26+
this.flag = flag;
2527
this.name = name;
2628
this.type = type;
2729
this.hasArg = hasArg;
@@ -31,7 +33,7 @@ private BuilderOption(String name, Class<?> type, String description,
3133
}
3234

3335
@Override
34-
public String getName() {
36+
public String getLongName() {
3537
return name;
3638
}
3739

@@ -80,4 +82,17 @@ public String getDefaultValue() {
8082
return defaultValue;
8183
}
8284

85+
@Override
86+
public String getFlag() {
87+
return flag;
88+
}
89+
90+
@Override
91+
public boolean isStoreableProperty() {
92+
if(this == LOCAL_DIR) {
93+
return true;
94+
}
95+
return false;
96+
}
97+
8398
}

src/com/gtnexus/appxpress/pmextractor/cli/ArgsAndPropertiesConsolidator.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class ArgsAndPropertiesConsolidator<T extends AppXpressOption> {
2929
private final Set<T> optSet;
3030
private final Properties properties;
3131
private final Asker asker;
32+
private boolean presentSave;
3233

3334
/**
3435
*
@@ -44,6 +45,7 @@ public ArgsAndPropertiesConsolidator(Map<T, String> userArgs,
4445
this.optSet = optSet;
4546
this.properties = properties;
4647
this.asker = new Asker(System.in, System.out);
48+
presentSave = false;
4749
}
4850

4951
/**
@@ -63,6 +65,7 @@ public ArgsAndPropertiesConsolidator(Map<T, String> userArgs,
6365
this.optSet = optSet;
6466
this.properties = properties;
6567
this.asker = new Asker(inputStream, printStream);
68+
presentSave = false;
6669
}
6770

6871
/**
@@ -73,8 +76,10 @@ public Map<T, String> consolidate() {
7376
for (T opt : optSet) {
7477
if(!opt.shouldBeOmitted()) {
7578
String val = consolidateSingle(opt);
76-
properties.put(opt.getName(), val);
7779
optMap.put(opt, val);
80+
if(opt.isStoreableProperty()) {
81+
properties.put(opt.getLongName(), val);
82+
}
7883
}
7984
}
8085
return optMap;
@@ -88,9 +93,15 @@ public Map<T, String> consolidate() {
8893
*/
8994
private String consolidateSingle(AppXpressOption option) {
9095
String input = null;
91-
String propVal = properties.getProperty(option.getName());
96+
String propVal = null;
97+
if(option.isStoreableProperty()) {
98+
propVal = properties.getProperty(option.getLongName());
99+
}
92100
if (userArgs.containsKey(option)) {
93101
input = userArgs.get(option);
102+
if(option.isStoreableProperty()) {
103+
presentSave = true;
104+
}
94105
}
95106
if (input != null && !input.isEmpty()) {
96107
return input;
@@ -123,21 +134,40 @@ private String getParameterFromUser(AppXpressOption option) {
123134
* when there is an IOException when writing to the properties
124135
* file at the propPath.
125136
*/
126-
public void presentSaveOption(String propPath) throws PMExtractorException {
127-
File settingsFile = new File(propPath);
137+
public void presentSaveOption(final String propPath) throws PMExtractorException {
138+
if(presentSave) {
139+
final String answer = askSaveQuestion();
140+
if (answer.equalsIgnoreCase("Y")) {
141+
saveProps(propPath);
142+
}
143+
}
144+
}
145+
146+
/**
147+
*
148+
* @param propPath
149+
* @throws PMExtractorException
150+
*/
151+
private void saveProps(final String propPath) throws PMExtractorException {
152+
final File settingsFile = new File(propPath);
153+
try (FileOutputStream settingsOutputStream = new FileOutputStream(
154+
settingsFile)) {
155+
properties.store(settingsOutputStream, null);
156+
} catch (IOException e) {
157+
throw new PMExtractorException(
158+
"Failed to write properties file!", e);
159+
}
160+
}
161+
162+
/**
163+
*
164+
* @return
165+
*/
166+
private String askSaveQuestion() {
128167
String answer = asker.ask("Save settings? [y/n]: ");
129168
while (!answer.equalsIgnoreCase("Y") && !answer.equalsIgnoreCase("N")) {
130169
answer = asker.ask("Invalid input. Please try again.");
131170
}
132-
if (answer.equalsIgnoreCase("Y")) {
133-
// Save the settings file
134-
try (FileOutputStream settingsOutputStream = new FileOutputStream(
135-
settingsFile)) {
136-
properties.store(settingsOutputStream, null);
137-
} catch (IOException e) {
138-
throw new PMExtractorException(
139-
"Failed to write properties file!", e);
140-
}
141-
}
171+
return answer;
142172
}
143173
}

src/com/gtnexus/appxpress/pmextractor/cli/ExtractorOption.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
*/
1111
public enum ExtractorOption implements AppXpressOption {
1212

13-
HELP("help", String.class, "Display usage for this tool", false, false, null),
14-
PLATFORM_ZIP("platformZip", String.class,"Exported Platform Module Name", true, true, null),
15-
LOCAL_DIR("localDir", String.class, "Relative Path of GIT staging folder",true, true, null),
16-
CUSTOMER("customer", String.class, "Customer of Platform Module", true, true, null),
17-
PLATFORM("platform", String.class, "Platform Module that is being exported", true, true, null),
18-
OVERWRITE_SCRIPTS("overwriteScripts", Boolean.class, "If Y -> overwriteScripts = true", true, false, "N"),
19-
OVERWRITE_FEF("overwriteFef", Boolean.class, "If Y -> overwriteFEF = true", true, false, "N");
20-
21-
private final String name;
13+
HELP("h", "help", String.class, "Display usage for this tool", false, false, null),
14+
PLATFORM_ZIP("pz", "platformZip", String.class,"Exported Platform Module Name", true, true, null),
15+
LOCAL_DIR("ld", "localDir", String.class, "Relative Path of GIT staging folder",true, true, null),
16+
CUSTOMER("c", "customer", String.class, "Customer of Platform Module", true, true, null),
17+
PLATFORM("p", "platform", String.class, "Platform Module that is being exported", true, true, null),
18+
OVERWRITE_SCRIPTS("os", "overwriteScripts", Boolean.class, "If Y -> overwriteScripts = true", true, false, "N"),
19+
OVERWRITE_FEF("of", "overwriteFef", Boolean.class, "If Y -> overwriteFEF = true", true, false, "N");
20+
21+
private final String flag;
22+
private final String longName;
2223
private final Class<?> type;
2324
private final boolean hasArg;
2425
private final boolean isMandatory;
@@ -29,17 +30,18 @@ public enum ExtractorOption implements AppXpressOption {
2930

3031
/**
3132
*
32-
* @param name
33+
* @param longName
3334
* The name of this ExtractorOption.
3435
* @param type
3536
* The ExtractorOption type.
3637
* @param description
3738
* @param isMandatory
3839
* @param defaulValue
3940
*/
40-
private ExtractorOption(String name, Class<?> type, String description,
41+
private ExtractorOption(String flag, String longName, Class<?> type, String description,
4142
boolean hasArg, boolean isMandatory, String defaulValue) {
42-
this.name = name;
43+
this.flag = flag;
44+
this.longName = longName;
4345
this.type = type;
4446
this.hasArg = hasArg;
4547
this.description = description;
@@ -49,19 +51,19 @@ private ExtractorOption(String name, Class<?> type, String description,
4951

5052
@Override
5153
public String toString() {
52-
return name;
54+
return longName;
5355
}
5456

55-
public String getName() {
56-
return name;
57+
public String getLongName() {
58+
return longName;
5759
}
5860

5961
public Class<?> getType() {
6062
return type;
6163
}
6264

6365
public String getMessage() {
64-
return msgProvider.getMessage(type, name);
66+
return msgProvider.getMessage(type, longName);
6567
}
6668

6769
public String getDescription() {
@@ -87,7 +89,7 @@ public boolean shouldBeOmitted() {
8789
*/
8890
public String getDefaultValue() {
8991
if (this.isMandatory) {
90-
throw new UnsupportedOperationException(this.name
92+
throw new UnsupportedOperationException(this.longName
9193
+ " is a mandatory field, and must come from "
9294
+ "user args or properties. There is no default value.");
9395
}
@@ -105,4 +107,17 @@ public boolean isValid(String val) {
105107
return validityProvider.isValid(val, type);
106108
}
107109

110+
@Override
111+
public String getFlag() {
112+
return flag;
113+
}
114+
115+
@Override
116+
public boolean isStoreableProperty() {
117+
if(this == LOCAL_DIR) {
118+
return true;
119+
}
120+
return false;
121+
}
122+
108123
}

test/com/gtnexus/appxpress/pmextractor/ArgsAndPropertiesConsolidatorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ public void testWithArgsAndProps() {
6868
properties.put(ExtractorOption.LOCAL_DIR.toString(), "prop_local_dir");
6969
properties.put(ExtractorOption.PLATFORM.toString(), "prop_platform");
7070
ArgsAndPropertiesConsolidator<ExtractorOption> consolidator = new ArgsAndPropertiesConsolidator<>(
71-
args, optSet, properties, inputStreamFrom(), new PrintStream(
71+
args, optSet, properties, inputStreamFrom("arg_customer", "arg_platform"), new PrintStream(
7272
new NullOutputStream()));
7373
Map<ExtractorOption, String> consolidated = consolidator.consolidate();
7474
for (ExtractorOption option : allNonOmmitable) {
75-
if (option.equals(ExtractorOption.LOCAL_DIR)
76-
|| option.equals(ExtractorOption.PLATFORM)) {
77-
assertTrue(consolidated.get(option).startsWith("prop_"));
75+
if (option.equals(ExtractorOption.LOCAL_DIR)) {
76+
assertTrue("The option was " + consolidated.get(option),
77+
consolidated.get(option).startsWith("prop_"));
7878
} else {
7979
assertTrue(consolidated.get(option)
8080
+ " does not start with arg_.",

test/com/gtnexus/appxpress/pmextractor/cli/DummyOption.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class DummyOption implements CLIOption{
77

88
@Override
9-
public String getName() {
9+
public String getLongName() {
1010
return "Dummy";
1111
}
1212

@@ -25,4 +25,9 @@ public boolean hasArg() {
2525
return false;
2626
}
2727

28+
@Override
29+
public String getFlag() {
30+
return "dummyFlag";
31+
}
32+
2833
}

0 commit comments

Comments
 (0)