Move os.generateCacheName outside Resin SDK

This commit is contained in:
Juan Cruz Viotti 2014-12-22 14:50:54 -04:00
parent 8c6db6608d
commit cda24c9e1b
5 changed files with 13 additions and 43 deletions

View File

@ -9,6 +9,7 @@ ui = require('../ui')
log = require('../log/log')
permissions = require('../permissions/permissions')
errors = require('../errors/errors')
cache = require('../cache/cache')
exports.download = (params, options) ->
networkParams =
@ -16,7 +17,7 @@ exports.download = (params, options) ->
wifiSsid: options.ssid
wifiKey: options.key
fileName = resin.os.generateCacheName(params.id, networkParams)
fileName = cache.generateCacheName(params.id, networkParams)
outputFile = options.output or path.join(resin.settings.get('directories.os'), fileName)
async.waterfall [

7
lib/cache/cache.coffee vendored Normal file
View File

@ -0,0 +1,7 @@
exports.generateCacheName = (id, connectionParams) ->
result = "#{id}-#{connectionParams.network}"
if connectionParams.wifiSsid?
result += "-#{connectionParams.wifiSsid}"
return "#{result}-#{Date.now()}"

View File

@ -3,9 +3,9 @@ sinon = require('sinon')
chai = require('chai')
chai.use(require('chai-string'))
expect = chai.expect
os = require('./os')
cache = require('./cache')
describe 'OS:', ->
describe 'Cache:', ->
describe '#generateCacheName()', ->
@ -17,7 +17,7 @@ describe 'OS:', ->
params:
network: 'ethernet'
result = os.generateCacheName(application.id, application.params)
result = cache.generateCacheName(application.id, application.params)
expect(result).to.match(new RegExp("#{application.id}-ethernet-\\d\+\$"))
describe 'given network is wifi', ->
@ -28,5 +28,5 @@ describe 'OS:', ->
network: 'wifi'
wifiSsid: 'MYSSID'
result = os.generateCacheName(application.id, application.params)
result = cache.generateCacheName(application.id, application.params)
expect(result).to.match(new RegExp("#{application.id}-wifi-#{application.params.wifiSsid}-\\d\+\$"))

View File

@ -5,6 +5,5 @@ module.exports =
data: require('./data/data')
auth: require('./auth/auth')
device: require('./device/device')
os: require('./os/os')
vcs: require('./vcs/vcs')
settings: require('./settings')

View File

@ -1,37 +0,0 @@
# Generate os cache name
#
# It generates an unique name for a certain instance of the os
# with id and connection params embedded in the filename.
#
# @param {String, Number} id application id
# @param {Object} connectionParams connection parameters
# @option connectionParams {String} network network type
# @option connectionParams {String} wifiKey wifi key
# @option connectionParams {String} wifiSsid wifi ssid
#
# @return {String} os cache name
#
# @note For security reasons, the wifiSsid is omitted from the filename
# @note The original filename extension (*.zip in this case) is lost in the renaming process
#
# @example Generate cache name for ethernet os
# result = resin.os.generateCacheName(51, { network: 'ethernet' })
# console.log(result)
# # 51-ethernet-1418040928724
#
# @example Generate cache name for wifi os
# result = resin.os.generateCacheName(51, {
# network: 'wifi'
# wifiKey: 'MyWifi'
# wifiSsid: 'secret'
# })
# console.log(result)
# # 51-wifi-MyWifi-1418040928724
#
exports.generateCacheName = (id, connectionParams) ->
result = "#{id}-#{connectionParams.network}"
if connectionParams.wifiSsid?
result += "-#{connectionParams.wifiSsid}"
return "#{result}-#{Date.now()}"