import groovy.transform.Field
import com.sonymusic.Utils

@Field SNOWSQL_ECR_REPO = '086679231553.dkr.ecr.us-east-1.amazonaws.com/snowsql'

def call(Map args) {
    def params = Utils.validateParams('snowsql', args, [
            script: [type: String, required: true],
    ])

    assert env.SNOWSQL_ACCOUNT: 'SNOWSQL_ACCOUNT is mandatory'
    assert env.SNOWSQL_USER: 'SNOWSQL_USER is mandatory'
    assert env.SNOWSQL_DATABASE: 'SNOWSQL_DATABASE is mandatory'
    assert env.SNOWSQL_SCHEMA: 'SNOWSQL_SCHEMA is mandatory'
    assert env.SNOWSQL_ROLE: 'SNOWSQL_ROLE is mandatory'
    assert env.SNOWSQL_WAREHOUSE: 'SNOWSQL_WAREHOUSE is mandatory'
    assert env.SNOWSQL_PWD: 'SNOWSQL_PWD is mandatory'

    withEcr {
        sh """
            docker run \
              --rm \
              --pull always \
              -v \"\$(pwd):/workspace\" \
              -w /workspace \
              -e SNOWSQL_ACCOUNT \
              -e SNOWSQL_USER \
              -e SNOWSQL_DATABASE \
              -e SNOWSQL_SCHEMA \
              -e SNOWSQL_ROLE \
              -e SNOWSQL_WAREHOUSE \
              -e SNOWSQL_PWD \
              ${SNOWSQL_ECR_REPO} \
              snowsql -f ${params.script}
        """
    }
}
