Completed
Push — master ( c8cc0a...f17656 )
by Thomas
44s
created

lib/containers/ContainerConfig.js   A

Complexity

Total Complexity 18
Complexity/F 2.57

Size

Lines of Code 100
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 18
c 1
b 0
f 0
nc 24
mnd 1
bc 15
fnc 7
dl 0
loc 100
rs 10
bpm 2.1427
cpm 2.5714
noi 1

5 Functions

Rating   Name   Duplication   Size   Complexity  
A ContainerConfig.read 0 17 1
A ContainerConfig.getPath 0 3 1
B ContainerConfig.setup 0 14 6
A ContainerConfig.validate 0 10 2
A ContainerConfig.write 0 11 1
1
'use strict'
2
3
const jsonfile = require('jsonfile')
4
const mkdirp = require('mkdirp')
5
const path = require('path')
6
const fs = require('fs')
7
8
var ContainerConfig = {}
9
10
/**
11
 * Validate a config object.
12
 *
13
 * @param {ContainerConfigObject} config
14
 * @returns {boolean}
15
 */
16
ContainerConfig.validate = function (config) {
17
  config.containerToken = config.containerToken || config.container_token
18
19
  if ('containerToken' in config) {
20
    ContainerConfig.setup(config)
21
    return true
22
  }
23
24
  return false
25
}
26
27
/**
28
 * Set up the app's globals using config values.
29
 *
30
 * @param {ContainerConfigObject} config
31
 */
32
ContainerConfig.setup = function (config) {
33
  if ('containerEndpoint' in config) {
34
    global.containerEndpoint = config.containerEndpoint
35
  }
36
  if ('version' in config) {
37
    global.apiVersion = config.version
38
  }
39
  if ('stability' in config) {
40
    global.apiStability = config.stability
41
  }
42
  if ('containerEndpointCheckSSL' in config && !config.containerEndpointCheckSSL) {
43
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
44
  }
45
}
46
47
/**
48
 * Get the correct (relative) path for a container config file.
49
 *
50
 * @param {string} name
51
 * @returns {string}
52
 */
53
ContainerConfig.getPath = function (name) {
54
  return path.join('.inc', 'containers', name + '.json')
55
}
56
57
/**
58
 * Read a container config file. If the file with the supplied container name doesn't exist,
59
 * this falls back to the 'default' container, if that one exists.
60
 *
61
 * @param {string} name
62
 * @param {function(Error, ContainerConfigObject)} cb
63
 */
64
ContainerConfig.read = function (name, cb) {
65
  jsonfile.readFile(ContainerConfig.getPath(name), function (err, config) {
66
    // Container file exists and is a JSON object, so check if it is valid
67
    if (!err && typeof config === 'object' && ContainerConfig.validate(config)) {
68
      // Yep, return in callback!
69
      return cb(null, config)
70
    }
71
72
    // Try default file if in `beta` mode
73
    if (name === 'beta') {
74
      return ContainerConfig.read('default', cb)
75
    }
76
77
    // No valid container found
78
    cb(err || new Error('Container config not found or invalid'), null)
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
79
  })
80
}
81
82
/**
83
 * Write to the config file for a specific container.
84
 *
85
 * @param {string} name
86
 * @param {object} content
87
 */
88
ContainerConfig.write = function (name, content) {
89
  // Ensure the containers directory exists
90
  mkdirp(path.join('.inc', 'containers'), function () {
91
    // Write JSON file
92
    jsonfile.writeFile(ContainerConfig.getPath(name), content)
93
    // Write package version file
94
    if (!fs.existsSync(path.join('.inc', 'version'))) {
95
      fs.writeFileSync(path.join('.inc', 'version'), global.containerVersion.toString(), 'utf8')
96
    }
97
  })
98
}
99
100
module.exports = ContainerConfig
101