11var  _  =  require ( 'underscore' ) ; 
22var  fs  =  require ( 'fs' ) ; 
33var  path  =  require ( 'path' ) ; 
4+ var  utils  =  require ( '../utils.js' ) ; 
45
56var  logger  =  require ( '../logging.js' ) ; 
67
@@ -21,7 +22,7 @@ exports.setToken = function(tok) {
2122
2223var  write_content_to_consul  =  function ( key_name ,  content ,  cb )  { 
2324  logger . trace ( 'Adding key %s, value:\n%s' ,  key_name ,  content ) ; 
24-   consul . kv . set ( { 'key' : key_name ,  value : content ,  token : token } ,  function ( err )  { 
25+   consul . kv . set ( { 'key' : key_name ,  value : content   !==   null  ?  String ( content )  :  null ,  token : token } ,  function ( err )  { 
2526    if  ( err )  { 
2627      return  cb ( 'Failed to write key '  +  key_name  +  ' due to '  +  err ) ; 
2728    } 
@@ -54,7 +55,7 @@ var create_key_name = function(branch, file, ref) {
5455/** 
5556 * Given an obj, recurse into it, populating the parts array with all of the key->value 
5657 * relationships, prefixed by parent objs. 
57-  *   
58+  * 
5859 * For example, the obj { 'first': { 'second': { 'third' : 'whee' }}} should yield a 
5960 * parts array with a single entry: 'first/second/third' with value 'whee'. 
6061 */ 
@@ -74,7 +75,7 @@ var render_obj = function(parts, prefix, obj) {
7475 * as parents of subtrees.  Arrays are ignored since they add annoying problems (multiple array 
7576 * entries can have the same value, so what do we do then?). 
7677 */ 
77- var  populate_kvs_from_json  =  function ( branch ,  prefix ,  obj ,  cb )  { 
78+ var  populate_kvs_from_object  =  function ( branch ,  prefix ,  obj ,  cb )  { 
7879
7980  var  writes  =  [ ] ; 
8081
@@ -89,41 +90,94 @@ var populate_kvs_from_json = function(branch, prefix, obj, cb) {
8990  } ) ; 
9091} ; 
9192
93+ 
9294/** 
9395 * If a file was modified, read its new value and update consul's KV store. 
9496 */ 
9597var  file_modified  =  function ( branch ,  file ,  cb )  { 
9698
97-   var  fqf  =  branch . branch_directory  +  path . sep  +  file ; 
98- 
99-   logger . trace ( 'Attempting to read "%s"' ,  fqf ) ; 
100- 
101-   if  ( branch . expand_keys  &&  file . endsWith ( '.json' ) )  { 
102-     // Delete current tree.  Yeah, I know this is kinda the coward's way out, but it's a hell of a lot 
103-     // easier to get provably correct than diffing the file against the contents of the KV store. 
104-     file_deleted ( branch ,  file ,  function ( err )  { 
105-       if  ( err )  return  cb ( 'Failed to delete key '  +  key_name  +  ' due to '  +  err ) ; 
106- 
107-       fs . readFile ( fqf ,  { encoding :'utf8' } ,  function ( err ,  body )  { 
99+   var  handle_json_kv_file  =  function ( file_path ,  cb )  { 
100+       fs . readFile ( file_path ,  { encoding : 'utf8' } ,  function  ( err ,  body )  { 
108101        /* istanbul ignore if */ 
109-         if  ( err )  return  cb ( 'Failed to read key '  +  fqf  +  ' due to '  +  err ) ; 
102+         if  ( err )  return  cb ( 'Failed to read key '  +  file_path  +  ' due to '  +  err ) ; 
110103        var  body  =  body  ? body . trim ( )  : '' ; 
111104        try  { 
112105          var  obj  =  JSON . parse ( body ) ; 
113-           populate_kvs_from_json ( branch ,  create_key_name ( branch ,  file ) ,  obj ,  cb ) ; 
114-         }  catch ( e )  { 
106+           populate_kvs_from_object ( branch ,  create_key_name ( branch ,  file ) ,  obj ,  cb ) ; 
107+         }  catch   ( e )  { 
115108          logger . warn ( "Failed to parse .json file.  Using body string as a KV." ) ; 
116109          write_content_to_consul ( create_key_name ( branch ,  file ) ,  body ,  cb ) ; 
117110        } 
118111      } ) ; 
112+   } ; 
113+ 
114+   var  handle_properties_kv_file  =  function ( file_path ,  common_properties_relative_path ,  cb )  { 
115+ 
116+     function  extract_and_populate_properties ( file_body ,  common_body ,  cb )  { 
117+       utils . load_properties ( file_body ,  common_body ,  function  ( error ,  obj )  { 
118+         if  ( error )  { 
119+           logger . warn ( 'Failed to load properties for : '  +  file  +  ' due to : '  +  error  +  '.'  +  ' Using body string as a KV.' ) ; 
120+           handle_as_flat_file ( file_path ,  branch ,  file ,  cb ) ; 
121+         }  else  { 
122+           populate_kvs_from_object ( branch ,  create_key_name ( branch ,  file ) ,  obj ,  cb ) ; 
123+         } 
124+       } ) ; 
125+     } 
126+ 
127+     fs . readFile ( file_path ,  { encoding : 'utf8' } ,  function  ( err ,  file_body )  { 
128+       /* istanbul ignore if */ 
129+       if  ( err )  return  cb ( 'Failed to read key '  +  file_path  +  ' due to '  +  err ) ; 
130+ 
131+       if ( common_properties_relative_path )  { 
132+         var  path_to_common_properties_file  =  branch . branch_directory  +  path . sep  +  common_properties_relative_path ; 
133+         fs . readFile ( path_to_common_properties_file ,  { encoding : 'utf8' } ,  function  ( err ,  common_body )  { 
134+           if  ( err )  { 
135+             logger . warn ( 'Failed to read common variables for '  +  path_to_common_properties_file  +  ' due to '  +  err ) ; 
136+             common_body  =  '' ; 
137+           } 
138+           extract_and_populate_properties ( file_body ,  common_body ,  cb ) ; 
139+         } ) ; 
140+       } 
141+       else  { 
142+         extract_and_populate_properties ( file_body ,  '' ,  cb ) ; 
143+       } 
119144    } ) ; 
120-   }  else  { 
121-     fs . readFile ( fqf ,  { encoding :'utf8' } ,  function ( err ,  body )  { 
145+   } ; 
146+ 
147+   var  handle_as_flat_file  =  function ( fqf ,  branch ,  file ,  cb )  { 
148+     fs . readFile ( fqf ,  { encoding : 'utf8' } ,  function  ( err ,  body )  { 
122149      /* istanbul ignore if */ 
123150      if  ( err )  return  cb ( 'Failed to read key '  +  fqf  +  ' due to '  +  err ) ; 
124151      var  body  =  body  ? body . trim ( )  : '' ; 
125152      write_content_to_consul ( create_key_name ( branch ,  file ) ,  body ,  cb ) ; 
126153    } ) ; 
154+   } ; 
155+ 
156+   var  handle_expended_keys_with_different_file_types  =  function ( )  { 
157+     if  ( file . endsWith ( '.json' ) )  { 
158+       // Delete current tree.  Yeah, I know this is kinda the coward's way out, but it's a hell of a lot 
159+       // easier to get provably correct than diffing the file against the contents of the KV store. 
160+       file_deleted ( branch ,  file ,  function  ( err )  { 
161+         if  ( err )  return  cb ( 'Failed to delete key '  +  key_name  +  ' due to '  +  err ) ; 
162+         handle_json_kv_file ( fqf ,  cb ) ; 
163+       } ) ; 
164+     }  else  if  ( file . endsWith ( '.properties' ) )  { 
165+       file_deleted ( branch ,  file ,  function  ( err )  { 
166+         if  ( err )  return  cb ( 'Failed to delete key '  +  key_name  +  ' due to '  +  err ) ; 
167+         handle_properties_kv_file ( fqf ,  branch . common_properties ,  cb ) ; 
168+       } ) ; 
169+     }  else  { 
170+       handle_as_flat_file ( fqf ,  branch ,  file ,  cb ) ; 
171+     } 
172+   } ; 
173+ 
174+   var  fqf  =  branch . branch_directory  +  path . sep  +  file ; 
175+   logger . trace ( 'Attempting to read "%s"' ,  fqf ) ; 
176+ 
177+   if  ( branch . expand_keys )  { 
178+     handle_expended_keys_with_different_file_types ( ) ; 
179+   }  else  { 
180+     handle_as_flat_file ( fqf ,  branch ,  file ,  cb ) ; 
127181  } 
128182} ; 
129183
0 commit comments