
const shell = require('shelljs')
const {
  requireValue,
  execShell
  } = require('./services/utils')

module.exports = async (options, logger) => {

  let {
    keyfile = process.env.GCP_KEY_FILE || requireValue('keyfile'),
    project = process.env.GCP_PROJECT || requireValue('project')
  } = options

  execShell(`echo ${keyfile} | base64 -d > /tmp/key-file.json`)
  execShell(`gcloud auth activate-service-account --key-file /tmp/key-file.json`)
  execShell(`gcloud config set project ${project} -q`)
  execShell(`gcloud auth configure-docker -q`)

  let suffix = (process.platform === "win32") ? '' : ' >> ~/.bashrc'
  shell.env['GOOGLE_APPLICATION_CREDENTIALS'] = '/tmp/key-file.json'
  shell.exec(`echo 'export GOOGLE_APPLICATION_CREDENTIALS=/tmp/key-file.json' ${suffix}`)
  shell.exec('echo GOOGLE_APPLICATION_CREDENTIALS = $GOOGLE_APPLICATION_CREDENTIALS')

  return 0
}