Commit ce5be375 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Fix #185 "init" command was not erasing existing conf with default conf

parent 7fdd449a
......@@ -4,6 +4,7 @@ var co = require('co');
var _ = require('underscore');
var sha1 = require('sha1');
var path = require('path');
var Configuration = require('../entity/configuration');
var Membership = require('../entity/membership');
var Merkle = require('../entity/merkle');
var Transaction = require('../entity/transaction');
......@@ -119,10 +120,10 @@ function FileDAL(profile, home, localDir, myFS, parentFileDAL, dalName, core, lo
var currency = '';
this.init = (overrideConf) => {
this.init = (overrideConf, defaultConf) => {
return co(function *() {
yield _.values(that.newDals).map((dal) => dal.init());
return yield that.loadConf(overrideConf);
return that.loadConf(overrideConf, defaultConf);
});
};
......@@ -1101,14 +1102,15 @@ function FileDAL(profile, home, localDir, myFS, parentFileDAL, dalName, core, lo
return that.confDAL.getParameters();
};
this.loadConf = function(overrideConf) {
return that.confDAL.loadConf()
.then(function(conf){
conf = _(conf).extend(overrideConf || {});
currency = conf.currency;
return conf;
});
};
this.loadConf = (overrideConf, defaultConf) => co(function *() {
let conf = Configuration.statics.complete(overrideConf || {});
if (!defaultConf) {
let savedConf = yield that.confDAL.loadConf();
conf = _(savedConf).extend(overrideConf || {});
}
currency = conf.currency;
return conf;
});
this.saveConf = function(confToSave) {
currency = confToSave.currency;
......
......@@ -209,10 +209,8 @@ function simpleFloat (question, property, conf, done) {
}, done);
}
function simplePercentOrPositiveInteger (question, property, conf, done) {
simpleValue(question, property, conf[property], conf, function (input) {
return input && (input.toString().match(/^[1-9][0-9]*$/) || input.toString().match(/^0\.[0-9]+$/)) ? true : false;
}, done);
function getRandomPort() {
return ~~(Math.random() * (65536 - constants.NETWORK.PORT.START)) + constants.NETWORK.PORT.START;
}
function upnpResolve(noupnp, done) {
......@@ -220,7 +218,7 @@ function upnpResolve(noupnp, done) {
var client = upnp.createClient();
var privateIP = null, publicIP = null;
// Look for 2 random ports
var privatePort = ~~(Math.random() * (65536 - constants.NETWORK.PORT.START)) + constants.NETWORK.PORT.START;
var privatePort = getRandomPort();
var publicPort = privatePort;
logger.info('Checking UPnP features...');
async.waterfall([
......@@ -434,7 +432,10 @@ function getLocalNetworkOperations(conf, autoconf) {
next();
});
},
autoconf ? (done) => done() : async.apply(simpleInteger, "Port", "port", conf)
autoconf ? (done) => {
conf.port = getRandomPort();
done();
} : async.apply(simpleInteger, "Port", "port", conf)
];
}
......
......@@ -184,12 +184,12 @@ program
program
.command('init [host] [port]')
.description('Setup a node configuration and sync data with given node')
.action(connect(bootstrapServer, true));
.action(connect(bootstrapServer, true, true));
program
.command('forward [host] [port] [what]')
.description('Forward local blockchain to a remote uCoin node')
.action(service(function (host, port, what, server, conf) {
.action(service(function (host, port, what, server) {
var localCurrent;
var remoteCurrent;
......@@ -698,7 +698,7 @@ function commandLineConf(conf) {
return _(conf).extend({ routing: true });
}
function connect(callback, forConf) {
function connect(callback, forConf, useDefaultConf) {
return function () {
var cbArgs = arguments;
var dbName = program.mdb || "ucoin_default";
......@@ -706,7 +706,7 @@ function connect(callback, forConf) {
var server = ucoin({ name: dbName }, commandLineConf());
// Initialize server (db connection, ...)
server.connectDB(forConf)
server.connectDB(forConf, useDefaultConf)
.then(function(){
cbArgs.length--;
cbArgs[cbArgs.length++] = server;
......
......@@ -48,9 +48,9 @@ function Server (dbConf, overrideConf) {
});
};
this.connectDB = function (forConf) {
this.connectDB = function (forConf, useDefaultConf) {
// Connect only once
return connectionPromise || (connectionPromise = that.connect(forConf));
return connectionPromise || (connectionPromise = that.connect(forConf, useDefaultConf));
};
this.initWithServices = function (done) {
......@@ -93,7 +93,7 @@ function Server (dbConf, overrideConf) {
});
};
this.connect = function (forConf) {
this.connect = function (forConf, useDefaultConf) {
// Init connection
if (that.dal) {
return Q();
......@@ -102,7 +102,7 @@ function Server (dbConf, overrideConf) {
return dbType(dbConf.name || "default", forConf)
.then(function(dal){
that.dal = dal;
return that.dal.init(overrideConf);
return that.dal.init(overrideConf, useDefaultConf);
})
.then(function(conf){
that.conf = conf;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment