var tools = require('../tools')
, print = require('../print')

var apikey = function (cmd, _config) {
  var add_key = function (name, key) {
    var conf = _config
    if (!name || !key) return print.error('Insufficient parameters. See "digio -h" for help')
    if (conf.names.indexOf(name) != -1) {
      return print.error('A key with that name already exists. Please try another name')
    }
    conf.names.push(name)
    conf.keys.push(key)
    conf.used = conf.names.indexOf(name)

    tools.write_config(conf, function (err, c) {
      if (err) return print.error('Unable to update config file!')
      print.success('Added key "' + name + '" with id ' +
        (c.names.indexOf(name) + 1) + ' and set it to active.')
    })
  }

  var use_key = function (name) {
    var conf = _config
    if (conf.names.indexOf(name) > -1) {
      conf.used = conf.names.indexOf(name)
      tools.write_config(conf, function (err, c) {
        if (err) return print.error('Unable to update config file!')
        print.success('Active API-key changed to "' +
          name + '" ('+
          conf.keys[conf.used] +
          ')')
      })
    }
    else return print.error('No key named "' + name + '". See "digio apikey list" for a complete list of keys.')
  }

  var list_keys = function () {
    var conf = _config
    if (conf.keys.length === 0) return print.info('You have not added any API-keys to your config file yet, see "digio -h" for help.')

    keys = []
    for (var i = 0; i < conf.keys.length; i++) {
      var line = conf.names[i] + ' - ' + conf.keys[i]
      if (conf.used === i) line += ' (Active)'
      keys.push(line)
    }
    console.log('\nTotal API-keys: ' + conf.keys.length + '\n')
    print.list(keys)
    console.log('')
  }

  var delete_key = function (name) {
    var conf = _config

    var i = conf.names.indexOf(name)
    if (i < 0) return print.error('No such key "' + name + '"')
    if (conf.used === i) conf.used = 0
    conf.keys.splice(i, 1)
    conf.names.splice(i, 1)
    tools.write_config(conf, function (err, c) {
      if (err) print.error('Unable to update config file!')
      print.success('Deleted key "' + name + '"')
    })
  }

  cmd.register(
    'apikey',
    'list',
    [],
    'Display a list of all saved API-keys',
    list_keys
  )

  cmd.register(
    'apikey',
    'add',
    ['<name>', '<key>'],
    'Add a new API-key and set it to active',
    add_key
  )

  cmd.register(
    'apikey',
    'use',
    ['<name>'],
    'Select which API-key to use when connecting',
    use_key
  )

  cmd.register(
    'apikey',
    'delete',
    ['<name>'],
    'Remove an API-key from the config file',
    delete_key
  )

}

module.exports = apikey

@@ -149,32 +149,33 @@
  conf = _config%0A
+%0A
     if (!name %7C%7C
@@ -180,16 +180,22 @@
 %7C%7C !key)
+%0A     
  return 
@@ -258,16 +258,17 @@
  help')%0A
+%0A
     if (
@@ -294,26 +294,24 @@
 name) != -1)
- %7B
 %0A      retur
@@ -388,21 +388,16 @@
  name')%0A
-    %7D
 %0A    con
@@ -589,32 +589,33 @@
  config file!')%0A
+%0A
       print.succ
@@ -989,24 +989,25 @@
 fig file!')%0A
+%0A
         prin
@@ -1047,26 +1047,16 @@
  to %22' +
-%0A         
  name + 
@@ -1061,26 +1061,16 @@
 + '%22 ('+
-%0A         
  conf.ke
@@ -1088,18 +1088,8 @@
 d%5D +
-%0A         
  ')'
@@ -1263,39 +1263,19 @@
     
-var conf = _config%0A    if (conf
+if (_config
 .key
@@ -1432,20 +1432,23 @@
  0; i %3C 
+_
 conf
+ig
 .keys.le
@@ -1477,20 +1477,23 @@
  line = 
+_
 conf
+ig
 .names%5Bi
@@ -1500,28 +1500,31 @@
 %5D + ' - ' + 
+_
 conf
+ig
 .keys%5Bi%5D%0A   
@@ -1520,16 +1520,17 @@
 keys%5Bi%5D%0A
+%0A
       if
@@ -1523,36 +1523,39 @@
 s%5Bi%5D%0A%0A      if (
+_
 conf
+ig
 .used === i) lin
@@ -1550,16 +1550,24 @@
 d === i)
+%0A       
  line +=
@@ -1579,16 +1579,17 @@
 ctive)'%0A
+%0A
       ke
@@ -1647,20 +1647,23 @@
 ys: ' + 
+_
 conf
+ig
 .keys.le
@@ -1815,24 +1815,25 @@
 dexOf(name)%0A
+%0A
     if (i %3C 
@@ -1923,16 +1923,17 @@
 sed = 0%0A
+%0A
     conf
@@ -1975,24 +1975,25 @@
 plice(i, 1)%0A
+%0A
     tools.wr
@@ -2089,16 +2089,17 @@
 file!')%0A
+%0A
       pr

Fix code consistency - make it easier to read