balena-cli/lib/actions/examples.coffee
2015-01-06 13:15:40 -03:00

67 lines
1.6 KiB
CoffeeScript

async = require('async')
fs = require('fs')
path = require('path')
_ = require('lodash')
gitCli = require('git-cli')
resin = require('../resin')
permissions = require('../permissions/permissions')
ui = require('../ui')
log = require('../log/log')
errors = require('../errors/errors')
examplesData = require('../data/examples.json')
exports.list = permissions.user ->
examplesData = _.map examplesData, (example, index) ->
example.id = index + 1
return example
log.out ui.widgets.table.horizontal examplesData, (example) ->
example.author ?= 'Unknown'
return example
, [ 'ID', 'Display Name', 'Repository', 'Author' ]
exports.info = permissions.user (params) ->
id = params.id - 1
example = examplesData[id]
if not example?
error = new Error("Unknown example: #{id}")
errors.handle(error)
log.out ui.widgets.table.vertical example, (example) ->
example.id = id
example.author ?= 'Unknown'
return example
, [
'ID'
'Display Name'
'Description'
'Author'
'Repository'
]
exports.clone = permissions.user (params) ->
example = examplesData[params.id - 1]
if not example?
error = new Error("Unknown example: #{id}")
errors.handle(error)
async.waterfall [
(callback) ->
exampleAbsolutePath = path.join(process.cwd(), example.name)
fs.exists exampleAbsolutePath, (exists) ->
return callback() if not exists
error = new Error("Directory exists: #{example.name}")
return callback(error)
(callback) ->
log.info("Cloning #{example.display_name} to #{example.name}")
gitCli.Repository.clone(example.repository, example.name, callback)
], (error) ->
errors.handle(error) if error?