Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
feat: add TOML config file (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Nov 30, 2018
1 parent 2321857 commit 50f911c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions bin/boltzm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const { argv } = require('yargs').options({
describe: 'Data directory of boltz-middleware',
type: 'string',
},
configpath: {
describe: 'Path to the config file',
type: 'string',
},
logpath: {
describe: 'Path to the log file',
type: 'string',
Expand Down
29 changes: 24 additions & 5 deletions lib/Config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Arguments } from 'yargs';
import fs from 'fs';
import path from 'path';
import toml from 'toml';
import { ApiConfig } from './api/Api';
import { BoltzConfig } from './boltz/BoltzClient';
import { getServiceDir, deepMerge, resolveHome } from './Utils';

// TODO: config file
class Config {
public logpath: string;
public loglevel: string;
Expand All @@ -17,8 +17,12 @@ class Config {
private defaultDataDir = getServiceDir('boltz-middleware');
private dataDir = this.defaultDataDir;

private configpath: string;

constructor() {
const { logpath } = this.getDataDirPaths(this.defaultDataDir);
const { configpath, logpath } = this.getDataDirPaths(this.defaultDataDir);

this.configpath = configpath;

this.logpath = logpath;
this.loglevel = process.env.NODE_ENV === 'production' ? 'info' : 'debug';
Expand Down Expand Up @@ -55,14 +59,28 @@ class Config {
}
});

// The data dir is not the default one therefore the paths
// dervied from it need to be updated
if (parameters.datadir && parameters.datadir !== this.defaultDataDir) {
if (parameters.datadir) {
this.dataDir = parameters.datadir;

deepMerge(this, this.getDataDirPaths(parameters.datadir));
}

if (fs.existsSync(this.configpath)) {
const config = fs.readFileSync(this.configpath, 'utf-8');

try {
const parsedConfig = toml.parse(config);

if (!parameters.datadir && parsedConfig.datadir) {
deepMerge(this, this.getDataDirPaths(parsedConfig.datadir));
}

deepMerge(this, parsedConfig);
} catch (error) {
throw `Error parsing config file in line ${error.line}:${error.column}: ${error.message}`;
}
}

deepMerge(this, parameters);

if (!fs.existsSync(this.dataDir)) {
Expand All @@ -72,6 +90,7 @@ class Config {

private getDataDirPaths = (dataDir: string) => {
return {
configpath: path.join(dataDir, 'boltz.conf'),
logpath: path.join(dataDir, 'boltz.log'),
};
}
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"cross-os": "^1.3.0",
"express": "^4.16.4",
"grpc": "^1.16.1",
"toml": "^2.3.3",
"winston": "^3.1.0",
"yargs": "^12.0.5"
},
Expand Down

0 comments on commit 50f911c

Please sign in to comment.