const AgentCLI = require('../../../clients/Agent')
const { validateNonEmpty } = require('../../../utils')
const { handleError } = require('../../../error')
const log = require('../../../logging')

async function builder (yargs) {
  yargs.option('app', {
    type: 'input',
    describe: 'Path to app file',
    demandOption: true,
    string: true,
    validateNonEmpty
  })
}

async function handler (argv) {
  try {
    const { app } = argv
    const agent = new AgentCLI(argv)
    log.info('Installing app...')
    await agent.installApp(app)
    log.info('App installed.')
  } catch (err) {
    handleError(err, 'Install app failed', argv.verbose)
  }
}

module.exports = {
  builder,
  handler,
  command: 'install',
  describe: 'Install app on device instance'
}
