@@ -116,36 +116,38 @@ static Path getConfigDirectory() {
116116 return configPath .resolve (CONFIG_DIR );
117117 }
118118
119- //TODO handle exceptions
120119 private static Path getAccountsFile (Path configRoot ) {
121120 Path file = configRoot .resolve (ACCOUNTS_CONFIG );
122- if (!Files .exists (configRoot )) {
123- try {
124- Files .createDirectories (configRoot ).getParent ();
125- } catch (IOException e ) {
126- }
127- try {
128- Files .createFile (configRoot );
129- } catch (Exception e ) {
130- }
121+ if (!requireConfigDirectory (configRoot )) {
122+ return null ;
131123 }
132124 return file ;
133125 }
134126
135- //TODO handle exceptions
136127 private static Path getPropertiesFile (Path configRoot ) {
137128 Path file = configRoot .resolve (PROPERTIES_CONFIG );
129+ if (!requireConfigDirectory (configRoot )) {
130+ return null ;
131+ }
132+ return file ;
133+ }
134+
135+ /**
136+ * Create the config directory if it doesn't already exist.
137+ * @param configRoot config directory
138+ * @return false if directory can't be created.
139+ */
140+ private static boolean requireConfigDirectory (Path configRoot ) {
138141 if (!Files .exists (configRoot )) {
139142 try {
140- Files .createDirectories (configRoot ).getParent ();
141- } catch (Exception e ) {
142- }
143- try {
144- Files .createFile (configRoot );
143+ Files .createDirectories (configRoot );
145144 } catch (Exception e ) {
145+ //TODO print error to user
146+ logger .error ("Could not create config directory" , e );
147+ return false ;
146148 }
147149 }
148- return file ;
150+ return true ;
149151 }
150152
151153 private static AccountList getHolderFromJson (Path file ) {
@@ -154,6 +156,7 @@ private static AccountList getHolderFromJson(Path file) {
154156 try {
155157 reader = Files .newBufferedReader (file , Charset .forName ("UTF-8" ));
156158 } catch (IOException e ) {
159+ //TODO print error to user
157160 logger .error ("Accounts file located, but failed to read from it" , e );
158161 return null ;
159162 }
@@ -166,6 +169,7 @@ private static boolean saveHolderToJson(AccountList holder, Path file) {
166169 try {
167170 Files .write (file , json );
168171 } catch (IOException e ) {
172+ //TODO print error to user
169173 logger .error ("Could not write account to accounts file" , e );
170174 return false ;
171175 }
@@ -178,6 +182,7 @@ private static HashMap<String, String> getPropertiesFromJson(Path file) {
178182 try {
179183 reader = Files .newBufferedReader (file , Charset .forName ("UTF-8" ));
180184 } catch (IOException e ) {
185+ //TODO print error to user
181186 logger .error ("Properties file located, but failed to read from it" , e );
182187 return null ;
183188 }
@@ -192,6 +197,7 @@ private static boolean savePropertiesToJson(HashMap<String, String> properties,
192197 try {
193198 Files .write (file , json );
194199 } catch (IOException e ) {
200+ //TODO print error to user
195201 logger .error ("Could not write properties to file" , e );
196202 return false ;
197203 }
0 commit comments