lib/_provisioning/update-config.js
function updateBrokerListInConfigFile (configFileName, brokerLines, verbosity) {
  if (verbosity) {
    console.log('Updating DXL config file at ' + configFileName)
  }

  var inBrokerSection = false
  var linesAfterBrokerSection = []
  var configFileData = util.getConfigFileData(configFileName)

  // Replace the current contents of the broker section with the new broker
  // lines. Preserve existing content in the file which relates to other
  // sections - for example, the 'Certs' section.
  var newConfigLines = configFileData.lines.reduce(
    function (acc, line) {
      if (line === '[Brokers]') {
        inBrokerSection = true
        acc.push(line)
      } else {
        if (inBrokerSection) {
          // Skip over any existing lines in the broker section since these
          // will be replaced.
          if (line.match(/^\s*($|[;#])/)) {
            // Preserve any empty/comment lines if there is another section
            // after the Broker section. Comments may pertain to the content of
            // the next section.
            linesAfterBrokerSection.push(line)
          } else if (line.match(/^\[.*]$/)) {
            // A section after the broker section has been reached.
            inBrokerSection = false
            Array.prototype.push.apply(acc, brokerLines)
            if (linesAfterBrokerSection.length === 0) {
              newConfigLines.push(configFileData.lineSeparator)
            } else {
              Array.prototype.push.apply(acc,
                linesAfterBrokerSection)
            }
            acc.push(line)
          } else {
            // A broker config entry in the existing file, which will be
            // replaced by the new broker lines, has been encountered. Any
            // prior empty/comment lines which had been stored so far would
            // be between broker config entries. Those lines should be dropped
            // since they might not line up with the new broker list data.
            linesAfterBrokerSection = []
          }
        } else {
          acc.push(line)
        }
      }
      return acc
    }, []
  )

  if (inBrokerSection) {
    Array.prototype.push.apply(newConfigLines, brokerLines)
  }
  // Flatten the broker line array back into a string, delimited using the
  // line separator which appeared to be used in the original config file.
  var newConfig = newConfigLines.join(configFileData.lineSeparator)
  if (inBrokerSection) {
    newConfig += configFileData.lineSeparator
  }

  provisionUtil.saveToFile(configFileName, newConfig)
}
function updateBrokerListInConfigFile (configFileName, brokerLines, verbosity) {
  if (verbosity) {
    console.log('Updating DXL config file at ' + configFileName)
  }

  var inBrokerSection = false
  var linesAfterBrokerSection = []
  var configFileData = util.getConfigFileData(configFileName)

  // Replace the current contents of the broker section with the new broker
  // lines. Preserve existing content in the file which relates to other
  // sections - for example, the 'Certs' section.
  var newConfigLines = configFileData.lines.reduce(
    function (acc, line) {
      if (line === '[Brokers]') {
        inBrokerSection = true
        acc.push(line)
      } else {
        if (inBrokerSection) {
          // Skip over any existing lines in the broker section since these
          // will be replaced.
          if (line.match(/^\s*($|[;#])/)) {
            // Preserve any empty/comment lines if there is another section
            // after the Broker section. Comments may pertain to the content of
            // the next section.
            linesAfterBrokerSection.push(line)
          } else if (line.match(/^\[.*]$/)) {
            // A section after the broker section has been reached.
            inBrokerSection = false
            Array.prototype.push.apply(acc, brokerLines)
            if (linesAfterBrokerSection.length === 0) {
              newConfigLines.push(configFileData.lineSeparator)
            } else {
              Array.prototype.push.apply(acc,
                linesAfterBrokerSection)
            }
            acc.push(line)
          } else {
            // A broker config entry in the existing file, which will be
            // replaced by the new broker lines, has been encountered. Any
            // prior empty/comment lines which had been stored so far would
            // be between broker config entries. Those lines should be dropped
            // since they might not line up with the new broker list data.
            linesAfterBrokerSection = []
          }
        } else {
          acc.push(line)
        }
      }
      return acc
    }, []
  )

  if (inBrokerSection) {
    Array.prototype.push.apply(newConfigLines, brokerLines)
  }
  // Flatten the broker line array back into a string, delimited using the
  // line separator which appeared to be used in the original config file.
  var newConfig = newConfigLines.join(configFileData.lineSeparator)
  if (inBrokerSection) {
    newConfig += configFileData.lineSeparator
  }

  provisionUtil.saveToFile(configFileName, newConfig)
}
function updateBrokerListInConfigFile ( configFileName , brokerLines , verbosity ) { if ( verbosity ) { console . log ( 'Updating DXL config file at ' + configFileName ) } var inBrokerSection = false var linesAfterBrokerSection = [ ] var configFileData = util . getConfigFileData ( configFileName ) // Replace the current contents of the broker section with the new broker // lines. Preserve existing content in the file which relates to other // sections - for example, the 'Certs' section. var newConfigLines = configFileData . lines . reduce ( function ( acc , line ) { if ( line === '[Brokers]' ) { inBrokerSection = true acc . push ( line ) } else { if ( inBrokerSection ) { // Skip over any existing lines in the broker section since these // will be replaced. if ( line . match ( / ^\s*($|[;#]) / ) ) { // Preserve any empty/comment lines if there is another section // after the Broker section. Comments may pertain to the content of // the next section. linesAfterBrokerSection . push ( line ) } else if ( line . match ( / ^\[.*]$ / ) ) { // A section after the broker section has been reached. inBrokerSection = false Array . prototype . push . apply ( acc , brokerLines ) if ( linesAfterBrokerSection . length === 0 ) { newConfigLines . push ( configFileData . lineSeparator ) } else { Array . prototype . push . apply ( acc , linesAfterBrokerSection ) } acc . push ( line ) } else { // A broker config entry in the existing file, which will be // replaced by the new broker lines, has been encountered. Any // prior empty/comment lines which had been stored so far would // be between broker config entries. Those lines should be dropped // since they might not line up with the new broker list data. linesAfterBrokerSection = [ ] } } else { acc . push ( line ) } } return acc } , [ ] ) if ( inBrokerSection ) { Array . prototype . push . apply ( newConfigLines , brokerLines ) } // Flatten the broker line array back into a string, delimited using the // line separator which appeared to be used in the original config file. var newConfig = newConfigLines . join ( configFileData . lineSeparator ) if ( inBrokerSection ) { newConfig += configFileData . lineSeparator } provisionUtil . saveToFile ( configFileName , newConfig ) }
Updates the broker section in a client configuration file with a list of
broker data lines.
@param {String} configFileName - Name of the configuration file to update.
@param {Array<String>} brokerLines - List of broker data strings to insert
into the configuration file.
@param {Number} verbosity - Level of verbosity at which to log any error
or trace messages.
@private
Updates the broker section in a client configuration file with a list of broker data lines .