Skip to content

Commit

Permalink
Add proxyUrl setting
Browse files Browse the repository at this point in the history
  • Loading branch information
lekkas committed Apr 22, 2016
1 parent eb6a180 commit f27eae5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 55 deletions.
6 changes: 3 additions & 3 deletions build/config.js
Expand Up @@ -14,13 +14,13 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var path, userHome, utils;
var hidepath, path, userHome;

path = require('path');

userHome = require('home-or-tmp');

utils = require('./utils');
hidepath = require('hidepath');


/**
Expand All @@ -42,7 +42,7 @@ module.exports = {
* @property {String} user - path to user config
* @memberof paths
*/
user: path.join(userHome, utils.addHiddenPathPrefix('resinrc.yml')),
user: path.join(userHome, hidepath('resinrc.yml')),

/**
* @property {String} project - path to project config
Expand Down
17 changes: 14 additions & 3 deletions build/defaults.js
Expand Up @@ -14,13 +14,13 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var path, userHome, utils;
var hidepath, path, userHome;

path = require('path');

userHome = require('home-or-tmp');

utils = require('./utils');
hidepath = require('hidepath');


/**
Expand Down Expand Up @@ -85,11 +85,22 @@ module.exports = {
return "https://dashboard." + this.resinUrl;
},

/**
* @property {Function} proxyUrl - Resin.io Proxy url
* @memberof defaults
*/
proxyUrl: function() {
if (this.resinUrl === 'resin.io') {
return 'resindevice.io';
}
return "devices." + this.resinUrl;
},

/**
* @property {String} dataDirectory - data directory path
* @memberof defaults
*/
dataDirectory: path.join(userHome, utils.addHiddenPathPrefix('resin')),
dataDirectory: path.join(userHome, hidepath('resin')),

/**
* @property {String} projectsDirectory - projects directory path
Expand Down
50 changes: 1 addition & 49 deletions build/utils.js
Expand Up @@ -14,60 +14,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var os, path, url, _;
var url, _;

_ = require('lodash');

os = require('os');

url = require('url');

path = require('path');


/**
* @summary Add hidden prefix to path
* @function
* @protected
*
* @description
* This function adds `_` on Windows and '.' in UNIX based operating systems.
*
* @param {String} filePath - file path
* @returns {String} hidden file path
*
* @throws Will throw if no path.
*
* @example
* console.log(utils.addHiddenPathPrefix('foo'))
* > _path // On Windows
* > .path // On UNIX
*
* @example
* console.log(utils.addHiddenPathPrefix('/foo/bar/baz'))
* > /foo/bar/.baz
*
* @example
* console.log(utils.addHiddenPathPrefix('C:\\foo\\bar\\baz'))
* > C:\\foo\\bar\\_baz
*/

exports.addHiddenPathPrefix = function(filePath) {
var basename, delimiter, dirname;
filePath = filePath != null ? filePath.trim() : void 0;
if (_.isEmpty(filePath)) {
throw new Error('Missing path');
}
if (os.platform() === 'win32') {
delimiter = '_';
} else {
delimiter = '.';
}
dirname = path.dirname(filePath);
basename = path.basename(filePath);
return path.join(dirname, "" + delimiter + basename);
};


/**
* @summary Merge objects into one
Expand Down
9 changes: 9 additions & 0 deletions lib/defaults.coffee
Expand Up @@ -73,6 +73,15 @@ module.exports =
dashboardUrl: ->
return "https://dashboard.#{@resinUrl}"

###*
# @property {Function} proxyUrl - Resin.io Proxy url
# @memberof defaults
###
proxyUrl: ->
if @resinUrl is 'resin.io'
return 'resindevice.io'
return "devices.#{@resinUrl}"

###*
# @property {String} dataDirectory - data directory path
# @memberof defaults
Expand Down
12 changes: 12 additions & 0 deletions tests/defaults.spec.coffee
Expand Up @@ -90,6 +90,18 @@ describe 'Defaults:', ->
setting = utils.evaluateSetting(defaults, 'dashboardUrl')
m.chai.expect(url.parse(setting).protocol).to.equal('https:')

describe '.proxyUrl', ->

it 'should be a valid url', ->
setting = utils.evaluateSetting(defaults, 'proxyUrl')
m.chai.expect ->
url.parse(setting)
.to.not.throw(Error)

it 'should not contain a protocol', ->
setting = utils.evaluateSetting(defaults, 'proxyUrl')
m.chai.expect(url.parse(setting).protocol).to.not.exist

describe '.dataDirectory', ->

it 'should be an absolute path', ->
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/test.coffee
Expand Up @@ -32,12 +32,14 @@ wary.it 'should override defaults given a user configuration that points to stag
dashboardUrl: getSetting('dashboardUrl')
vpnUrl: getSetting('vpnUrl')
registryUrl: getSetting('registryUrl')
proxyUrl: getSetting('proxyUrl')
.then (settings) ->
m.chai.expect(settings.resinUrl).to.equal('resinstaging.io/')
m.chai.expect(settings.apiUrl).to.equal('https://api.resinstaging.io/')
m.chai.expect(settings.dashboardUrl).to.equal('https://dashboard.resinstaging.io/')
m.chai.expect(settings.vpnUrl).to.equal('vpn.resinstaging.io/')
m.chai.expect(settings.registryUrl).to.equal('registry.resinstaging.io/')
m.chai.expect(settings.proxyUrl).to.equal('devices.resinstaging.io/')

wary.it 'should give precedence to project configuration', {}, ->
fs.writeFileSync config.paths.user, '''
Expand All @@ -52,12 +54,14 @@ wary.it 'should give precedence to project configuration', {}, ->
dashboardUrl: getSetting('dashboardUrl')
vpnUrl: getSetting('vpnUrl')
registryUrl: getSetting('registryUrl')
proxyUrl: getSetting('proxyUrl')
.then (settings) ->
m.chai.expect(settings.resinUrl).to.equal('resin.custom.com/')
m.chai.expect(settings.apiUrl).to.equal('https://api.resin.custom.com/')
m.chai.expect(settings.dashboardUrl).to.equal('https://dashboard.resin.custom.com/')
m.chai.expect(settings.vpnUrl).to.equal('vpn.resin.custom.com/')
m.chai.expect(settings.registryUrl).to.equal('registry.resin.custom.com/')
m.chai.expect(settings.proxyUrl).to.equal('devices.resin.custom.com/')

wary.it 'should give predecende to environment variable configuration', {}, ->
fs.writeFileSync config.paths.user, '''
Expand All @@ -80,6 +84,7 @@ wary.it 'should be able to return all settings', {}, ->
imageMakerUrl: 'https://img.resindev.custom.com/'
deltaUrl: 'https://delta.resindev.custom.com/'
dashboardUrl: 'https://dashboard.resindev.custom.com/'
proxyUrl: 'devices.resindev.custom.com/'
dataDirectory: '/opt'
cacheDirectory: path.join('/opt', 'cache')
projectsDirectory: '/usr/src/projects'
Expand Down

0 comments on commit f27eae5

Please sign in to comment.