const { handleError } = require('../../error')
const { displayTable } = require('../../table')
const { getApi } = require('../../utils')

async function builder (yargs) {
  yargs.option('verbose', {
    alias: 'v',
    type: 'boolean',
    describe: 'Console will receive verbose error output'
  }).option('format', {
    type: 'input',
    describe: 'Output format (default is json) e.g. table',
    string: true,
    choices: ['table', 'json', 'csv']
  })
}

async function handler (argv) {
  const api = await getApi()
  try {
    const projects = await api.v1GetProjects()
    displayTable(argv.format, projects, ['id', 'name'])
  } catch (err) {
    handleError(err, 'List failed', argv.verbose)
  }
}

module.exports = {
  builder,
  handler,
  command: 'list',
  aliases: ['ls'],
  describe: 'List projects'
}
