const execa = require('execa')
const yaml = require('js-yaml')
const extensions = ['yaml', 'yml', 'json']
const decrypt = file => {
  const extension = file.split('.').slice(-1)[0]
  if (!extensions.includes(extension)) {
    throw new Error(
      `File not supported (${extension}). Only yaml and json are supported`
    )
  }

  const { stdout, stderr } = execa.sync('sops', ['--decrypt', file])
  if (stderr && stderr.length > 0) throw new Error(stderr)

  return yaml.load(stdout)
}
module.exports = { decrypt }
