kinvey-cli
Version:
Utility for deploying and managing FlexServices on Kinvey's FlexService Runtime
131 lines (109 loc) • 4.78 kB
text/coffeescript
2016 Kinvey, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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.
fs = require 'fs'
async = require 'async'
chalk = require 'chalk'
validUrl = require 'valid-url'
KinveyError = require './error.coffee'
logger = require './logger.coffee'
request = require './request.coffee'
user = require './user.coffee'
exports.formatHost = (host) ->
validHost = validUrl.isHttpUri host
if validHost
if validHost.slice(-1) isnt '/' then validHost = validHost + '/'
return validHost
validHost = validUrl.isHttpsUri host
if validHost
if validHost.slice(-1) isnt '/' then validHost = validHost + '/'
return validHost
return 'https://' + host + '-manage.kinvey.com/'
exports.formatList = (list, name = 'name') ->
result = list.map (el) -> { name: el[name], value: el }
result.sort (x, y) ->
if x[name].toLowerCase() < y[name].toLowerCase() then -1 else 1
result
exports.formatHostList = (list) ->
result = list.map (el) -> { name: el, value: el }
result.unshift { name: 'all hosts', value: null }
result
exports.makeRequest = makeRequest = (options, cb) ->
options.method ?= 'GET'
if user.isLoggedIn()
options.headers ?= { }
options.headers?.Authorization = "Kinvey #{user.getToken()}"
logger.debug 'Request: %s %s', options.method, options.url
request.Request options, (err, response) ->
connErrors = [ 'ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT', 'PROTOCOL_CONNECTION_LOST' ]
if err?
if err.message.indexOf('ENOTFOUND') isnt -1
return cb new KinveyError 'InvalidConfigUrl'
for msg in connErrors
if err.message.indexOf(msg) isnt -1
return cb new KinveyError 'ConnectionError'
return cb err
logger.debug 'Response: %s %s %s', options.method, options.url, chalk.green response.statusCode
if 2 is parseInt response.statusCode / 100, 10
return cb null, response
if 'InvalidCredentials' is response.body?.code
logger.warn 'Invalid credentials, please authenticate.'
unless false is options.refresh
return user.refresh (err) ->
if err? then cb err
else makeRequest options, cb
if response.body?.code?
cb new KinveyError response.body.code, response.body.description
else
cb new KinveyError 'RequestError', response.statusCode
exports.readFile = readFile = (file, cb) ->
logger.debug 'Reading contents from file %s', chalk.cyan file
fs.readFile file, cb
exports.readJSON = readJSON = (file, cb) ->
logger.debug 'Reading JSON from file %s', chalk.cyan file
async.waterfall [
(next) -> readFile file, next
(data, next) ->
json = null
try
logger.debug 'Parsing JSON from file: %s', chalk.cyan file
json = JSON.parse data
catch e
logger.warn 'Invalid JSON in file %s', chalk.cyan file
return next e
next null, json
], cb
exports.writeFile = writeFile = (file, contents, cb) ->
logger.debug 'Writing contents to file %s', chalk.cyan file
fs.writeFile file, contents, cb
exports.writeJSON = writeJSON = (file, json, cb) ->
logger.debug 'Writing JSON to file %s', chalk.cyan file
contents = JSON.stringify json
writeFile file, contents, cb
Copyright