88import fi .helsinki .cs .tmc .cli .io .Io ;
99
1010import fi .helsinki .cs .tmc .cli .utils .BadValueTypeException ;
11- import fi .helsinki .cs .tmc .cli .utils .BiConsumerWithException ;
11+ import fi .helsinki .cs .tmc .cli .utils .PropertyFunctions ;
1212import fi .helsinki .cs .tmc .core .domain .ProgressObserver ;
1313import fi .helsinki .cs .tmc .core .holders .TmcSettingsHolder ;
1414import fi .helsinki .cs .tmc .core .utilities .TmcServerAddressNormalizer ;
@@ -26,10 +26,16 @@ public class ConfigCommand extends AbstractCommand {
2626
2727 private CliContext context ;
2828 private Io io ;
29- private static final Map <String , BiConsumerWithException <String , Object >> ALLOWED_KEYS = new HashMap <>();
30- private static final Set <String > PROGRESS_BAR_COLORS = new HashSet <String >(Arrays .asList (new String [] {
31- "black" , "red" , "green" , "blue" , "yellow" , "blue" , "purple" , "cyan" , "white" , "none"
32- }));
29+ private static final Map <String , PropertyFunctions > ALLOWED_KEYS = new HashMap <>();
30+ private static final Set <String > PROGRESS_BAR_COLORS = new HashSet <>(Arrays .asList ("black" , "red" , "green" , "blue" , "yellow" , "blue" , "purple" , "cyan" , "white" , "none" ));
31+ private static final String sendAnalyticsKey = "send-analytics" ;
32+ private static final String serverAddressKey = "server-address" ;
33+ private static final String updateDateKey = "update-date" ;
34+ private static final String testResultRightKey = "testresults-right" ;
35+ private static final String testResultLeftKey = "testresults-left" ;
36+ private static final String progressBarLeftKey = "progressbar-left" ;
37+ private static final String progressBarRightKey = "progressbar-right" ;
38+ private static final String sendDiagnosticsKey = "send-diagnostics" ;
3339
3440 private HashMap <String , String > properties ;
3541 private boolean quiet ;
@@ -40,46 +46,117 @@ public ConfigCommand() {
4046
4147 private void configureAllowedKeys () {
4248 // add new possible config options here
43- // each key has a BiConsumer function which validates the value and saves it in settings or properties
44- ALLOWED_KEYS .put ("send-diagnostics" , (key , value ) -> {
45- boolean send = getBooleanSendValue ((String ) value );
46- context .getSettings ().setSendDiagnostics (send );
47- SettingsIo .saveCurrentSettingsToAccountList (context .getSettings ());
49+ // create an anonymous class which determines where the value is stored
50+ // some values are stored in settings, some in properties
51+ ALLOWED_KEYS .put (sendDiagnosticsKey , new PropertyFunctions () {
52+ @ Override
53+ public String getter () {
54+ return Boolean .toString (context .getSettings ().getSendDiagnostics ());
55+ }
56+
57+ @ Override
58+ public void setter (String value ) throws BadValueTypeException {
59+ boolean send = getBooleanSendValue (value );
60+ context .getSettings ().setSendDiagnostics (send );
61+ SettingsIo .saveCurrentSettingsToAccountList (context .getSettings ());
62+ }
4863 });
49- ALLOWED_KEYS .put ("send-analytics" , (key , value ) -> {
50- boolean send = getBooleanSendValue ((String ) value );
51- context .getSettings ().setSpywareEnabled (send );
52- SettingsIo .saveCurrentSettingsToAccountList (context .getSettings ());
64+ ALLOWED_KEYS .put ("send-analytics" , new PropertyFunctions () {
65+ @ Override
66+ public String getter () {
67+ return Boolean .toString (context .getSettings ().isSpywareEnabled ());
68+ }
69+
70+ @ Override
71+ public void setter (String value ) throws BadValueTypeException {
72+ boolean send = getBooleanSendValue (value );
73+ context .getSettings ().setSpywareEnabled (send );
74+ SettingsIo .saveCurrentSettingsToAccountList (context .getSettings ());
75+ }
5376 });
54- ALLOWED_KEYS .put ("server-address" , ( key , address ) -> {
55- String addr = ( String ) address ;
56- if (! addr . matches ( "^https?://.*" ) ) {
57- throw new BadValueTypeException ( "Please start the address with http[s]://" );
77+ ALLOWED_KEYS .put ("server-address" , new PropertyFunctions () {
78+ @ Override
79+ public String getter ( ) {
80+ return context . getSettings (). getServerAddress ( );
5881 }
59- context .getSettings ().setServerAddress (addr );
60- if (!normalizeServerAddress ()) {
61- io .println ("There was a problem setting the server address." );
62- return ;
82+
83+ @ Override
84+ public void setter (String value ) throws BadValueTypeException {
85+ String addr = value ;
86+ if (!addr .matches ("^https?://.*" )) {
87+ throw new BadValueTypeException ("Please start the address with http[s]://" );
88+ }
89+ context .getSettings ().setServerAddress (addr );
90+ if (!normalizeServerAddress ()) {
91+ io .println ("There was a problem setting the server address." );
92+ return ;
93+ }
94+ SettingsIo .saveCurrentSettingsToAccountList (context .getSettings ());
6395 }
64- SettingsIo .saveCurrentSettingsToAccountList (context .getSettings ());
6596 });
66- ALLOWED_KEYS .put ("update-date" , (key , value ) -> {
67- String date = (String ) value ;
68- if (!date .matches ("[0-9]+" )) {
69- throw new BadValueTypeException ("Please insert the date as a number" );
97+ ALLOWED_KEYS .put ("update-date" , new PropertyFunctions () {
98+ @ Override
99+ public String getter () {
100+ return context .getProperties ().get ("update-date" );
101+ }
102+
103+ @ Override
104+ public void setter (String date ) throws BadValueTypeException {
105+ if (!date .matches ("[0-9]+" )) {
106+ throw new BadValueTypeException ("Please insert the date as a number" );
107+ }
108+ properties .put ("update-date" , date );
109+ }
110+ });
111+ ALLOWED_KEYS .put ("testresults-right" , new PropertyFunctions () {
112+ @ Override
113+ public String getter () {
114+ return context .getProperties ().get ("testresults-right" );
115+ }
116+
117+ @ Override
118+ public void setter (String value ) throws BadValueTypeException {
119+ addBarColorToProperties ("testresults-right" , value );
120+ }
121+ });
122+ ALLOWED_KEYS .put ("testresults-left" , new PropertyFunctions () {
123+ @ Override
124+ public String getter () {
125+ return context .getProperties ().get ("testresults-right" );
126+ }
127+
128+ @ Override
129+ public void setter (String value ) throws BadValueTypeException {
130+ addBarColorToProperties ("testresults-right" , value );
131+ }
132+ });
133+ ALLOWED_KEYS .put ("progressbar-left" , new PropertyFunctions () {
134+ @ Override
135+ public String getter () {
136+ return context .getProperties ().get ("testresults-right" );
137+ }
138+
139+ @ Override
140+ public void setter (String value ) throws BadValueTypeException {
141+ addBarColorToProperties ("testresults-right" , value );
142+ }
143+ });
144+ ALLOWED_KEYS .put ("progressbar-right" , new PropertyFunctions () {
145+ @ Override
146+ public String getter () {
147+ return context .getProperties ().get ("testresults-right" );
148+ }
149+
150+ @ Override
151+ public void setter (String value ) throws BadValueTypeException {
152+ addBarColorToProperties ("testresults-right" , value );
70153 }
71- properties .put ("update-date" , date );
72154 });
73- ALLOWED_KEYS .put ("testresults-right" , this ::addBarColorToProperties );
74- ALLOWED_KEYS .put ("testresults-left" , this ::addBarColorToProperties );
75- ALLOWED_KEYS .put ("progressbar-left" , this ::addBarColorToProperties );
76- ALLOWED_KEYS .put ("progressbar-right" , this ::addBarColorToProperties );
77155 }
78156
79157 private boolean getBooleanSendValue (String value ) throws BadValueTypeException {
80- String newVal = value ;
81- isBooleanValue (newVal );
82- return Boolean .parseBoolean (newVal );
158+ isBooleanValue (value );
159+ return Boolean .parseBoolean (value );
83160 }
84161
85162 private void isBooleanValue (String newVal ) throws BadValueTypeException {
@@ -167,11 +244,11 @@ public void run(CliContext context, CommandLine args) {
167244
168245 private void printAllProperties () {
169246 // how to handle that some values are stored in settings and some in properties?
170- ALLOWED_KEYS .keySet ().stream ().sorted ().forEach (k -> {
171- String property = properties .get (k );
172- io .println (k + "=" + (property != null ? property : "<not set>" ));
247+ ALLOWED_KEYS .entrySet ().stream ().forEach (e -> {
248+ String key = e .getKey ();
249+ String value = e .getValue ().getter ();
250+ io .println (key + "=" + ( value != null ? value : "<no value set>" ));
173251 });
174-
175252 }
176253
177254 private void deleteProperties (String [] keys ) {
@@ -254,8 +331,7 @@ private void setPropertiesQuietly(String[] arguments) {
254331
255332 private boolean saveValue (String key , String value ) {
256333 try {
257- ALLOWED_KEYS .get (key ).apply (key , value );
258-
334+ ALLOWED_KEYS .get (key ).setter (value );
259335 } catch (Exception e ) {
260336 io .errorln (e .getMessage ());
261337 return false ;
@@ -273,10 +349,9 @@ private boolean checkIfAllowedKey(String key) {
273349 return true ;
274350 }
275351
276- private void addBarColorToProperties (String key , Object value ) throws BadValueTypeException {
277- String color = (String ) value ;
352+ private void addBarColorToProperties (String key , String color ) throws BadValueTypeException {
278353 if (!PROGRESS_BAR_COLORS .contains (color )) {
279- throw new BadValueTypeException ("Color " + value + " not supported." );
354+ throw new BadValueTypeException ("Color " + color + " not supported." );
280355 }
281356 properties .put (key , color );
282357 SettingsIo .saveProperties (properties );
0 commit comments