Skip to content

Commit

Permalink
fix: support starting up with an empty or missing settings file and …
Browse files Browse the repository at this point in the history
…using -c (#430)

Support starting up with an empty or missing settings file. If defaults.json it does not exist, generate a uuid and save it.
  • Loading branch information
sbender9 authored and tkurki committed Jan 29, 2018
1 parent f8509e8 commit a168004
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
43 changes: 32 additions & 11 deletions lib/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const express = require('express')
const debug = require('debug')('signalk-server:config')
const _ = require('lodash')
const fs = require('fs')
const uuidv5 = require('uuid/v5')

function load (app) {
app.__argv = process.argv.slice(2)
Expand Down Expand Up @@ -88,18 +89,27 @@ function setConfigDirectory (app) {

if (!fs.existsSync(app.config.configPath)) {
fs.mkdirSync(app.config.configPath)
}
} else {
app.config.configPath = app.argv.c || app.config.appPath
}

if (app.config.configPath != app.config.appPath) {
var configPackage = path.join(app.config.configPath, 'package.json')
if (!fs.existsSync(configPackage)) {
fs.writeFileSync(
path.join(app.config.configPath, 'package.json'),
configPackage,
JSON.stringify(pluginsPackageJsonTemplate, null, 2)
)
}
} else {
app.config.configPath = app.argv.c || app.config.appPath
}
}

function getDefaultsPath (app) {
const defaultsFile = app.argv.c ? 'defaults.json' : 'settings/defaults.json'
const defaultsFile =
app.config.configPath != app.config.appPath
? 'defaults.json'
: 'settings/defaults.json'
return path.join(app.config.configPath, defaultsFile)
}

Expand Down Expand Up @@ -160,6 +170,16 @@ function setSelfSettings (app) {
throw new Error(`invalid uuid: ${uuid}`)
}

if (_.isUndefined(mmsi) && _.isUndefined(uuid)) {
uuid = 'urn:mrn:signalk:uuid:' + uuidv5('signalk.org', uuidv5.DNS)
_.set(app.config.defaults, 'vessels.self.uuid', uuid)
writeDefaultsFile(app, app.config.defaults, err => {
if (err) {
console.error(`unable to write defaults file: ${err}`)
}
})
}

if (mmsi) {
app.selfType = 'mmsi'
app.selfId = 'urn:mrn:imo:mmsi:' + mmsi
Expand All @@ -175,18 +195,19 @@ function setSelfSettings (app) {

function readSettingsFile (app) {
const settings = getSettingsFilename(app)
if (
!app.argv.c &&
!app.argv.s &&
process.env.HOME &&
!fs.existsSync(settings)
) {
if (!app.argv.s && !fs.existsSync(settings)) {
console.log('Settings file does not exist, using empty settings')
app.config.settings = { pipedProviders: [] }
app.config.settings = {}
} else {
debug('Using settings file: ' + settings)
app.config.settings = require(settings)
}
if (_.isUndefined(app.config.settings.pipedProviders)) {
app.config.settings.pipedProviders = []
}
if (_.isUndefined(app.config.settings.interfaces)) {
app.config.settings.interfaces = {}
}
}

function writeSettingsFile (app, settings, cb) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"stat-mode": "^0.2.2",
"stream-throttle": "^0.1.3",
"through": ">=2.2.7 <3",
"uuid": "^3.2.1",
"ws": "^4.0.0",
"xml2js": "^0.4.17"
},
Expand Down

0 comments on commit a168004

Please sign in to comment.